简体   繁体   English

以编程方式从配置加载不同的EndpointBehavior

[英]Load different EndpointBehavior from config programmatically

Is it possible to exchange the endpoint behavior of an endpoint defined in the app.config file? 是否可以交换app.config文件中定义的端点的端点行为?

Basically I have a single endpoint with a defined custom binding. 基本上,我有一个具有定义的自定义绑定的端点。 From code I set the endpoint address for the WCF proxy client. 通过代码,我为WCF代理客户端设置了端点地址。 I'd like to use different endpoint behaviors depending on the endpoint address. 我想根据端点地址使用不同的端点行为。

Pseudocode: 伪代码:

var client = new WcfClient("endpointName", new endpointAddress("https://..."));
client.Endpoint.Behaviors.Add(EndpointBehavior.CreateFromConfig("behaviorName"));

Is this (easily) possible? 这(容易)有可能吗? I'd still like to have my behavior definitions in the app.config, but load them dynamically depending on the endpoint's address. 我仍然希望在app.config中有我的行为定义,但是要根据端点的地址动态加载它们。

You can access the configuration via System.ServiceModel.Configuration namespace. 您可以通过System.ServiceModel.Configuration命名空间访问配置。 Read the corresponding sections and construct your endpoint/behaviors manually... 阅读相应的章节并手动构建端点/行为...

You can also create multiple endpoints and instantiate the client by name: http://msdn.microsoft.com/en-us/library/ms751515.aspx 您还可以创建多个终结点并通过名称实例化客户端: http : //msdn.microsoft.com/zh-cn/library/ms751515.aspx

You can also try to use BehaviorExtensionElement from the configuration namespace to try to create a behavior. 您也可以尝试使用配置名称空间中的BehaviorExtensionElement来尝试创建行为。 I found an example here: http://weblogs.asp.net/cibrax/archive/2010/05/11/getting-wcf-bindings-and-behaviors-from-any-config-source.aspx 我在这里找到了一个示例: http : //weblogs.asp.net/cibrax/archive/2010/05/11/getting-wcf-bindings-and-behaviors-from-any-config-source.aspx

For the server for example: If the ServiceHost instance is already open, you can also access most information from it directly 以服务器为例:如果ServiceHost实例已经打开,则还可以直接从其中访问大多数信息。

// BaseAddress
Console.WriteLine(serviceHost.BaseAddress);

// Endpoints (non-MEX)
foreach (ServiceEndpoint ep in serviceHost.Description.Endpoints)
{
  if (serviceHost.BaseAddress.Any(uri => uri.Equals(ep.ListenUri) &&
      ep.Contract.ContractType != typeof(IMetadataExchange))
  {
    Console.WriteLine("ListenURI: " + ep.ListenUri);
    Console.WriteLine("  Name   : " + ep.Name);
    Console.WriteLine("  Binding: " + ep.Binding.GetType().FullName);
  }
}

// List of MEX endpoints:
foreach (ServiceEndpoint ep in serviceHost.Description.Endpoints)
{
  if (ep.Contract.ContractType == typeof(IMetadataExchange))
  {
    Console.WriteLine(ep.ListenUri.ToString());
  }
}

Set the endpoint at runtime: 在运行时设置端点:

yourProxy.ChannelFactory.Endpoint.Address = New ServiceModel.EndpointAddress("someSvcURL") yourProxy.ChannelFactory.Endpoint.Address =新ServiceModel.EndpointAddress(“ someSvcURL”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM