3

http://megakemp.com/2009/02/06/managing-shared-cookies-in-にあるヘッダー「集中型Cookie管理」で概説されているこの方法でWCFサービス呼び出しを行うときに、共有認証Cookieを管理しています。 wcf /

IClientMessageInspectorカスタム、、、作品IEndpointBehaviorを設定しましたBehaviorExtensionElement。私のエンドポイントの動作は、次のようにメッセージインスペクターを追加します。

public class MyEndpointBehavior : IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        // yuck.. Wish I had an instance of MyClientMessageInspector
        // (which has the auth cookie already) so I could just inject that
        // instance here instead of creating a new instance
        clientRuntime.MessageInspectors.Add(new MyClientMessageInspector());
    }
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }
    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

すべて問題なく動作しますが、複数のクライアント間でCookieを共有する場合、このソリューションは機能しなくなります。このApplyDispatchBehavior()メソッドは新しいインスタンスを作成するため、他のクライアントはそのメッセージインスペクターインスタンス、つまり認証チケットを取得しません。

そこで、次のようにインスタンスを挿入できるカスタムコンストラクターを作成しようと考えました。

MyEndpointBehavior(MyClientMessageInspector msgInspector) { ... }

ただし、WCFにはパラメーターのないコンストラクターが必要です。インターネットを介して除草するWCFには、依存性注入、作成などを可能にするフックがIInstanceProviderありIServiceBehaviorます。しかし、それが私がここで探しているものではないと思います。

誰かが私を正しい方向に導くのを手伝ってもらえますか?

4

3 に答える 3

5

メッセージインスペクターのすべてのインスタンスが同じストレージを共有するように、メッセージインスペクター自体の外部にCookieを保存するように、概念を拡張するだけで済みます。

貧乏人のやり方は、始めたばかりで、インスタンスフィールドの代わりに静的フィールドを使用することです。明らかに、複数のスレッドがある場合は、フィールドの更新中に同時実行性を提供する必要があります。そこから、Cookieコンテナーの概念に外挿して、すべてのクライアントと同じコンテナーを共有していることを確認すると、さらに洗練されたものになります。コンテナの共有はChannelParameterCollection、クライアントチャネルのを取得してプロパティを追加することで実行できます。その後、mssageを検査し、そこからCookieを取得するときに、動作によってそのプロパティが検索されます。これは少し次のようになります。

アプリロジック

// Hold onto a static cookie container
public static CookieContainer MyCookieContainer;

// When instantiating the client add the cookie container to the channel parameters
MyClient client = new MyClient();
client.InnerChannel.GetProperty<ChannelParameterCollection>().Add(MyCookieContainer);

メッセージインスペクターロジック

public void BeforeSendMessage(ref Message, IClientChannel clientChannel)
{
    // Find the cookie container for the current channel
    CookieContainer cookieContainer = clientChannel.GetProperty<ChannelParameterCollection>().Select(p => p as CookieContainer).Where(cc => cc != null).First();

    // ... use the cookie container to set header on outgoing context ...
}
于 2011-05-27T20:56:45.373 に答える
2

正解です。IInstanceProviderはあなたのケースでは役に立ちません。サービスインスタンスを提供するためにのみ使用されます。動作にパラメーターなしのコンストラクターは必要ありません。config要素にはパラメーターなしのコンストラクターが必要です。このクラスは、依存性注入クラス(以下を参照)を使用して、動作に必要な適切なインスペクタークラスを作成できます。

namespace ConsoleApplication4
{
    public class MyEndpointBehavior : IEndpointBehavior
    {
        IClientMessageInspector inspector;
        public MyEndpointBehavior(IClientMessageInspector inspector)
        {
            this.inspector = inspector;
        }

        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            clientRuntime.MessageInspectors.Add(this.inspector);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }
    }
    public class MyEndpointBehaviorElement : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(MyEndpointBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new MyEndpointBehavior(ClientInspectorFactory.GetClientInspector());
        }
    }
    public class MyClientInspector : IClientMessageInspector
    {
        public MyClientInspector()
        {
        }

        public void AfterReceiveReply(ref Message reply, object correlationState)
        {
            Console.WriteLine("AfterReceiveReply");
        }

        public object BeforeSendRequest(ref Message request, IClientChannel channel)
        {
            Console.WriteLine("BeforeSendRequest");
            return null;
        }
    }
    public static class ClientInspectorFactory
    {
        static IClientMessageInspector instance;
        public static IClientMessageInspector GetClientInspector()
        {
            if (instance == null)
            {
                instance = new MyClientInspector();
            }

            return instance;
        }
    }
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        int Add(int x, int y);
    }
    public class Service : ITest
    {
        public int Add(int x, int y) { return x + y; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service));
            host.Open();
            Console.WriteLine("Host opened");

            ChannelFactory<ITest> factory = new ChannelFactory<ITest>("client1");
            ITest proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Add(3, 4));
            ((IClientChannel)proxy).Close();
            factory.Close();

            factory = new ChannelFactory<ITest>("client2");
            proxy = factory.CreateChannel();
            Console.WriteLine(proxy.Add(5, 8));
            ((IClientChannel)proxy).Close();
            factory.Close();

            host.Close();
        }
    }
}
于 2011-05-27T21:12:50.640 に答える
0

@carlosfigueiraと@drewから提供された回答が気に入りましたが、最終的には少し異なるアプローチを思いつきました。IEndpointBehaviorをプログラムで構成することを選択しましたが、構成を使用することはしませんでした。物事をはるかに簡単にしました。次のように、エンドポイントの動作を変更して、クライアントメッセージインスペクターを保存しました。

public class MyEndpointBehavior : IEndpointBehavior
{
    private MyClientMessageInspector_myClientMessageInspector;

    public MyClientMessageInspector MyClientMessageInspector
    {
        get
        {
            if (_myClientMessageInspector == null)
            {
                _myClientMessageInspector = new MyClientMessageInspector();
            }
            return _myClientMessageInspector;
         }
    }
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }
    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        clientRuntime.MessageInspectors.Add(MyClientMessageInspector);
    }
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }
    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

次に、次のように、この動作をクライアント間で共有しました。

var behavior = new MyEndpointBehavior();
client1.Endpoint.Behaviors.Add(behavior);
client2.Endpoint.Behaviors.Add(behavior);

これで、両方のクライアントが同じ認証Cookieを共有します。

于 2011-05-29T03:07:14.280 に答える