如何将值传递给 wcf 服务上的构造函数? [英] How do I pass values to the constructor on my wcf service?

查看:25
本文介绍了如何将值传递给 wcf 服务上的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将值传递给实现我的服务的类的构造函数.

I would like to pass values into the constructor on the class that implements my service.

但是,ServiceHost 只允许我传入要创建的类型的名称,而不是要传递给其构造函数的参数.

However ServiceHost only lets me pass in the name of the type to create, not what arguments to pass to its contrstructor.

我希望能够传入一个创建我的服务对象的工厂.

I would like to be able to pass in a factory that creates my service object.

到目前为止我发现了什么:

What I have found so far:

  • WCF Dependency Injection Behavior which is more than what I am looking for and seems to be over-complex for my needs.

推荐答案

您需要实现自定义 ServiceHostFactoryServiceHostIInstanceProvider<的组合/代码>.

You'll need to implement a combination of custom ServiceHostFactory, ServiceHost and IInstanceProvider.

给定具有此构造函数签名的服务:

Given a service with this constructor signature:

public MyService(IDependency dep)

这是一个可以启动 MyService 的示例:

Here's an example that can spin up MyService:

public class MyServiceHostFactory : ServiceHostFactory
{
    private readonly IDependency dep;

    public MyServiceHostFactory()
    {
        this.dep = new MyClass();
    }

    protected override ServiceHost CreateServiceHost(Type serviceType,
        Uri[] baseAddresses)
    {
        return new MyServiceHost(this.dep, serviceType, baseAddresses);
    }
}

public class MyServiceHost : ServiceHost
{
    public MyServiceHost(IDependency dep, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        if (dep == null)
        {
            throw new ArgumentNullException("dep");
        }

        foreach (var cd in this.ImplementedContracts.Values)
        {
            cd.Behaviors.Add(new MyInstanceProvider(dep));
        }
    }
}

public class MyInstanceProvider : IInstanceProvider, IContractBehavior
{
    private readonly IDependency dep;

    public MyInstanceProvider(IDependency dep)
    {
        if (dep == null)
        {
            throw new ArgumentNullException("dep");
        }

        this.dep = dep;
    }

    #region IInstanceProvider Members

    public object GetInstance(InstanceContext instanceContext, Message message)
    {
        return this.GetInstance(instanceContext);
    }

    public object GetInstance(InstanceContext instanceContext)
    {
        return new MyService(this.dep);
    }

    public void ReleaseInstance(InstanceContext instanceContext, object instance)
    {
        var disposable = instance as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }

    #endregion

    #region IContractBehavior Members

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
        dispatchRuntime.InstanceProvider = this;
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
    }

    #endregion
}

在您的 MyService.svc 文件中注册 MyServiceHostFactory,或直接在代码中使用 MyServiceHost 用于自托管方案.

Register MyServiceHostFactory in your MyService.svc file, or use MyServiceHost directly in code for self-hosting scenarios.

您可以轻松地推广这种方法,事实上,一些 DI 容器已经为您完成了此操作(提示:Windsor 的 WCF 设施).

You can easily generalize this approach, and in fact some DI Containers have already done this for you (cue: Windsor's WCF Facility).

这篇关于如何将值传递给 wcf 服务上的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆