我有一个托管在 Windows 服务中的 WCF 服务库。我需要拦截来电 服务方式。对于这种情况,建议将 WCF 注册到 Unity 容器中,如本链接所示
http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx
我正在尝试通过 Codeplex 中的 Unity.WCF 程序集实现类似的方法。我无法理解将容器配置或 Bootstrap 放在 wcf 服务库(或 Windows 服务)中的位置。没有提供固体 sample (与溶液)。
我的 Windows 服务主机
private UnityServiceHost _serviceHost = null;
private readonly UnityContainer _container;
public Service() {
InitializeComponent();
_container = new UnityContainer();
_container.AddNewExtension<Interception>();
_container.RegisterType<ISecurityRepository, SecurityRepository>();
_container.Configure<Interception>().SetDefaultInterceptorFor<ISecurityRepository>(new TransparentProxyInterceptor());
}
protected override void OnStart(string[] args) {
//SecurityService
if (_serviceHost != null) {
_serviceHost.Close();
} else {
_serviceHost = new UnityServiceHost(_container, typeof(SecurityRepository));
_serviceHost.Open();
}
}
protected override void OnStop() {
//SecurityService
if (_serviceHost != null) {
_serviceHost.Close();
_serviceHost = null;
}
}
我的服务契约(Contract)
[ServiceContract(SessionMode = SessionMode.Required)]
public interface ISecurityRepository
{
[OperationContract(IsInitiating = true)]
IList<vNavigationTree> GetNavigationTree(string ticket);
[OperationContract(IsInitiating = true)]
string GetSessionGuid(string userName, string IP, string machineName);
}
在这种情况下,拦截器似乎不起作用。简而言之,我需要一个示例项目,其中 WCF 服务注册到 DI 容器并拦截服务方法。
最佳答案
我将尝试更明确地解释我尝试做的事情以及我如何做到这一点。我有一个 WPF 应用程序通过 WCF 与数据库通信,这意味着我的应用程序大致分为两部分:客户端和服务器端 (WCF)。我通过 PRISM 提供的 UnityBootStrapper 将客户端包装到 Unity 容器中。我还需要将服务器端包装到另一个 Unity 容器中,以使 Unity 解决服务器端依赖关系。
我的问题已通过 Unity.WCF 解决(以 Nuget 包的形式提供),它提供了可以用来代替 ServiceHost 的 UnityServiceHost 类。我猜这个包是按照这篇文章解释的方式创建的:
http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx
关于wcf - 在Unity容器中注册WCF服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9293553/