【发布时间】:2012-09-03 08:54:21
【问题描述】:
我有一个检查 MSMQ 消息的 WCF Windows 服务。 它可以正常接收消息,但似乎没有调用 ProcessMSMQMessage 事件。
有什么想法吗?我是否正确设置了 ProcessMSMQMessage 事件?还是我错过了什么?
我的代码如下。谢谢。
WCF 服务类...
public partial class MyService : ServiceBase
{
private ServiceHost host;
public MyService()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
string queueName = ConfigurationManager.AppSettings["ProcessMsgQueueName"];
if (!MessageQueue.Exists(queueName))
{
MessageQueue thisQueue = MessageQueue.Create(queueName, true);
thisQueue.SetPermissions("Everyone", MessageQueueAccessRights.ReceiveMessage);
}
try
{
Uri serviceUri = new Uri("msmq.formatname:DIRECT=OS:" + queueName);
// communicate to MSMQ how to transfer and deliver the messages
MsmqIntegrationBinding serviceBinding = new MsmqIntegrationBinding();
serviceBinding.Security.Transport.MsmqAuthenticationMode = MsmqAuthenticationMode.None;
serviceBinding.Security.Transport.MsmqProtectionLevel = System.Net.Security.ProtectionLevel.None;
serviceBinding.SerializationFormat = MsmqMessageSerializationFormat.Binary;
host = new ServiceHost(typeof(MyService.Service1)); // add watcher class name
host.AddServiceEndpoint(typeof(MyService.IService1), serviceBinding, serviceUri);
host.Open();
}
catch (Exception ex)
{
EventLog.WriteEntry("SERVICE" + ex.Message, EventLogEntryType.Error);
}
}
protected override void OnStop()
{
if (host != null)
host.Close();
}
}
IService1 合同...
[ServiceContract(Namespace = "MyService")]
[ServiceKnownType(typeof(Events.Dashboard_Message))]
public interface IService1
{
[OperationContract(IsOneWay = true)]
void ProcessMSMQMessage(MsmqMessage<Events.Dashboard_Message> msg);
}
Service1 类...
public class Service1 : IService1
{
[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
public void ProcessMSMQMessage(MsmqMessage<Events.Dashboard_Message> msg)
{
string msgName = msg.GetType().Name;
// send to eventlog
EventLog.WriteEntry("MyService", msgName);
}
}
【问题讨论】:
-
终于搞定了!!问题出在 IService1 合同中。需要将 Action = "" 添加到 [OperationContract(IsOneWay = true, Action = "")]
-
我明白了……哎呀!无法提交您的答案,因为: •声誉低于 10 的用户在提问后 8 小时内无法回答自己的问题。您可以在 17 分钟内自行回答。在此之前,请使用 cmets,或编辑您的问题。
-
没关系。错误消息告诉您发生了什么。根据您的声誉,对用户有时间限制。同时。此限制已过期,现在您可以将答案放入新帖子中。