通过 web.config 进行 WCF 服务的 Unity 依赖注入
我有一个正在试验 DI 的项目。我正在使用 Unity,对于正常的装配和注入来说,一切似乎运行良好。
我正在尝试进一步打破与 WCF 服务的依赖关系。我想要注入的 WCF 服务当前是在运行时创建的,没有使用 DI,并且我不使用 VS .net 生成的代理:
MyService = new ChannelFactory<IMyService>("BasicHttpBinding_IMyService").CreateChannel();
上述端点位于 web.config 中:
<endpoint address="http://localhost:35806/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="Interfaces.IMyService" name="BasicHttpBinding_IMyService" />
我正在尝试找出如何映射它通过 web.config 向接口提供 WCF 服务,以便我可以使用构造函数注入
在 web.config 中,使用“mapTo”进行正常映射,您可以在其中指定接口别名和先前定义的类的别名。
<type type="IMyService" mapTo="MyService">
<lifetime type="singleton"/>
</type>
由于 WCF 服务代理是在运行时动态创建的,因此我没有对“MyService”类的引用,而是需要从服务的“BasicHttpBinding_IMyService”端点中提取。
关于如何实现这一点有什么想法吗?
I have a project that I am experimenting with DI in. I am using Unity and things seem to work well for normal assemblies and the injections.
I am trying to further break dependencies with WCF services. The WCF service that I want to inject is created at runtime currently without using DI and I don't use the VS .net generated proxies:
MyService = new ChannelFactory<IMyService>("BasicHttpBinding_IMyService").CreateChannel();
Endpoint for the above is in the web.config:
<endpoint address="http://localhost:35806/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="Interfaces.IMyService" name="BasicHttpBinding_IMyService" />
I am trying to figure out how to map this WCF service to an interface via the web.config so that I can use constructor injection
In web.config the normal mapping occurs using the "mapTo" where you would specify the interface alias and an alias to a class you previously defined.
<type type="IMyService" mapTo="MyService">
<lifetime type="singleton"/>
</type>
Since the WCF service proxy is created dynamically at run time I do not have this reference to "MyService" class instead it needs to pull from the "BasicHttpBinding_IMyService"
endpoint for the service.
Any ideas on how this can be accomplished?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从配置文件中看到此工作的唯一方法是创建一个实现 IMyService 的 MyService 类,并在幕后创建自己的 Channel(使用 ChannelFactory 代码片段)并本质上充当代理。
但为什么不直接调用
Unity 容器并传入已创建的 MyService 通道实例呢?
The only way I see this working from the config file is to create a MyService class that implements IMyService - and behind the scenes it creates its own Channel (using the ChannelFactory code snippet) and essentially acts as a proxy.
But instead of that, why not just call
on your unity container and pass in an already created MyService channel instance?
我去年编写了一组 Unity 扩展来实现这一目标。您可以从 http://neovolve.codeplex.com/releases/view/19004 有这种支持。
查看该工具包的 chm 文件。 Neovolve.Toolkit.Unity.ProxyParameterValueElement 的文档将描述如何使用此工具包实现您想要的目标。
I wrote a set of Unity extensions last year that achieve exactly this. You can download my toolkit from http://neovolve.codeplex.com/releases/view/19004 which has this support.
Have a look at the chm file for the toolkit. The documentation for the Neovolve.Toolkit.Unity.ProxyParameterValueElement will describe how to achieve what you want with this toolkit.