2012-02-15 4 views
2

Windows 서비스에서 호스팅되는 wcf 서비스 라이브러리가 있습니다. 서비스 방법에 대한 호출을 가로 챌 필요가 있습니다. 이 경우의 경우는이 링크에서 볼 수있는 바와 같이 유니티 용기에 WCF를 등록하는 것이 좋습니다 나는 곳으로 이해할 수 없었다 Codeplex.I에서 Unity.WCF 어셈블리에 의해 유사한 접근 방식을 구현하기 위해 노력하고Unity Container에서 WCF 서비스 등록

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

내 컨테이너 구성이나 부트 스트 래퍼를 wcf 서비스 라이브러리 (또는 Windows 서비스)에 넣으십시오. 제공된 견고한 샘플 (솔루션 비교)이 없습니다.

내 Windows 서비스의 호스트

개인 UnityServiceHost _serviceHost = NULL; 개인 읽기 전용 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

나는 더 명확하게하려고 시도한 것과 내가 그것을 어떻게 처리했는지 설명하려고 노력할 것입니다. WCF를 통해 데이터베이스와 통신하는 WPF 응용 프로그램이 있습니다. 이는 내 응용 프로그램이 클라이언트 측과 서버 측 (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. 커스텀 속성을 생성하십시오. 이것은 프로그램의 포인트 컷 (pointcut)으로 사용됩니다.

>> [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) 
>>} 
관련 문제