wcfcustom-errorswebhttpbindingwebhttp

wcf CustomWebHttpBehavior works only on the first endpoint


I have a wcf (.net 4.5) with one service and multiple interfaces\end points. This service is declared as follows:

   <service name="MyService.Service1">
                    <endpoint address="Try1" behaviorConfiguration="restfulBehvaiour"
                      binding="webHttpBinding" contract="MyService.IService1" />

                    <endpoint address="Try2" behaviorConfiguration="restfulBehvaiour"
                      binding="webHttpBinding" contract="MyService.ITry" />
                  </service>
...
    <behavior name="restfulBehvaiour">
                <webHttp helpEnabled="true" />
            </behavior>

I am trying to return any exception as json. I have followd the tutorial on http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/

In short:

1) On the svc file, added this (it implements both interfaces)

<%@ ServiceHost Language="C#" Debug="true" Service="MyService.Service1" CodeBehind="Service1.svc.cs" Factory="MyService.CustomWebServiceHostFactory"%>

2) where CustomWebServiceHostFactory is

 public class CustomWebServiceHostFactory : System.ServiceModel.Activation.WebServiceHostFactory
{
    public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
    {

        var sh = new ServiceHost(typeof(Service1), baseAddresses);

        sh.Description.Endpoints[0].Behaviors.Add(new CustomWebHttpBehavior());
        return sh;

    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {

        return base.CreateServiceHost(serviceType, baseAddresses);

    }

3) and the custom CustomWebHttpHandler is

protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {

        // clear default error handlers.

        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();

        // add our own error handler.

        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandlerEx());

    }

4) and the ErrorHandlerEx is some class that handles the exceptions (returns json object).

this all work great for the first end point (Try1), but the second one (Try2) is being ignored and not going threw the CustomWebServiceHostFactry.

If I switch the order of the endpoints in web.config, the first one always works and the seconds exceptions are being handled by the default wcf handlers.

How can I fix this behaviour, so that every end point will work as the above tutorial suggests?


Solution

  • You only implement the behavior on one endpoint (the first one) in your custom service host.

    sh.Description.Endpoints[0].Behaviors.Add(new CustomWebHttpBehavior());
    

    Endpoints[0] is the first endpoint in the collection. You need to add it to both (or all if you have more than 2) endpoints for the service. I recommend a foreach loop:

    foreach (ServiceEndpoint endpoint in sh.Description.Endpoints)
    {
        endpoint.Behaviors.Add(new CustomWebHttpBehavior());
    }
    

    This should resolve the issue of the behavior only being applied to the first endpoint.