文檔的例子可以在這裏找到http://msdn.microsoft.com/en-us/library/ms789008.aspx和http://msdn.microsoft.com/en-us/library/ms751493.aspx使用netMsmqBinding
我知道周圍IIS7的應用程序池的問題無法啓動沒有收到消息(我使用的變暖前技術,試圖解決這個獲得) ,但是我無法讓我的WCF Web服務接收來自MSMQ的消息,即使在本地PC上的Visual Studio 2010中的WebDev下運行時也是如此。
下面的簡單測試是基於上述文檔,但即使他們簡單的演示似乎並不奏效,這讓我覺得我忽略了一些非常明顯的東西。
如果您構建以下,並使用下面的測試線,只有第二個將被寫出。第一個將進入MSMQ,然後永遠不會被目的地接收。
a)「這是一個測試。」 b)「!這是一個不同的測試。」
我建立,它發送一個數據塊到一個服務端點控制檯的客戶端:
class Program
{
static void Main(string[] args)
{
string input;
while (!string.IsNullOrEmpty(input = Console.ReadLine()))
{
if (input.StartsWith("!") && input.Length > 1)
{
using (var client = new MSMQClient.DirectServiceRef.Service1Client())
{
client.Log(input.Substring(1));
}
}
else
{
using (var client = new MSMQClient.LogServiceRef.Service2Client())
{
client.Log(input);
}
}
}
Console.WriteLine("Done.");
Console.ReadKey();
}
}
App.config中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService2">
<security mode="None" />
</binding>
<binding name="BasicHttpBinding_IService1">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:1910/Service2.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService2" contract="LogServiceRef.IService2"
name="BasicHttpBinding_IService2" />
<endpoint address="http://localhost:1909/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="DirectServiceRef.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
</configuration>
服務2是接收輸入一個簡單的服務,併發送它進入MSMQ。
[ServiceContract]
public interface IService2
{
[OperationContract]
bool Log(string data);
}
public class Service2 : IService2
{
public Service2()
{
var queueName = ConfigurationManager.AppSettings["queueName"];
if (!MessageQueue.Exists(queueName))
{
MessageQueue.Create(queueName, true);
}
}
[OperationBehavior]
public bool Log(string data)
{
using (var client = new MSMQService2.MSMQServiceRef.Service1Client())
{
using (var scope = new TransactionScope(TransactionScopeOption.Required))
{
client.Log(data);
scope.Complete();
}
}
return true;
}
}
的Web.config:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="queueName" value=".\private$\Service1.svc" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="msmqBinding">
<security mode="None" />
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="logService">
<endpoint binding="basicHttpBinding"
contract="MSMQService2.IService2" />
</service>
</services>
<client>
<endpoint address="net.msmq://localhost/private/Service1.svc"
binding="netMsmqBinding"
bindingConfiguration="msmqBinding"
contract="MSMQServiceRef.IService1" />
</client>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
服務1是一個簡單的服務,其接收輸入,並將其記錄到本地文件。
[ServiceContract]
public interface IService1
{
[OperationContract(IsOneWay=true)]
void Log(string data);
}
public class Service1 : IService1
{
public Service1()
{
var queueName = ConfigurationManager.AppSettings["queueName"];
if (!MessageQueue.Exists(queueName))
{
MessageQueue.Create(queueName, true);
}
}
[OperationBehavior(TransactionAutoComplete=true, TransactionScopeRequired=true)]
public void Log(string data)
{
using (var log = new FileInfo(ConfigurationManager.AppSettings["logFilePath"]).AppendText())
{
log.WriteLine(data);
}
}
}
Web.config文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="logFilePath" value="C:\testlog.txt"/>
<add key="queueName" value=".\private$\Service1.svc"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="msmqBinding">
<security mode="None" />
</binding>
</netMsmqBinding>
</bindings>
<services>
<service name="logService">
<endpoint address="net.msmq://localhost/private/Service1.svc"
binding="netMsmqBinding"
bindingConfiguration="msmqBinding"
contract="MSMQService1.IService1" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
這最後的服務似乎並沒有從MSMQ到 「接收」 的消息。當從我的桌面運行解決方案時,或者將服務部署到Server 2008/IIS7部署並遠程或本地運行客戶端時,都會發生這種情況。
我錯過了什麼,或者是否有一些選項我應該在Windows設置中檢查?
Regards, Rob。
謝謝,我會試一試,MSMQ事件日誌看起來很有用。 WCF跟蹤幫助確定服務是否甚至檢查MSMQ隊列? – peridian