C# 如何以编程方式创建此配置?
我有一个WCF,我需要以友好的方式创建此配置,因为我的 app.config从不在客户端计算机中更改 有人帮忙吗C# 如何以编程方式创建此配置?,c#,wcf,C#,Wcf,我有一个WCF,我需要以友好的方式创建此配置,因为我的 app.config从不在客户端计算机中更改 有人帮忙吗 <behaviors> <endpointBehaviors> <!-- REST --> <behavior name="restBehavior"> <webHttp defaultOutgoingResponseFormat="Json" defaultBodyStyle="Wrapped"/
<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>
配置文件中的大多数WCF元素都有相应的类或属性,可以在代码中设置(这大概就是您所谓的“动态”的意思),例如,“endpointBehaviors”可以通过ServiceEndpoint类的Behaviors属性访问:
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());
}
在MSDN中搜索您感兴趣的配置元素应该足以让您开始。您所说的“动态创建此配置”是什么意思?只是按原样处理数据?请在我的代码中详细说明您希望在什么时候创建此配置。不在app.config中让我了解一下,您想通过代码创建文件,文件的内容是什么?您希望在文件中包含什么?WCF配置包含了几个重要的WCF概念。我建议在采取任何其他步骤之前先阅读此文档。