2011-10-04 3 views
0

내 서비스 레이어를 내 도메인 모델 레이어의 정면으로 사용하여 도메인 개체에 대한 호출을 조정합니다. 인스턴스 생성을 위해 MEF를 활용하기 위해 WCF 서비스에 대한 커스텀 인스턴스 공급자를 작성했습니다. 이제 감사 및 로깅을 위해 PIAB를 적용해야합니다. 내가 어떻게 할 수 있니?MEF와 함께 WCF에서 PIAB를 적용하는 방법은 무엇입니까?

답변

0

지미 톤 너는 MEF와 PIAB를 혼합하는 것에 대한 좋은 블로그를 작성했습니다 (http://blogs.msdn.com/b/jimmytr/archive/2010/06/22/mixing-mef-and-piab.aspx 방문). 그것은 PIAB를 MEFied WCF 서비스에 적용하는 내 솔루션에 영감을주었습니다. 아이디어는 간단합니다. MEF를 사용하여 모든 서비스 구성을 먼저 관리하십시오. 그런 다음 사용자 지정 인스턴스 공급자에서 MEF 컨테이너로 서비스 인스턴스를 찾은 다음 PolicyInjection.Wrap을 적용합니다.

서비스 :

[Export(typeof(ICustomerService))] 
public class CustomerService : ICustomerService 
{ 
    #region ICustomerService Members 
    public Customer GetCustomer(string customerID) 
    { 
     return CustomerDAO.GetCustomer(customerID); 
    } 
    #endregion 
} 

사용자 인스턴스 제공자 :

public class PolicyInjectionInstanceProvider : IInstanceProvider 
{ 
    private Type serviceContractType; 

    private CompositionContainer Container { get; set; } 

    public PolicyInjectionInstanceProvider(Type t) 
    { 

     if (t!= null && !t.IsInterface) 
     { 
      throw new ArgumentException("Specified Type must be an interface"); 
     } 

     this.serviceContractType = t; 

    } 

    #region IInstanceProvider Members 

    public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message) 
    { 
     Type type = instanceContext.Host.Description.ServiceType; 

     if (serviceContractType != null) 
     { 
      Compose(); 
      var importDefinition = new ImportDefinition(i => i.ContractName.Equals(serviceContractType.FullName), serviceContractType.FullName, ImportCardinality.ZeroOrMore, false, false); 
      var atomicComposition = new AtomicComposition(); 
      IEnumerable<Export> extensions; 

      Container.TryGetExports(importDefinition, atomicComposition, out extensions); 

      if (extensions != null && extensions.Count() > 0) 
      { 
       var service = extensions.First().Value; 
       return PolicyInjection.Wrap(serviceContractType, service); 
      }     
     } 
     else 
     { 
      if (!type.IsMarshalByRef) 
      { 
       throw new ArgumentException("Type Must inherit MarshalByRefObject if no ServiceInterface is Specified"); 
      } 
      return PolicyInjection.Create(type); 
     } 
     return null; 
    } 

    public object GetInstance(InstanceContext instanceContext) 
    { 
     return GetInstance(instanceContext, null); 
    } 

    public void ReleaseInstance(InstanceContext instanceContext, object instance) 
    { 
     var disposable = instance as IDisposable; 
     if (disposable != null) 
     { 
      disposable.Dispose(); 
     } 
     Container.Dispose(); 
    } 

    #endregion 

    #region Private Methods 

    private void Compose() 
    { 
     var catalog = new AggregateCatalog(); 
     catalog.Catalogs.Add(new DirectoryCatalog(@".\")); //Extensions 
     Container = new CompositionContainer(catalog); 
    } 

    #endregion 
} 
다음 코드 샘플입니다
관련 문제