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

时间:2010-11-22 19:13:53

标签: c# wcf configuration

当以编程方式更新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设置为端点的所述名称。

3 个答案:

答案 0 :(得分:2)

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

//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;
    }

答案 1 :(得分:0)

您可以像编辑任何其他XML文件一样对其进行编辑,但我认为更改只有在您重新启动应用程序后才会生效。

我个人不再使用WCF的XML配置了。纯代码解决方案的优点大大超过了我的缺点。

答案 2 :(得分:0)

嗯..我应该把它添加为评论,但显然我的大脑刚刚走出了深层,我无法再找到评论的回复链接:((悲伤的大脑) Anyhoo,我很惊讶你断言M $不赞成从代码设置wcf配置,直到我读到链接 - 我会说他们的意图是配置文件优先于代码配置这是硬编码的在代码中。 当您的绑定值来自动态sw配置系统时,代码配置比文件配置更好。 继承我的代码来创建一个basit http绑定,无论是否有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);