c# – 无法让自己的主机在NServiceBus上工作

前端之家收集整理的这篇文章主要介绍了c# – 无法让自己的主机在NServiceBus上工作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用版本2.0.0.1219

我试图通过NServiceBus和VS2010自我托管订阅者和发布者.程序运行并初始化但我无法获取消息.发布者表现得像是发帖,没有错误,但订阅者什么都没收到.

这是订户配置

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig,NServiceBus.Core"/>
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig,NServiceBus.Core"/>
  </configSections>

  <!-- in order to configure remote endpoints use the format: "queue@machine" 
       input queue must be on the same machine as the process Feeding off of it.
       error queue can (and often should) be on a different machine.
  -->

  <MsmqTransportConfig InputQueue="loads" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"/>

  <UnicastBusConfig>
    <MessageEndpointMappings>
      <add Messages="NServiceMessage" Endpoint="loads"/>
    </MessageEndpointMappings>
  </UnicastBusConfig>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>

和发布者配置

<?xml version="1.0"?>
<configuration>

  <configSections>
    <section name="MsmqTransportConfig" type="NServiceBus.Config.MsmqTransportConfig,NServiceBus.Core"/>
  </configSections>

  <MsmqTransportConfig InputQueue="loads" ErrorQueue="error" NumberOfWorkerThreads="1" MaxRetries="5"/>

  <UnicastBusConfig DistributorControlAddress="" DistributorDataAddress="" ForwardReceivedMessagesTo="">
    <MessageEndpointMappings>
      <!-- publishers don't need to set this for their own message types -->
      <!--<add Messages="Messages" Endpoint="messagebus" />-->
    </MessageEndpointMappings>
  </UnicastBusConfig>  

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

</configuration>

这是发布商代码

class Program
{
    private static IBus _serviceBus;

    static void Main(string[] args)
    {
        _serviceBus = Configure.With()
            .Log4Net()
            .DefaultBuilder()
            .XmlSerializer()
            .MsmqSubscriptionStorage()
            .MsmqTransport()
            .UnicastBus()
            .LoadMessageHandlers()
            .CreateBus()
            .Start();

        while (true)
        {
            Console.WriteLine("Press a key to send data.");
            Console.ReadKey();
            SendMessaage();
        }
    }


    private static void SendMessaage()
    {
        LoadMessage message = GetNextMessage();
        _serviceBus.Publish(message);
    }

    private static LoadMessage GetNextMessage()
    {
        LoadMessage result = new LoadMessage();

        result.DeliveryDate = DateTime.Today.AddDays(3).ToShortDateString();
        result.DestinationCity = "Boise";
        result.DestinationCountry = "USA";
        result.DestinationState = "ID";
        result.EventId = Guid.NewGuid();
        result.Time = DateTime.Now.ToUniversalTime();
        result.OriginState = "OR";
        result.OriginCity = "Portland";
        result.OriginCountry = "USA";
        result.EquipmentID = 3;

        return result;
    }
}

和订户代码

class Program
{
    private static IBus _serviceBus;
    private static LoadMessageHandler _messageHandler;

    static void Main(string[] args)
    {
        _messageHandler = new LoadMessageHandler();

        _serviceBus = Configure
           .With()
           .Log4Net()
           .DefaultBuilder()
           .BinarySerializer()
           .MsmqSubscriptionStorage()
           .MsmqTransport()
           .UnicastBus()
           .LoadMessageHandlers()
           .CreateBus()
           .Start();

        Console.ReadKey();
    }
}

和消息代码

public class LoadMessageHandler : IHandleMessages<LoadMessage>
{
    public void Handle(LoadMessage message)
    {
        Console.WriteLine(String.Format("GUID: {0}",message.EventId));
    }
}

解决方法

更多问题:

1:必须将msmq传输配置为事务性,以便发布者接受订阅消息.有关配置这些内容的示例,请参见http://blogs.planbsoftware.co.nz/?p=234.

2:Publisher正在使用XmLSerializer,而订阅者正在使用BinarySerializer,这使它们不兼容.

原文链接:https://www.f2er.com/csharp/243504.html

猜你在找的C#相关文章