所以这对我来说很新,我正在取得进展。我目前有一个使用 netmsmqbinding 向队列发送简单消息的 wcf 服务。一切运行顺利,事件视图甚至说消息已放入队列。虽然当我进入实际队列时,那里什么都没有。我有一个服务、服务主机和 asp web 应用程序。请注意,在 Web 应用程序中,我有对该服务的服务引用。同样在事件查看器的 msmq 日志中,我得到 3 个事件。任何援助将不胜感激。谢谢。
这是我的设置。
服务接口:
[ServiceContract]
public interface IMSMQService
{
[OperationContract(IsOneWay = true)]
void ShowMessage(string msg);
}
服务:
public class MSMQService : IMSMQService
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void ShowMessage(string msg)
{
Debug.WriteLine(msg + " Received at: " + System.DateTime.Now.ToString());
}
}
服务的 App.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
<appSettings>
<!-- use appSetting to configure MSMQ queue name -->
<add key="queueName" value=".\private$\TestQueue"/>
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.serviceModel>
<services>
<service name="MSMQNoSecurityService.MSMQService" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/msmq"/>
</baseAddresses>
</host>
<endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding" bindingConfiguration="MyBinding" contract="MyService.IMSMQService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="MyBinding">
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/></startup></configuration>
服务主机
static void Main(string[] args)
{
ServiceHost svcHost = null;
try
{
// Get MSMQ queue name from appsettings in configuration.
string queueName = ConfigurationManager.AppSettings["queueName"];
// Create the transacted MSMQ queue if necessary.
if (!MessageQueue.Exists(queueName))
MessageQueue.Create(queueName, true);
svcHost = new ServiceHost(typeof(MyService.MSMQService));
svcHost.Open();
Console.WriteLine("\n\nService is Running at following address");
Console.WriteLine("\nhttp://localhost:9001/MSMQService");
}
catch (Exception eX)
{
svcHost = null;
Console.WriteLine("Service can not be started \n\nError Message [" + eX.Message + "]");
}
if (svcHost != null)
{
Console.WriteLine("\nPress any key to close the Service");
Console.ReadKey();
svcHost.Close();
svcHost = null;
}
}
服务主机的App.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
<appSettings>
<add key="queueName" value=".\private$\TestQueue"/>
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<services>
<service name="MyService.MSMQService" behaviorConfiguration="MyBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/msmq"/>
</baseAddresses>
</host>
<endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding" bindingConfiguration="MyBinding" contract="MyService.IMSMQService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<netMsmqBinding>
<binding name="MyBinding" >
<security mode="None"/>
</binding>
</netMsmqBinding>
</bindings>
</system.serviceModel>
</configuration>
MVC 控制器
public class WcfMsmqController : Controller
{
public ActionResult Index()
{
MSMQServiceClient proxy = new MSMQServiceClient();
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
{
proxy.ShowMessage("test");
scope.Complete();
}
proxy.Close();
return View();
}
}
Web.config 中的服务模型(自动生成):
<system.serviceModel>
<bindings>
<netMsmqBinding>
<binding name="NetMsmqBinding_IMSMQService">
<security mode="None" />
</binding>
</netMsmqBinding>
</bindings>
<client>
<endpoint address="net.msmq://localhost/private/TestQueue" binding="netMsmqBinding"
bindingConfiguration="NetMsmqBinding_IMSMQService" contract="ServiceReference1.IMSMQService"
name="NetMsmqBinding_IMSMQService" />
</client>
</system.serviceModel>
MSMQ 日志中的事件:修改了其中的一些以在此处查看
ID 为 CN=msmq,CN=blah,OU=blah,OU=blah,DC=blah,DC=local\8280 的消息已发送到队列 PRIVATE=blah
ID 为 CN=msmq,CN=blah,OU=blah,OU=blah,DC=blah,DC=local\8280 的消息被放入队列 PRIVATE=blah
收到消息