【问题标题】:Can WCF Service have constructors?WCF 服务可以有构造函数吗?
【发布时间】:2010-09-27 18:22:26
【问题描述】:

当我在我的解决方案中新建一个 WCF 服务时,我可以执行以下操作,有一个带参数的构造函数要传入吗?如果是,运行时如何、何时以及在何处填充我所需的 IBusinessLogic 对象?

[ServiceContract]
public interface IServiceContract
{
    [OperationContract]
    ...
}

public class MyService : IServiceContract
{
    IBusinessLogic _businessLogic;
    public ServiceLayer(IBusinessLogic businessLogic)
    {
        _businessLogic = businessLogic;
    }
    ...
}

【问题讨论】:

标签: wcf dependency-injection constructor


【解决方案1】:

开箱即用的 WCF 将仅使用默认构造函数,您不能使用参数化构造函数。您必须做一些额外的工作才能使 WCF 调用参数化构造函数。

你可以试试这个:

How do I pass values to the constructor on my wcf service?

【讨论】:

  • 这不是真的。你可以让它使用非默认的构造函数,但是有几个箍要跳过
  • @steve - 是的,我应该对此更清楚,并说“开箱即用”。
  • 默认构造函数是什么?我有一个无参数构造函数,启动 WebServiceHost 时不会调用它...
  • @Ted - 这取决于您定义的服务行为。
【解决方案2】:

【讨论】:

    【解决方案3】:

    您可以让 WCF(某种程度上间接地)调用非默认构造函数,为此您需要滚动您自己的实例提供程序。您需要实现 IInstanceProvider 并添加自定义服务行为。一些链接将向您展示如何与 Spring.NET 结合使用:

    WCF Service Dependency Injection

    Code example WCF Service Dependency Injection

    【讨论】:

      【解决方案4】:

      除了其他响应之外,另一种情况是创建单例服务时 - 这是当您将服务实例传递给 ServiceHost(而不是类型)时;

      显然,当您创建实例时,您可以使用任何构造函数;

      这种方法需要向您的服务添加一个属性:[ServiceBehavior(InstanceContextMode.Single)];

      【讨论】:

        【解决方案5】:

        我将@Mark Seemann 的解决方案解释为通用实例提供程序行为

        使用方法:

        var host = new ServiceHost(typeof(MyService), baseAddress);
        var instanceProvider = new InstanceProviderBehavior<T>(() => new MyService(businessLogic));
        instanceProvider.AddToAllContracts(host);
        

        InstanceProviderBehavior 代码:

        public class InstanceProviderBehavior<T> : IInstanceProvider, IContractBehavior
            where T : class
        {
          private readonly Func<T> m_instanceProvider;
        
          public InstanceProviderBehavior(Func<T> instanceProvider)
          {
            m_instanceProvider = instanceProvider;
          }
        
          // I think this method is more suitable to be an extension method of ServiceHost.
          // I put it here in order to simplify the code.
          public void AddToAllContracts(ServiceHost serviceHost)
          {
            foreach (var endpoint in serviceHost.Description.Endpoints)
            {
              endpoint.Contract.Behaviors.Add(this);
            }
          }
        
          #region IInstanceProvider Members
        
          public object GetInstance(InstanceContext instanceContext, Message message)
          {
            return this.GetInstance(instanceContext);
          }
        
          public object GetInstance(InstanceContext instanceContext)
          {
            // Create a new instance of T
            return m_instanceProvider.Invoke();
          }
        
          public void ReleaseInstance(InstanceContext instanceContext, object instance)
          {
            try
            {
              var disposable = instance as IDisposable;
              if (disposable != null)
              {
                disposable.Dispose();
              }
            }
            catch { }
          }
        
          #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
        }
        

        【讨论】:

          【解决方案6】:

          您必须实现 IInstanceProvider 才能调用参数化的服务构造函数。这个构造函数在生成的代理中将不可用。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-08
            • 1970-01-01
            • 1970-01-01
            • 2010-09-20
            • 1970-01-01
            • 2013-05-31
            • 1970-01-01
            • 2011-12-30
            相关资源
            最近更新 更多