使用 netMsmqBinding 读取消息的 WCF Msmq 问题

发布于 2024-10-19 16:35:34 字数 891 浏览 0 评论 0原文

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

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

我不确定如何从 Msmq 获取消息?这是我的服务详细信息...任何帮助表示赞赏..

[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
    }
}

I have a WCF service using netMsmqBinding that I am using to add messages of Msmq<string> to a queue. The messages are added fine and I can see them in the queue via the computer management console.

I have another WCF service that is trying to retrieve the messages from the queue, this is where I'm having a problem. My method in my service is getting called whenever a message is added to the queue (that bit is working fine) but the Msmq<string> message seems to have all null values.

I'm not sure how I can get the message from that Msmq<string>? Here is my service details... any help appreciated..

[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
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-10-26 16:35:34

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

当将 WCF 与 netMsmqBinding 结合使用时,整个想法是您不需要需要处理 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 消息中,然后再次将其取出,等等...真的不必处理这个问题。

I think you're not quite "getting" the idea of WCF over MSMQ.

When using WCF with the netMsmqBinding, the whole idea is that you don't need to deal with the details of MSMQ - let the WCF runtime handle that!

So basically, your approach should be as with any WCF service:

  • define your service contract and its methods (operation contract)
  • define your data structures as a [DataContract] and use those in your service methods
  • implement the service

So your service should be something like:

[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);
}

You should have a data contract to describe your data - just your data, no MSMQ details needed here! Then you should have one set of service methods that will deal with the Customer object - you can put it into the queue for storing, create a new one etc.

You would then implement the client and the server side for this service contract, and the WCF runtime will handle all the details of MSMQ transport, putting the payload (the Customer object) into a MSMQ message and getting it back out again and so on... you don't have to deal with that, really.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文