locked
'System.ServiceModel' TraceSource doesn't log the exceptions caused in IErrorHandler.ProviderFault RRS feed

  • Question

  • Hello,

    I use the following codes to apply my own error handler to my services. In regular case, it works well. But I find if some exception occurs in the ProvideFault method, the WCF tracing seems not to log the exception to the listener, the client will get the typical error: "The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

    It doesn't work to use 'try-catch' in the whole block in the ProvideFault method to catch all the exceptions. For example, In my case, some dependency assmebly is missing and the .NET runtime will throw FileNotFoundException: Could not load file or assembly 'xxxx, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.]. I could not catch this exception in the ProviderFault.

    It seems that WCF runtime 'swallows' some exception but it's worse that its tracing does not record these exception. This 'bug' is nasty. Because I can't get any log about this kind of exception and have no thread to know why the service crash. How can get I all the kinds of exceptions ocurring inner the WCF request/response process.

    Thanks a lot.

     

        public class MyServiceBehavior : Attribute, IServiceBehavior

        {

            public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)

            {

     

            }

     

            public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)

            {

                foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)

                {

                    channelDispatcher.ErrorHandlers.Add(new MyErrorHandler());

                }

     

            }

     

            public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)

            {

            }

        }

     

        public class MyErrorHandler : IErrorHandler

        {

            public bool HandleError(Exception error)

            {

                return false;

            }

     

            public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)

            {

                throw new Exception("Some exception could not be caught in this method");

            }

        }

     

     

     

      <system.diagnostics> 

        <sources>

          <source name="System.ServiceModel"

               switchValue="Information"

               propagateActivity="false" >

            <listeners>

              <add name="messages"

                type="System.Diagnostics.XmlWriterTraceListener"

                initializeData="E:\WcfTraces.svclog" />

            </listeners>  

          </source>

     


    问题要简单,错误须详细@错误/异常/堆栈信息+操作系统+软件版本+all the context of the issue Hope Helpful | http://www.leoworks.net
    Sunday, September 4, 2011 5:53 PM

Answers

  • Hello, where is the exception thrown? IErrorHandler can only handle exceptions thrown in your service implementation. If the exception is thrown elsewhere, it won't be caught. For example, security related exceptions won't be caught as they're thrown on the wire. I'm not sure about missing assembly, though.

    In addition, please do not throw exceptions in ProvideFault. This may lead to unexpected errors. Instead, create a FaultException, and set the fault parameter's value to it. You can refer to http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx for a sample.

    Also note missing assembly exceptions should not be returned to the client, except for debugging purposes. When debugging, it is recommended to set IncludeExceptionDetailsInFault to true, which will automatically send detailed error messages to the client. Exceptions such as missing assembly are bugs in your service, which you must fix before it goes live. After the service goes live, if you still encounter internal server errors, you should not return detailed messages to the client.


    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    Windows Azure Technical Forum Support Team Blog
    • Marked as answer by Yi-Lun Luo Friday, September 9, 2011 9:03 AM
    Tuesday, September 6, 2011 1:53 AM