6

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

提前致谢。

4

2 回答 2

6

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

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

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

于 2010-05-07T13:30:00.997 回答
0

自 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

于 2018-06-19T17:03:55.647 回答