以声明方式在以编程方式构造的终结点上配置 WCF 行为扩展



我有一个 WCF 行为扩展,我想将其添加到 WCF 客户端。但是,客户端是以编程方式构造的。端点地址可能会有所不同,但我知道类型。我可以通过编程方式或在配置文件中添加行为(首选),但我只需要在配置文件中传递一些配置。

我不希望在常见行为(machine.config)中出现这种情况。

我可以通过编程方式添加行为

endpoint.Behaviors.Add(new MyCustomBehavior())

但我宁愿在配置中执行此操作,因此我也可以在那里配置扩展。

是否可以以声明方式将终结点

行为扩展添加和配置到以编程方式构造的终结点,只知道类型或接口,同时保留客户端终结点以编程方式构造?

<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 工具。

为此,

您需要为终端节点创建行为配置扩展。有关如何执行此操作的详细信息,请查看 https://learn.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>

最新更新