代码之家  ›  专栏  ›  技术社区  ›  user137348

向托管在windows服务(autofac)中的wcf服务添加ioc支持

  •  6
  • user137348  · 技术社区  · 16 年前

    我想将wcf服务设置为使用ioc容器。autofac wiki中有一篇关于wcf集成的文章,但它只显示了与iis中托管的服务的集成。

    但我的服务是在windows服务中托管的。

    这里我有个建议来安排开幕式 http://groups.google.com/group/autofac/browse_thread/thread/23eb7ff07d8bfa03

    我听从了建议,到目前为止我得到的是:

        private void RunService<T>()
        {
            var builder = new ContainerBuilder();
    
            builder.Register(c => new DataAccessAdapter("1")).As<IDataAccessAdapter>();
    
            ServiceHost serviceHost = new ServiceHost(typeof(T));
    
            serviceHost.Opening += (sender, args) => serviceHost.Description.Behaviors.Add(
                new AutofacDependencyInjectionServiceBehavior(builder.Build(), typeof(T), ??? ));                      
    
    
            serviceHost.Open();
         }
    

    AutoFacDependencyInjectionServiceBehavior有一个接受3个参数的ctor。第三个是 IComponentRegistration 我也不知道从哪儿弄来的。有什么想法吗?

    提前谢谢。

    2 回复  |  直到 8 年前
        1
  •  6
  •   JJS Joel Coehoorn    9 年前

    我写了一篇博客文章,描述了如何在自托管wcf服务时使用autofac wcf集成。

    http://alexmg.com/self-hosting-wcf-services-with-the-autofac-wcf-integration/

    这应该足以指引你正确的方向。我将更新autofac wiki上的文档以包含相同的示例。

        2
  •  0
  •   Rafael P. Miranda    8 年前

    自alex-meyer反应以来,autofac发生了一些变化。基本上是一行代码:

    //Instead of
    host.Description.Behaviors.Add(new AutofacDependencyInjectionServiceBehavior(container, typeof(EchoService), registration));
    //Use this
    host.AddDependencyInjectionBehavior<IEchoService>(container);
    

    来源: https://autofaccn.readthedocs.io/en/latest/integration/wcf.html#self-hosting