"singleton" DelegatingHandler

Topics: Web Api
Dec 21, 2011 at 10:43 AM

Given the following example:

    public class LoggingHandler : DelegatingHandler
    {
        public LoggingHandler()
        {
        }

        public LoggingHandler(HttpMessageHandler innerChannel)
            : base(innerChannel)
        {
        }

    }
    
    [ServiceContract]
    public class ServiceClass1
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass1";
        }
    }


    [ServiceContract]
    public class ServiceClass2
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass2";
        }

    }

    public static class ServicesInitializer
    {
        public static void Start()
        {
            var logginghandler = new LoggingHandler();

            var config1 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };
            RouteTable.Routes.Add(new ServiceRoute("Service1", new HttpServiceHostFactory { Configuration = config1 }, typeof(ServiceClass1)));

            var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };

            //var logginghandler2 = new LoggingHandler();
            //var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler2 } };

            RouteTable.Routes.Add(new ServiceRoute("Service2", new HttpServiceHostFactory { Configuration = config2 }, typeof(ServiceClass2)));
        }
   }

If I try to run the services, then the first one works fine, but the second one fails... can only get one to work at any given time....

If I then uncomment the two lines, (and comment out the line above), then things work fine....

How come ?

TIA

Søren

Dec 21, 2011 at 5:45 PM

You can pass in the same logging handler instance in both configs:

      //var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };

The reason why you have to configure it twice is that each route entry results in a different service host with independent configuration and handlers. However, they can easily be the same handler instances.

Henrik

From: smolesen [email removed]
Sent: Wednesday, December 21, 2011 2:44 AM
To: Henrik Frystyk Nielsen
Subject: "singleton" DelegatingHandler [wcf:283722]

From: smolesen

Given the following example:

    public class LoggingHandler : DelegatingHandler
    {
        public LoggingHandler()
        {
        }
 
        public LoggingHandler(HttpMessageHandler innerChannel)
            : base(innerChannel)
        {
        }
 
    }
    
    [ServiceContract]
    public class ServiceClass1
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass1";
        }
    }
 
 
    [ServiceContract]
    public class ServiceClass2
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass2";
        }
 
    }
 
    public static class ServicesInitializer
    {
        public static void Start()
        {
            var logginghandler = new LoggingHandler();
 
            var config1 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };
            RouteTable.Routes.Add(new ServiceRoute("Service1", new HttpServiceHostFactory { Configuration = config1 }, typeof(ServiceClass1)));
 
            var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };
 
            //var logginghandler2 = new LoggingHandler();
            //var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler2 } };
 
            RouteTable.Routes.Add(new ServiceRoute("Service2", new HttpServiceHostFactory { Configuration = config2 }, typeof(ServiceClass2)));
        }
   }

If I try to run the services, then the first one works fine, but the second one fails... can only get one to work at any given time....

If I then uncomment the two lines, (and comment out the line above), then things work fine....

How come ?

TIA

Søren

Dec 21, 2011 at 6:17 PM

Well, Glenn pointed out that I was too optimistic here. Message handlers do indeed get wired together so you can’t reuse them across service hosts.

This means that you will have to create two instances.

Sorry about that


Henrik

From: Henrik Frystyk Nielsen
Sent: Wednesday, December 21, 2011 9:45 AM
To: 'wcf@discussions.codeplex.com'
Subject: RE: "singleton" DelegatingHandler [wcf:283722]

You can pass in the same logging handler instance in both configs:

      //var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };

The reason why you have to configure it twice is that each route entry results in a different service host with independent configuration and handlers. However, they can easily be the same handler instances.

Henrik

From: smolesen [email removed]
Sent: Wednesday, December 21, 2011 2:44 AM
To: Henrik Frystyk Nielsen
Subject: "singleton" DelegatingHandler [wcf:283722]

From: smolesen

Given the following example:

    public class LoggingHandler : DelegatingHandler
    {
        public LoggingHandler()
        {
        }
 
        public LoggingHandler(HttpMessageHandler innerChannel)
            : base(innerChannel)
        {
        }
 
    }
    
    [ServiceContract]
    public class ServiceClass1
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass1";
        }
    }
 
 
    [ServiceContract]
    public class ServiceClass2
    {
        [WebGet(UriTemplate = "")]
        public string Get()
        {
            return "ServiceClass2";
        }
 
    }
 
    public static class ServicesInitializer
    {
        public static void Start()
        {
            var logginghandler = new LoggingHandler();
 
            var config1 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };
            RouteTable.Routes.Add(new ServiceRoute("Service1", new HttpServiceHostFactory { Configuration = config1 }, typeof(ServiceClass1)));
 
            var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler } };
 
            //var logginghandler2 = new LoggingHandler();
            //var config2 = new WebApiConfiguration { MessageHandlerFactory = () => new List { logginghandler2 } };
 
            RouteTable.Routes.Add(new ServiceRoute("Service2", new HttpServiceHostFactory { Configuration = config2 }, typeof(ServiceClass2)));
        }
   }

If I try to run the services, then the first one works fine, but the second one fails... can only get one to work at any given time....

If I then uncomment the two lines, (and comment out the line above), then things work fine....

How come ?

TIA

Søren