8

WCF クライアントに追加したい WCF 動作拡張があります。ただし、クライアントはプログラムによって構築されます。エンドポイント アドレスは異なる場合がありますが、タイプはわかっています。プログラムまたは構成ファイル (推奨) で動作を追加できますが、構成ファイルでのみ構成を渡す必要があります。

これを一般的な動作 (machine.config) で使用したくありません。

プログラムで動作を追加できます

endpoint.Behaviors.Add(new MyCustomBehavior())

しかし、私は config でそれを行いたいので、そこで拡張機能を構成することもできます。

クライアントエンドポイントをプログラムで構築したままにして、型またはインターフェイスのみを知っているプログラムで構築されたエンドポイントにエンドポイントの動作拡張を宣言的に追加して構成することは可能ですか?

<system.serviceModel>
  <client>
    <!-- Created programmatically -->
  </client>
<extensions>
  <behaviorExtensions>
    <add name="MyCustomBehavior" type="namespace.CustomBehaviors", MyAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" />
  </behaviorExtensions>   
</extensions>
  <behaviors>
    <endpointBehaviors>
      <behavior name="MyCustomBehavior">
        <MyCustomBehavior MyImportantBehaviorParam1="foo"  />
      </behavior>
    </endpointBehaviors>   
  </behaviors>
</system.serviceModel>

もちろん、構成を別のセクションに配置して、そこで動作を読み取らせることもできますが、可能であれば WCF 機能を使用したいと思います。

4

1 に答える 1

12

これを行うには、エンドポイントの動作構成拡張を作成する必要があります。その方法の詳細については、https://docs.microsoft.com/en-us/archive/blogs/carlosfigueira/wcf-extensibility-behavior-configuration-extensionsを確認してください。

更新: 問題が発生しました。コードを介して作成されたエンドポイントに、構成で宣言された動作を直接追加する方法はありません。それでも実行できますが、リフレクションを使用しCreateBehaviorて動作構成拡張機能のメソッドにアクセスし (メソッドは保護されています)、エンドポイントの動作を実際に作成して、コードで作成されたエンドポイントに追加する必要があります。以下のコードは、これを行う方法を示しています。

public class StackOverflow_10232385
{
    public class MyCustomBehavior : IEndpointBehavior
    {
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
            Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
            Console.WriteLine("In {0}.{1}", this.GetType().Name, MethodBase.GetCurrentMethod().Name);
        }
    }

    public class MyCustomBehaviorExtension : BehaviorExtensionElement
    {
        public override Type BehaviorType
        {
            get { return typeof(MyCustomBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new MyCustomBehavior();
        }
    }

    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }
    }

    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");

        var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ServiceModelSectionGroup smsg = configuration.GetSectionGroup("system.serviceModel") as ServiceModelSectionGroup;
        EndpointBehaviorElement endpointBehaviorElement = smsg.Behaviors.EndpointBehaviors["MyCustomBehavior_10232385"];
        foreach (BehaviorExtensionElement behaviorElement in endpointBehaviorElement)
        {
            MethodInfo createBehaviorMethod = behaviorElement.GetType().GetMethod("CreateBehavior", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, Type.EmptyTypes, null);
            IEndpointBehavior behavior = createBehaviorMethod.Invoke(behaviorElement, new object[0]) as IEndpointBehavior;
            endpoint.Behaviors.Add(behavior);
        }

        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

そして、このコードの構成:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="myCustomBehavior_10232385" type="QuickCode1.StackOverflow_10232385+MyCustomBehaviorExtension, QuickCode1"/>
        </behaviorExtensions>
    </extensions>
    <behaviors>
        <endpointBehaviors>
            <behavior name="MyCustomBehavior_10232385">
                <myCustomBehavior_10232385/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>
于 2012-04-19T17:28:28.667 に答える