1

I'm writing some services in WCF to be called by a Silverlight client. I change status code to 200 every time a fault is to be returned, via a IDispatchMessageInspector.

It works almost perfect, but sometimes it keeps returning error 500: NotFound.

I have just written another IDispatchMessageInspector to commit changes in a ObjectContext. But when this fails, the error handler is not called.

I think by the time UnitOfWorkMessageInspector runs, the message was already been set up as a non-fault-response. How can I do both things work?

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    throw new FaultException("It was changed by another user. Try again.");
                }
            }
        }
André Werlang
  • 5,839
  • 1
  • 35
  • 49

2 Answers2

0

Code 500 is Internal Error.

Some error is occuring and it is not caught on the server side.

In your code you only catch OptimisticConcurrencyException you need to also catch Exception.

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252
  • I'm kind of catching all exceptions via a IDispatchMessageInspector and changing status code to 200 when it faults. It doesn't work when the exception was thrown in another IDispatchMessageInspector. – André Werlang Feb 27 '11 at 15:13
  • How can I change status code from 500 to 200 if the fault only occurs in BeforeSendReply? – André Werlang Feb 27 '11 at 15:15
  • You need to go through all your code and check if there are any uncaught exceptions, also within the IDispatchMessageInspector and in catch blocks. – Shiraz Bhaiji Feb 27 '11 at 15:24
0

I managed to make it work. I figured out my IDispatchMessageInspector for switching from status code 500 to 200 wouldn't work because the message was already built, then I replaced the message.

    public class UnitOfWorkMessageInspector : IDispatchMessageInspector
    {
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
            if (!reply.IsFault)
            {
                try
                {
                    UnitOfWorkBase.Commit();
                }
                catch (OptimisticConcurrencyException)
                {
                    FaultException fe = new FaultException("It was changed by another user. Try again.");
                    reply = Message.CreateMessage(reply.Version, fe.CreateMessageFault(), fe.Action);
                }
            }
        }
André Werlang
  • 5,839
  • 1
  • 35
  • 49