Explicitly calling a service constructor when hosting a WCF web service on IIS

前端 未结 3 587
迷失自我
迷失自我 2021-01-06 10:19

I want to host a WCF service of mine on Microsoft IIS (IIS hosting).

To do this, I created my service:

// The service
public class MyService : IMySer         


        
相关标签:
3条回答
  • 2021-01-06 10:42

    You must write several custom classes to support creating service with parameters:

    • Custom class implementing IInstanceProvider. This class will be responsible for creating your service instance with your non default constructor.
    • Custom class implementing IServiceBehavior. This class will be responsible for adding custom instance provider into endpoint dispatcher.
    • Custom ServiceHost which will apply your behavior.
    • Custom ServiceHostFactory which will instantiate your custom service host. You will reference this factory from .svc file.

    This is generally the same as building support for dependency injection. You can check for example this article.

    0 讨论(0)
  • 2021-01-06 10:57

    If all you need is to call a specific constructor in your service, then you only need to implement an IInstanceProvider and attach an IServiceBehavior to your service:

    IInstanceProvider

    public class ServiceInstanceProvider : IInstanceProvider
    {
        public object GetInstance(InstanceContext instanceContext)
        {
            return this.GetInstance(instanceContext, null);
        }
    
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return new MyService(...);
        }
    
        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {}
    }
    

    IServiceBehavior as Attribute

    public class InstanceProviderBehaviorAttribute : Attribute, IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription,
                ServiceHostBase serviceHostBase,
                Collection<ServiceEndpoint> endpoints,
                BindingParameterCollection bindingParameters)
        {}
    
        public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
                ServiceHostBase serviceHostBase)
        {
            foreach (ChannelDispatcher cd in serviceHostBase.ChannelDispatchers)
            {
                foreach (EndpointDispatcher ed in cd.Endpoints)
                {
                    if (!ed.IsSystemEndpoint)
                    {
                        ed.DispatchRuntime.InstanceProvider = new ServiceInstanceProvider();
                    }
                }
            }
        }
    
        public void Validate(ServiceDescription serviceDescription,
                ServiceHostBase serviceHostBase)
        {}
    }
    

    MyService with the custom ServiceBehavior Attribute

    [InstanceProviderBehavior]
    public class MyService : IMyService {
        public MyService() { }
        public MyService(...) : this() {
            ...
        }
       ...
    }
    

    More info on this here:

    • Carlos Figueira MSDN blog: WCF Extensibility – IInstanceProvider
    • Carlos Figueira MSDN blog: WCF Extensibility
    0 讨论(0)
  • 2021-01-06 11:04

    Take a look at Castle WCF Facility (I use this one in production) or Autofac WCF integration

    0 讨论(0)
提交回复
热议问题