【问题标题】:Cannot get self host to work on NServiceBus无法让自己的主机在 NServiceBus 上工作
【发布时间】:2010-08-25 15:49:24
【问题描述】:

使用版本 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));
    }
}

【问题讨论】:

  • 需要更完整的代码,包括命名空间和目标 dll,才能确定此问题的原因。 中的NServiceMessage指的是定义LoadMessage类型的dll。

标签: c# .net nservicebus servicebus


【解决方案1】:

更多问题:

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

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

【讨论】:

  • 谢谢,成功了,这是我错的后半部分。
【解决方案2】:

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

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-08
    相关资源
    最近更新 更多