MSMQ通过WCF吞下格式错误的消息,没有错误,没有警告

时间:2010-10-01 12:45:39

标签: wcf msmq netmsmqbinding ierrorhandler

我有一个使用WCF响应MSMQ上的消息的服务。当消息格式正确时,它正常工作,我们很高兴。当消息用于不同的服务,并且操作不同时,消息保留在队列中,我们获取日志条目,我们修复,我们很高兴。

但是当消息具有正确的操作,但包含无法由服务器反序列化的XML元素时,消息将从队列中消失,我们不会获得日志条目,我们接下来不知道发生了什么。我们不高兴。

我尝试过添加IErrorHandler。我试过添加一个Faulted事件处理程序。我唯一能做的就是使用内置于WCF的诊断日志记录。有没有什么方法可以用代码,我自己的记录器或错误处理程序来响应这样的错误?

我的队列碰巧是交易性的,但这个问题似乎没什么区别。这发生在Windows Server 2003上的MSMQ 3.0版上。

此源代码显示问题:

class Program : IErrorHandler {
    private static Uri QueueUri = new Uri("net.msmq://localhost/private/server_queue");

    static void Main(string[] args) {
        Program program = new Program();

        if (args.Length > 0) {
            if (args[0] == "server") {
                program.Server();
            } else if (args[0] == "bad") {
                program.BadClient();
            }
        } else {
            program.Client();
        }
    }

    public void BadClient() {
        using (var client = new BadServiceClient(QueueUri)) {
            client.Do(new [] {new BadStuff { BadMessage = "hi" }});
        }
    }

    public void Client() {
        using (var client = new ServiceClient(QueueUri)) {
            client.Do(new [] {new Stuff {Message = "hi"}});
        }
    }

    public void Server() {
        var serviceHost = new ServiceHost(typeof(Service));
        serviceHost.AddServiceEndpoint(typeof (IService), new NetMsmqBinding(), QueueUri);
        serviceHost.Open();

        serviceHost.Faulted += serviceHost_Faulted;
        serviceHost.UnknownMessageReceived += new EventHandler<UnknownMessageReceivedEventArgs>(serviceHost_UnknownMessageReceived);
        foreach (ChannelDispatcher dispatcher in serviceHost.ChannelDispatchers) {
            dispatcher.ErrorHandlers.Add(this);
        }

        Console.WriteLine("press the any key");
        Console.ReadKey(true);
    }

    void serviceHost_UnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e) {
        Console.WriteLine("unknown message: {0}", e);
    }

    private static void serviceHost_Faulted(object sender, EventArgs e) {
        Console.WriteLine("fault: {0}", e);
    }

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault) {
        Console.WriteLine("handling error: {0}", error);
    }

    public bool HandleError(Exception error) {
        Console.WriteLine("handling error: {0}", error);
        return true;
    }
}

[ServiceContract]
public interface IService {
    [OperationContract(IsOneWay = true, Action = "do")]
    void Do(IEnumerable<Stuff> stuffs);
}

[ServiceContract]
public interface IBadService {
    [OperationContract(IsOneWay = true, Action = "do")]
    void Do(IEnumerable<BadStuff> stuffs);
}

class ServiceClient : ClientBase<IService>, IService {
    public ServiceClient(Uri uri) : base(new NetMsmqBinding(), new EndpointAddress(uri)) {}

    public void Do(IEnumerable<Stuff> stuffs) {
        Channel.Do(stuffs);
    }
}

class BadServiceClient : ClientBase<IBadService>, IBadService {
    public BadServiceClient(Uri uri) : base(new NetMsmqBinding(), new EndpointAddress(uri)) {}

    public void Do(IEnumerable<BadStuff> stuffs) {
        Channel.Do(stuffs);
    }
}

[ServiceBehavior(TransactionTimeout = "00:5:00", InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
class Service : IService {
    [OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]
    public void Do(IEnumerable<Stuff> stuffs) {
        foreach (var stuff in stuffs) {
            Console.WriteLine("service doing stuff, message: " + stuff.Message);
        }
    }
}

[DataContract]
public class Stuff {
    [DataMember]
    public string Message;
}

[DataContract]
public class BadStuff : Stuff {
    [DataMember]
    public string BadMessage;
}

1 个答案:

答案 0 :(得分:0)

现在已经很晚了,但是对于像我这样可能有类似问题的其他人也可以给出答案。请注意,MSMQ的使用与无法提取格式错误的消息无关。 Tim提到使用IDispatchMessageInspector实现时,如果因为格式错误(例如包含&amp; char)而无法反序列化XML,则不会给出消息。

我唯一能找到的是打开常规WCF消息跟踪,其中logMalformedMessages在messageLogging元素下根据本文档设置为true(希望MS不移动链接):

Configuring Message Logging