队列显示为空

时间:2014-11-25 20:53:49

标签: c# asp.net-mvc wcf msmq netmsmqbinding

所以这对我来说很新鲜,而且我正在取得进步。我目前有一个使用netmsmqbinding的wcf服务来向队列发送一条简单的消息。一切都运行顺畅,事件视图甚至说消息已放入队列中。虽然当我转到实际队列时,那里什么都没有。我有一个服务,服务主机和asp网络应用程序。请注意,在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 of Service:

<?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日志的事件:修改了部分内容以便在此处查看

  1. ID为CN = msmq的消息,CN = blah,OU = blah,OU = blah,DC = blah,DC = local \ 8280已发送到队列PRIVATE = blah

  2. ID为CN = msmq,CN = blah,OU = blah,OU = blah,DC = blah,DC = local \ 8280的消息被置于队列中PRIVATE = blah

  3. 收到消息

2 个答案:

答案 0 :(得分:0)

这似乎与您的交易有关。

您应该尝试将[TransactionFlow(TransactionFlowOption.Allowed)]放在服务合同中的操作上。类似的东西:

 [ServiceContract]
 public interface IMSMQService
 {
   [OperationContract(IsOneWay = true)]
   [TransactionFlow(TransactionFlowOption.Allowed)]
   void ShowMessage(string msg);
 } 

无论如何,如果您决定在Web场或涉及多台计算机的设备上部署此事务,您应该了解本地事务。

答案 1 :(得分:0)

想出来。我不得不在servicecontract中添加一个命名空间。之前它会默认为某种时空。

[ServiceContract(Namespace = "http://MyService")]
public interface IMSMQService
{
    [OperationContract(IsOneWay = true)]
    [TransactionFlow(TransactionFlowOption.Allowed)]
    void ShowMessage(string msg);
}