【问题标题】:Adding IoC Support to my WCF service hosted in a windows service (Autofac)将 IoC 支持添加到我在 Windows 服务 (Autofac) 中托管的 WCF 服务
【发布时间】:2010-05-07 08:06:04
【问题描述】:

我想设置我的 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 类型,我不知道从哪里可以得到它。有什么想法吗?

提前致谢。

【问题讨论】:

    标签: wcf windows-services inversion-of-control autofac


    【解决方案1】:

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

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

    这应该足以为您指明正确的方向。我将更新 Autofac wiki 上的文档以包含相同的示例。

    【讨论】:

    • 看起来很棒!!我明天试试。非常感谢!!
    【解决方案2】:

    自 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

    【讨论】:

      猜你喜欢
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      相关资源
      最近更新 更多