6

使用版本 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"/>
    <section name="UnicastBusConfig" type="NServiceBus.Config.UnicastBusConfig, 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));
    }
}
4

2 回答 2

5

您似乎对两个端点使用相同的输入队列,将您的订阅者移动到它自己的队列,看看是否有效。

也不需要为您的订阅者配置订阅存储,因为它是负责存储订阅信息的发布者。

希望这可以帮助!

于 2010-08-25T16:54:45.633 回答
5

更多问题:

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

2:发布者使用 XmlSerializer,订阅者使用 BinarySerializer,这使得它们不兼容。

于 2010-08-25T19:46:24.180 回答