2012-02-15 154 views
2

我有一個在windows服務中託管的wcf服務庫。我需要攔截撥打 服務方式的電話。對於這種情況,建議到WCF註冊到Unity容器,可以在這個環節可以看出在Unity容器中註冊WCF服務

http://weblogs.asp.net/fabio/archive/2009/03/24/inversion-of-control-with-wcf-and-unity.aspx

我想實現從Codeplex.I Unity.WCF裝配了類似的做法無法理解在哪裏把我的容器配置或引導程序放在wcf服務庫(或windows服務)中。沒有提供可靠的樣本(vs解決方案)。

我的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)] 公共接口ISecurityRepository {

[OperationContract(IsInitiating = true)] 
    IList<vNavigationTree> GetNavigationTree(string ticket); 

    [OperationContract(IsInitiating = true)] 
    string GetSessionGuid(string userName, string IP, string machineName); 
} 

在這種情況下,它似乎攔截不work.Briefly什麼我需要的是一個示例項目,其中WCF服務已註冊到DI容器,並且服務方法被攔截。

回答

2

我會嘗試解釋我試圖做得更明確以及我如何設法做到這一點。我有一個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

1

你需要做的是利用統一攔截管道。

Unity提供了一個內置的策略注入行爲,以方便執行aop。策略注入行爲通過在每個方法的基礎上使用調用處理程序和匹配規則來爲特定方法附加或注入某些功能。

a。從ICallhandler的自定義界面開始。

>> public interface ILogAttributeHandler : ICallHandler 
>> { 
>> } 
>> 

b。爲您的處理程序添加實現。這是您的方法被攔截時想要應用的建議。

>> public class ActivityAttributeHandler : ILogAttributeHandler 
>> { 
>> public ActivityAttributeHandler(string activityType) 
>> { 
>> ActivityType = activityType; 
>> } 

>> private string ActivityType { get; set; } 
>> public int Order { get; set; } 

>> public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) 
>> { 
>>   //// Invoke the handler 
>>   IMethodReturn output = getNext()(input, getNext); 

>>   //// Perform post-processing task 
>>   var agent = output.ReturnValue as Agent; 

>>   if (agent != null) 
>>   { 
>>    //// do work here 
>>   } 

>>   return getNext()(input, getNext); 
>>  } 
} 

c。創建您的自定義屬性,這將用作程序中的切入點。

>> [AttributeUsage(AttributeTargets.Method)] 
>> public class ActivityAttribute : HandlerAttribute 
>> { 
>>  private readonly string _activityName; 

>>  public ActivityAttribute(string activityName) 
>>  { 
>>   _activityName = activityName; 
>>  } 
>> } 
>>  public override ICallHandler CreateHandler(IUnityContainer container) 
>>  { 
>> return null; 
>>} 

d。現在,您只剩下在您的統一配置中配置攔截,並將該屬性添加到您想要攔截的服務接口操作中。

> container 
>     .RegisterType<ILogAttributeHandler, LogAttributeHandler>() 
>     .AddNewExtension<Interception>() 
>     .Configure<Interception>() 
>    .SetInterceptorFor<ISecurityRepository>("SecurityRepository", new 
> InterfaceInterceptor()); 

e。將屬性應用於您的界面操作

>>public interface ISecurityRepository 
>> { 
>> [OperationContract(IsInitiating = true)] 
>> [Activity("Logon")] 
>> IList<vNavigationTree> GetNavigationTree(string ticket) 
>>}