English 中文(简体)
WCF on IIS: Passing data from a ServiceHostFactory to the Service cases
原标题:WCF on IIS: Passing data from a ServiceHostFactory to the Service instances
  • 时间:2010-11-10 11:38:37
  •  标签:
  • iis
  • wcf

I am having a WCF application hosted on IIS. I am initializing an IoC container in a custom ServiceHostFactory.

基本上,我想要能够把IoC集装箱“inject”运至在该服务公司范围内设立的各个服务机构。

你们如何做到这一点?

Ariel

最佳回答

和你一样,你将需要一个习俗服务机构,用于创建你的服务。 与此类似:

public class SessionPerCallServiceHostFactory : ServiceHostFactory
{
    public SessionPerCallServiceHostFactory()
    {
    }

    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        return new SessionPerCallServiceHost(serviceType, baseAddresses);
    }
}

你们还需要一个负责创造必要服务的服务机构:

public class SessionPerCallServiceHost : ServiceHost
{
    public SessionPerCallServiceHost()
    {
    }

    public SessionPerCallServiceHost(Type serviceType, params Uri[] baseAddresses)
    : base(serviceType, baseAddresses)
    {
    }

    protected override void OnOpening()
    {
        Description.Behaviors.Add(new SessionPerCallServiceBehavior());
        base.OnOpening();
    }

}

采用惯用的ISERBehavior,能够提供用来制造服务的提供人:

public class SessionPerCallServiceBehavior : IServiceBehavior
{
    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)
                {                        
                    ed.DispatchRuntime.InstanceProvider =
                        new SessionPerCallInstanceProvider(serviceDescription.ServiceType);
                }
            }
        }
    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters)
    {
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
    }

}

最后,将允许你利用国际奥委会在所请求的服务场合注入你所希望的任何东西的助手:

public class SessionPerCallInstanceProvider : IInstanceProvider
{
    private readonly Type _serviceType;

    public SessionPerCallInstanceProvider(Type serviceType)
    {
        _serviceType = serviceType;
    }

    public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
    {
        //I m creating it without any tricks but you could use your IoC container here
        return Activator.CreateInstance(_serviceType);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return GetInstance(instanceContext, null);
    }

    public void ReleaseInstance(System.ServiceModel.InstanceContext instanceContext, object instance)
    {

    }
}

希望!

问题回答

暂无回答




相关问题
Session Management with Windows Authentication

In an ASP.NET web app, using Integrated Windows Authentication, is the session tied to the windows identity? In other words, if I login (using IWA) to the app, and the app stores some "stuff" in my ...

Using Elmah with Cassini

Does anyone know if I can use Elmah with Visual Studio build-in web server(aka Cassini)? I get it working easily on IIS, but using same configuration, it doesn t work with Cassini. When I requested ...

Setting hostname in IIS, include www?

I want to set the hostname for a website I m adding in IIS 7, however do I include the www in the hostname or not? because I want both www.mysite.com and mysite.com both to point to mysite on the ...

inetpub versus any other folder

I ve run websites out of inetpub, as well as from folders just residing on the C: drive. I wonder, are there any definitive advantages to running websites out of inetputwwwroot?

IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

热门标签