-3

我有一个 WCF,我需要动态创建此配置,因为我的 app.config 在客户端计算机中永远不会更改。

任何身体帮助?

<behaviors>
  <endpointBehaviors>
    <!-- REST -->
    <behavior name="restBehavior">
      <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped"/>
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="defaultBehavior">
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<client>
  <endpoint name="json" address="http://localhost:8080/json"
               binding="webHttpBinding"
               bindingConfiguration="webBinding"
               behaviorConfiguration="restBehavior"
               contract="ServiceReference.ServiceClientContract" />
</client>
4

1 回答 1

2

Most WCF elements in the config file have a corresponding class or property that can be set in code (which is presumably what you meant by "dynamically"?) For example, 'endpointBehaviors' can be accessed through the Behaviors property of the ServiceEndpoint class:

Uri baseAddress = new Uri("http://localhost:8001/Simple");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(
    typeof(ICalculator),
    new WSHttpBinding(),
    "CalculatorServiceObject");

endpoint.Behaviors.Add(new MyEndpointBehavior());

Console.WriteLine("List all behaviors:");
foreach (IEndpointBehavior behavior in endpoint.Behaviors)
{
    Console.WriteLine("Behavior: {0}", behavior.ToString());
}

http://msdn.microsoft.com/en-us/library/system.servicemodel.description.serviceendpoint.behaviors.aspx

Searching any of the elements your are interested in configuring in MSDN should be enough to get you started.

于 2013-05-29T15:06:14.570 回答