队列显示为空

本文关键字:显示 队列 | 更新日期: 2023-09-27 18:32:01

所以这对我来说是很新的,我正在取得进展。 我目前有一个使用 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());
    }
}

应用配置服务:

<?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;
    }   
}

服务主机的应用配置:

<?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=
  1. blah,OU=blah,DC

    =blah,DC=local''8280 的消息被发送到队列 PRIVATE=blah

  2. ID 为 CN=msmq,CN=blah,OU=
  3. blah,OU=blah,DC=blah,DC=local''8280 的消息被放入队列 PRIVATE=blah

  4. 收到的消息

队列显示为空

它似乎与您的交易有关。

应尝试在服务协定中对操作进行[TransactionFlow(TransactionFlowOption.Allowed)]。像这样:

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

无论如何,如果您决定将其部署在 Web 场或涉及多台计算机的内容上,则应注意本地事务。

通了。 我不得不向服务合同添加一个命名空间。 在此之前,它会默认为某种天普里。

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