首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有自定义构造函数的ServiceBehavior。需要注入依赖项

具有自定义构造函数的ServiceBehavior。需要注入依赖项
EN

Stack Overflow用户
提问于 2013-04-25 03:08:22
回答 1查看 590关注 0票数 1

我有一个这样实现的自定义ServiceBehavior,作为装饰WCF服务的属性:

代码语言:javascript
复制
public sealed class NHibernateSessionServiceBehavior : Attribute, IServiceBehavior
{
    public Type ServiceType { get; set; }
    public IConfigurator Configurator { get; set; }

    public NHibernateSessionServiceBehavior(Type serviceType, IConfigurator configurator)
    {
        ServiceType = serviceType;
        Configurator = configurator;
    }

    #region Implementation of IServiceBehavior

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

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {
        NHibernateSessionHelper.Instance.InitializeSessionFactory(Configurator);
    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channelDispatch in serviceHostBase.ChannelDispatchers)
        {
            foreach (EndpointDispatcher endpointDispatch in channelDispatch.Endpoints)
            {
                endpointDispatch.DispatchRuntime.MessageInspectors.Add(new NHibernateMessageInspector(ServiceType));
            }
        }
    }

    #endregion
}

我遇到的问题是,我需要向属性构造函数传递IConfigurator的一个实例,但编译器显然不允许我这样做。我需要一种方法来将此行为添加到注入IConfigurator实例的服务中。你知道该怎么做吗?

EN

回答 1

Stack Overflow用户

发布于 2013-04-25 13:57:52

看看这篇文章:Using Instance Provider and ServiceHostFactory to Construct the Service

本系列还可能对您有所帮助:

http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity-part-ii.aspx

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16200244

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档