【问题标题】:Pass a parameter to a WCF service constructor将参数传递给 WCF 服务构造函数
【发布时间】:2012-09-05 09:01:38
【问题描述】:

我需要将 exist DI 容器作为参数传递给 WCF 服务构造函数 为了做到这一点,我正在使用 IInstanceProvider WCF 服务应托管在自托管中。

public class CustomInstanceProvider : IInstanceProvider, IContractBehavior
{
    private readonly IUnityContainer UnityContainer;

    public CustomInstanceProvider(IUnityContainer unityContainer)
    {
        if (unityContainer == null)
        {
            throw new ArgumentNullException("unityContainer");
        }

        UnityContainer = unityContainer;
    }


    #region Implementation of IInstanceProvider

    public object GetInstance(InstanceContext instanceContext)
    {
        return new Service(UnityContainer);
    }

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

    }

    #endregion

    #region Implementation of IContractBehavior

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {

    }

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

    }

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

    }


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

    }

    #endregion
}

我也实现了 CustomServiceHost

  public class CustomServiceHost : ServiceHost
    {
        public CustomServiceHost(IUnityContainer unityContainer, Type serviceType, params Uri[] baseAddresses)
        : base(serviceType, baseAddresses)
    {
        if (unityContainer == null)
        {
            throw new ArgumentNullException("unityContainer is null");
        }

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

    }
}

和服务主机工厂

public class CustomServiceHostFactory : ServiceHostFactory
{
    private readonly IUnityContainer UnityContainer;
    public CustomServiceHostFactory(IUnityContainer unityContainer)
    {
        UnityContainer = unityContainer;
    }

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

}

我创建 WCF 服务:

           var uris = new Uri[1];
        uris[0] = new Uri("http://localhost:8793/Service/");
        CustomServiceHostFactory factory = new CustomServiceHostFactory(Container);
        CustomServiceHost serviceHost =  (CustomServiceHost)factory.CreateServiceHost("guy",uris);
        ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(IService), new WSHttpBinding(), "");
        serviceHost.Open();

我遇到了一个异常:

 An exception occurred while initializing module 'PluginsModule'. 

- The exception message was: 'ServiceHostFactory.CreateServiceHost' cannot be invoked within the current hosting environment. This API requires that the calling application be hosted in IIS or WAS.

- The Assembly that the module was trying to be loaded from was:<"PluginsModule.plugin" , Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Check the InnerException property of the exception for more information. If the exception occurred while creating an object in a DI container, you can exception.GetRootException() to help locate the root cause of the problem.

请帮忙

谢谢。

【问题讨论】:

  • 您不想以类型作为第一个参数而不是字符串来调用 CreateServiceHost 吗?那是您已覆盖的方法
  • 当您调用 (CustomServiceHost)factory.CreateServiceHost("guy",uris); 时是否会调用 return new CustomServiceHost(UnityContainer, serviceType, baseAddresses);
  • 不,我也重写了 CreateServiceHost(string constructorString, Uri[] baseAddresses) 并且当我调用它返回 base.CreateServiceHost(constructorString, baseAddresses) 时它到达那里并失败
  • public override ServiceHostBase CreateServiceHost (string service, Uri[] baseAddresses) { // The service parameter is ignored here because we know our service. ServiceHost serviceHost = new ServiceHost(typeof(HelloService), baseAddresses); return serviceHost; }

标签: c# wcf c#-4.0 self-hosting


【解决方案1】:

好的, 问题是我不需要使用 CustomInstanceProvider。 ServiecInstanceProvider 仅用于 IIS。 我希望我的服务托管在自托管中。

        var uris = new Uri[1];
        uris[0] = new Uri("http://localhost:8793//Service/ntService/");
        var serviceHost = new CustomServiceHost(Container,typeof(Service),uris);
        serviceHost.AddDefaultEndpoints();

这就是我需要做的: 实现 IInstanceProvider 和 ServiceHost。 现在我可以将参数传递给我的构造函数了。

谢谢

【讨论】:

    猜你喜欢
    • 2013-10-09
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2015-10-11
    相关资源
    最近更新 更多