我有一个托管在 Windows 服务中的 wcf 服务库。我需要拦截对服务方法的调用。对于这种情况,建议将 WCF 注册到 Unity 容器中,如此链接所示
http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx
我正在尝试通过来自 Codeplex 的 Unity.WCF 程序集实现类似的方法。我无法理解将容器配置或引导程序放在 wcf 服务库(或 Windows 服务)中的什么位置。没有提供固体样品(相对于溶液)。
我的 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;
}
}
我的服务合同
[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 容器并拦截了服务方法。