如何注册autofac 2 WCF服务合同

编程入门 行业动态 更新时间:2024-10-24 18:26:33
本文介绍了如何注册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服务合同

本文发布于:2023-11-16 20:43:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:合同   autofac   WCF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!