Search code examples
wcfws-discovery

Prevent DiscoveryEndpoint creating service instance


I am using DiscoveryEndpoints in WCF but I have noticed that when a service is being discovered and the DiscoveryEndpoint is contacted it will actually cause an instance of the service to be created. I do not want this.

This is almost certainly related to the fact I am using a custom instance provider (to support StructureMap) - which applies custom InstanceProvider to each EndpointDispatcher.

It seems I only want to apply the custom InstanceProvider for the endpoints whose contract actually match the service implementation.

Any ideas?


Solution

  • I think I worked it out... I am just ignoring anything that has IsSystemEndpoint set:

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcherBase cdb in serviceHostBase.ChannelDispatchers)
            {
                ChannelDispatcher cd = cdb as ChannelDispatcher;
                if (cd != null)
                {
                    foreach (EndpointDispatcher ed in cd.Endpoints)
                    {
                        if (!ed.IsSystemEndpoint) // Ignore MEX etc
                            ed.DispatchRuntime.InstanceProvider =
                                new StructureMapInstanceProvider(serviceDescription.ServiceType);
                    }
                }
            }
        }