2

Windows サービスでホストされている wcf サービス ライブラリがあります。Service メソッドの呼び出しをインターセプトする必要があります。この場合、このリンクで見られるように、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 コンテナーに登録され、サービス メソッドがインターセプトされるサンプル プロジェクトが必要です。

4

2 に答える 2

1

ユニティ インターセプト パイプラインを活用する必要があります。

Unity は、aop の実装を容易にする組み込みのポリシー注入動作を提供します。ポリシー挿入動作は、メソッドごとに呼び出しハンドラと照合ルールを使用して、特定のメソッドにいくつかの機能をアタッチまたは挿入します。

を。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. あとは、Unity 構成内でインターセプトを構成し、インターセプトするサービス インターフェイス操作に属性を追加するだけです。

>  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)
>>}
于 2012-02-16T07:51:12.770 に答える