【问题标题】:WCF Msmq problem reading messages using netMsmqBinding使用 netMsmqBinding 读取消息的 WCF Msmq 问题
【发布时间】:2011-02-25 13:43:02
【问题描述】:

我有一个使用 netMsmqBinding 的 WCF 服务,用于将 Msmq<string> 的消息添加到队列中。消息添加得很好,我可以通过计算机管理控制台在队列中看到它们。

我有另一个 WCF 服务正在尝试从队列中检索消息,这就是我遇到问题的地方。每当将消息添加到队列中时(该位工作正常),我的服务中的方法就会被调用,但 Msmq<string> 消息似乎具有所有空值。

我不确定如何从Msmq<string> 那里得到消息?这是我的服务细节...任何帮助表示赞赏..

[ServiceContract]
[ServiceKnownType(typeof(Msmq<string>))]
public interface IMessageListener
{
   [OperationContract(IsOneWay = true, Action = "*")]
    void ListenForMessage(Msmq<string> msg);
}

public class MessageListener : IMessageListener
{
    [OperationBehavior(TransactionScopeRequired = false, TransactionAutoComplete = true)]
    public void ListenForMessage(MsmqMessage<string> msg)
    {
         //this gets called and seems to remove the message from the queue, but message attributes are all null
    }
}

【问题讨论】:

  • 首先:您的实现类中的方法不需要 [OperationContract] - 实际上,您不应该在那里拥有该属性

标签: wcf netmsmqbinding


【解决方案1】:

我认为您并没有完全“理解”WCF over MSMQ 的想法。

当使用带有 netMsmqBinding 的 WCF 时,整个想法是您不需要处理 MSMQ 的细节 - 让 WCF 运行时处理!

所以基本上,您的方法应该与任何 WCF 服务一样:

  • 定义您的服务合同及其方法(运营合同)
  • 将您的数据结构定义为 [DataContract] 并在您的服务方法中使用这些结构
  • 实现服务

所以你的服务应该是这样的:

[DataContract]
public class Customer
{
   [DataMember]
   public int ID { get; set; }

   [DataMember]
   public string Name { get; set; }

   ...
}

[ServiceContract]
public interface ICustomerService
{
    [OperationContract(IsOneWay=true)]
    void SaveCustomer(Customer myCustomer)

    [OperationContract(IsOneWay=true)]
    void CreateCustomer(int ID, string name);
}

您应该有一个数据合同来描述您的数据 - 只需您的数据,此处不需要 MSMQ 详细信息!然后你应该有一组服务方法来处理Customer 对象 - 你可以将它放入队列中进行存储,创建一个新的等等。

然后,您将为此服务合同实现客户端和服务器端,WCF 运行时将处理 MSMQ 传输的所有细节,将有效负载(Customer 对象)放入 MSMQ 消息中并将其取出再等等……你不必处理这个,真的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-03
    • 2012-03-18
    • 2012-02-13
    • 1970-01-01
    • 2010-10-16
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多