C# 如何以编程方式将wcf绑定部分添加到web.config

C# 如何以编程方式将wcf绑定部分添加到web.config,c#,wcf,configuration,C#,Wcf,Configuration,当以编程方式更新wcf服务的web.config时,可以通过执行以下操作来添加行为 ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel"); ServiceBehaviorElement SerBeh3 = new ServiceBehaviorElement(); SerBeh3.

当以编程方式更新wcf服务的web.config时,可以通过执行以下操作来添加行为

ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");
            ServiceBehaviorElement SerBeh3 = new ServiceBehaviorElement();
            SerBeh3.Name = "AuthenticationSvcWrapBehavior";
            secgroup.Behaviors.ServiceBehaviors.Add(SerBeh3);
我的问题是如何添加绑定部分


我只想创建一个名为Mode和Transport.ClientCredentialType的绑定,然后将BindingConfiguration设置为端点的所述名称

您可以像编辑任何其他XML文件一样对其进行编辑,但我认为在重新启动应用程序之前,这些更改不会生效


就我个人而言,我不再为WCF使用XML配置。对我来说,纯代码解决方案的优点远远大于缺点。

嗯。。我应该加上这句话作为评论,但显然我的大脑已经走到了尽头,我再也找不到评论上的回复链接了:((悲伤的大脑) 总之,我对你的断言感到惊讶,M$不赞成从代码中设置wcf配置,直到我读了链接——我想说他们的意图是配置文件比代码中硬编码的代码配置更容易出错。 当绑定值来自动态软件配置系统时,代码配置比文件配置要好得多。 下面是我创建basit http绑定的代码,可以使用ssl,也可以不使用ssl:

    public static BasicHttpBinding GetBinding(string Url, int timeoutSeconds)
    {
        BasicHttpBinding binding = null;
        UriBuilder urb = new UriBuilder(Url);

        switch (urb.Scheme)
        {
            case "http":
                binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                break;
            case "https":
                binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
                break;
            default:
                throw new ArgumentException("unknown scheme : " + urb.Scheme);
        }
        binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
        binding.MaxBufferPoolSize = int.MaxValue;
        binding.MaxReceivedMessageSize = int.MaxValue;
        binding.MaxBufferSize = int.MaxValue;
        if (timeoutSeconds > 0) 
            binding.SendTimeout = TimeSpan.FromSeconds(timeoutSeconds);

        return binding;

    }
来电者打电话来

   EndpointAddress addr  = new EndpointAddress(url);
   Binding bind =  DataProviderUtilities.GetBinding(_url, timeOutSeconds);
yourserviceClient foo = new yourServiceClient(addr, bind);

我知道了如何在配置中添加绑定部分。也许我很密集,但我认为关于配置更改的文档很糟糕

//Update Service model   for wcf services         
            ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");

            //Add the binding section with the settings that enable HTTPS communications
            secgroup.Bindings.BasicHttpBinding.Bindings.Add(CreateBasicHttpBinding("SecureWebBinding",
                                                                                   BasicHttpSecurityMode.Transport,
                                                                                   HttpClientCredentialType.None));

private BasicHttpBindingElement CreateBasicHttpBinding(string name, BasicHttpSecurityMode mode, HttpClientCredentialType credentialType)
    {
        BasicHttpBindingElement basicHttpBinding = new BasicHttpBindingElement();
        basicHttpBinding.Name = name;
        basicHttpBinding.Security.Mode = BasicHttpSecurityMode.Transport;
        basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
        return basicHttpBinding;
    }

感谢您的反馈…如果您有任何这样做的例子,我很好奇。MSDN直接反驳并说使用代码不是最佳实践。。。