Unanswered Health Monitoring and WCF

  • Friday, February 17, 2012 2:32 PM
     
      Has Code

    I'd like to use Health Monitoring to log unhandled exceptions that I trap in my own Error Handler:

    public class MyErrorHandler : IErrorHandler, IServiceBehavior
    {
        #region IServiceBehavior Members
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<serviceendpoint> endpoints, BindingParameterCollection bindingParameters)
        {
        }
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            IErrorHandler errorHandler = new VirusInfoErrorHandler();
    
            foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher;
    
                if (channelDispatcher != null)
                {
                    channelDispatcher.ErrorHandlers.Add(errorHandler);
                }
            }
        }
    
        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
        }
    
        #endregion
    
        #region IErrorHandler Members
    
        public bool HandleError(Exception ex)
        {
            MyErrorEvent l_errEvt = new MyErrorEvent("Oh Noes!", this, WebEventCodes.WebExtendedBase + 2, ex);
    
            // Always throws a "Value does not fall within the expected range" ArgumentException.
            l_errEvt.Raise();                
    
            return true;
        }
    
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
        }
    
        #endregion    
    }</serviceendpoint>
    public MyErrorEvent(string message, object eventSource, int eventCode, Exception exception)
        : base(message, eventSource, WebEventCodes.WebExtendedBase + eventCode, exception)
    {
                
    }
    
    public override void FormatCustomEventDetails(WebEventFormatter formatter)
    {
        if (formatter == null)
        {
            throw new ArgumentNullException("formatter", "Please supply a valid formatter object.");
        }
    
        try
        {
            base.FormatCustomEventDetails(formatter);
    
            formatter.AppendLine("this was a bad error");
        }
        catch (Exception ex)
        {
    
        }
    }
    I've created a very simple custom WebRequestErrorEvent (see above).

    The exception's stack trace is:

    at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
    at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
    at System.Web.HttpRequest.get_UrlInternal()
    at System.Web.Management.WebRequestInformation..ctor()
    at System.Web.Management.WebRequestErrorEvent.PreProcessEventInit()
    at System.Web.Management.WebBaseEvent.RaiseInternal(WebBaseEvent eventRaised, ArrayList firingRuleInfos, Int32 index0, Int32 index1)
    at System.Web.Management.WebBaseEvent.Raise(WebBaseEvent eventRaised)
    at System.Web.Management.WebBaseEvent.Raise()
    at ACME.MyErrorHandler.HandleError(Exception ex) in G:\Code\MySvc\Lib\ACME.ErrorHandler\MyErrorHandler.cs:line 78
    at System.ServiceModel.Dispatcher.ErrorBehavior.HandleErrorCommon(Exception error, ErrorHandlerFaultInfo& faultInfo)


    Can a WebRequestErrorEvent not be used with WCF?  I can raise WebRequestEvents in non-Exception scenarios (ex: informational) with no problems, but not WebRequestErrorEvents.





    • Edited by _Bullines Friday, February 17, 2012 2:37 PM
    • Edited by _Bullines Friday, February 17, 2012 2:40 PM
    • Edited by _Bullines Friday, February 17, 2012 2:41 PM
    • Edited by _Bullines Friday, February 17, 2012 4:50 PM
    •  

All Replies

  • Sunday, February 19, 2012 12:57 PM
    Moderator
     
     
    Hello, I haven't used WebRequestErrorEvent. What error code do you get in ThrowExceptionForHRInternal? Just a wild guess, try to enable ASP.NET compatibility mode if you haven't, as this class seems to be an ASP.NET class.

    Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
    If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.

  • Tuesday, February 21, 2012 2:54 PM
     
      Has Code

    I have enabled ASP.NET Compatibility Mode in both my web.config:

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

    and the service itself:

    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]

    Interestingly, if I create a custom WebRequestEvent and add a parameter to its constructor for an Exception, the result is the same as with my custom WebRequestErrorEvent.

  • Thursday, March 01, 2012 12:26 PM