如何注册autofac 2 WCF服务合同 [英] How to register two WCF service contracts with autofac

查看:307
本文介绍了如何注册autofac 2 WCF服务合同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现两个服务合同的WCF服务...

I have a WCF service that implements two service contracts...

public class MyService : IService1, IService2

和我自托管服务...

and I am self-hosting the service...

host = new ServiceHost(typeof(MyService));



一切工作正常时,该服务只实现一个服务合同,但是当我试图建立autofac像这样既注册:

Everything was working fine when the service implemented only one service contract, but when I attempt to set up autofac to register both like this:

host.AddDependencyInjectionBehavior<IService1>(_container);
host.AddDependencyInjectionBehavior<IService2>(_container);



...它抛出的第二个例外,报告:

... it throws an exception on the second one, reporting:

的值不能被添加到集合中,作为收集已包含相同类型的项目:Autofac.Integration.Wcf.AutofacDependencyInjectionServiceBehavior'。这个系列只支持每种类型的一个实例。

乍一看我还以为这是说,我的两个合同在某种程度上被视为同一类型,但是,二读,我相信这是说,
AutofacDependencyInjectionServiceBehavior是有问题的类型,例如我不能用它的两倍!

At first glance I thought this was saying my two contracts were somehow being seen as the same type but on second reading I believe it is saying that the AutofacDependencyInjectionServiceBehavior is the type in question, i.e. I cannot use it twice!

然而,我发现的这个帖子是多次使用它在一个稍微不同的形式明确表明:

And yet, I found this post that explicitly showed using it multiple times in a slightly different form:

foreach (var endpoint in host.Description.Endpoints)
{
  var contract = endpoint.Contract;
  Type t = contract.ContractType;
  host.AddDependencyInjectionBehavior(t, container);
}



不幸的是,这给了同样的错误消息。

Unfortunately, that gave the very same error message.

是否可以注册多个服务合同的一个服务,如果是这样,怎么样?

Is it possible to register more than one service contract on one service and, if so, how?

推荐答案

在事实上,你可以与Autofac单个主机注册多个端点。

In fact you can register multiple endpoint for a single host with Autofac.

这是事实,你不能添加多个 AutofacDependencyInjectionServiceBehavior 而是通过所有端点这种行为进行迭代,并在它们注册 ApplyDispatchBehavior 方法:的来源

That is true that you cannot add multiple AutofacDependencyInjectionServiceBehavior but this behaviour iterates through all the endpoints and registers them in the ApplyDispatchBehavior method: source

为了使这项工作,你需要注册您的服务 AsSelf()

In order to make this work you need to register your service AsSelf()

builder.RegisterType<MyService>();



然后就可以正常配置端点:

Then you can configure your endpoint normally:

host = new ServiceHost(typeof(MyService));
host.AddServiceEndpoint(typeof(IService1), binding, string.Empty);
host.AddServiceEndpoint(typeof(IService2), binding, string.Empty);



最后,你需要调用 AddDependencyInjectionBehavior 与sevicehost类型本身:

And finally you need to call the AddDependencyInjectionBehavior with the sevicehost type itself:

host.AddDependencyInjectionBehavior<MyService>(container);

下面是一个小的这表明这种行为示例项目(基于文档)。

Here is a small sample project (based on the documentation) which demonstrates this behavior.

这篇关于如何注册autofac 2 WCF服务合同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆