Unity.wcf 和 InstanceContextMode.Single [英] Unity.wcf and InstanceContextMode.Single

查看:38
本文介绍了Unity.wcf 和 InstanceContextMode.Single的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Unity.WCF 为 WCF 服务注入依赖项.当我将服务设置为 InstanceContextMode.Single 时会出现问题.

I am using Unity.WCF to inject dependencies for WCF service. Problem occurs when I set my service to InstanceContextMode.Single.

我在 Google 上发现,当 InstanceContextMode 设置为 Single 时,不会调用 InstanceProvider.还有一个解决方法,但我想知道 Unity.WCF 中是否有一些内置支持,因为显然这是一个众所周知的问题.

I found on Google that when InstanceContextMode is set to Single, InstanceProvider is not called. There is also a workaround for this but I was wondering if there is some built-in support for this in Unity.WCF because apparently this is a well known problem.

我在这里找到了信息:启用InstanceProvider 用于单例服务.

I found the information here: Enabling InstanceProvider for singleton services.

推荐答案

我会引用 Paul Hiles 对您提出的同一问题发表评论:

使用 InstanceContextMode.Single 会使您的服务扩展性非常差,因此在大多数情况下最好避免使用,特别是当它仅用于允许 AppFabric 自动启动时.您可以安全地删除 ServiceBehavior 属性并以另一种方式进行操作.

Using InstanceContextMode.Single makes your service scale very badly so is best avoided in most cases, particularly if it is just being used to allow AppFabric auto-start. You can safely remove the ServiceBehavior attribute and do it another way.

使用 Unity.WCF,您可以将初始化代码添加到添加 Unity.WCF NuGet 包时创建的 WcfServiceFactory 类的 ConfigureContainer 方法中.这只会在服务的生命周期内执行一次.

With Unity.WCF, you can add your initialisation code to the ConfigureContainer method of the WcfServiceFactory class that is created when you add the Unity.WCF NuGet package. This will only be executed once for the lifetime of the service.

顺便说一句,您不应该将 Unity 容器传递到您的服务中.将您的服务使用的任何组件添加到构造函数中(例如存储库、帮助程序等),然后使用 ConfigureContainer 方法向 Unity 注册.当您的服务被实例化时,依赖项将被自动注入.

BTW, you should not be passing the Unity container into your service. Add any components that your service uses into the constructor (e.g. repositories, helpers etc.) and also register then with Unity using the ConfigureContainer method. When your service is instantiated, the dependencies will be injected automatically.

您还可以找到文章 来自 这个 MSDN 系列 有用.

You may also find article from this MSDN series useful.

这篇关于Unity.wcf 和 InstanceContextMode.Single的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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