c# - 如何以编程方式将 wcf 绑定(bind)部分添加到 web.config

标签 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);

我的问题是如何添加绑定(bind)部分?

我想要做的就是创建一个具有名称、模式和 Transport.ClientCredentialType 的绑定(bind),然后将 BindingConfiguration 设置为端点的所述名称。

最佳答案

我想出了如何在配置中添加绑定(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;
    }

关于c# - 如何以编程方式将 wcf 绑定(bind)部分添加到 web.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4249277/

相关文章:

c# - NHibernate 获取全部

c# - 检测wcf服务是否激活

spring-boot - 在 Kotlin 中迁移到版本 3+ 时,Spring Boot 无法再绑定(bind)配置属性

java - 在 Java Web 应用程序中使用可配置属性

c# - 可观察对象/动态接口(interface)?

c# - 从 Outlook 收件箱读取邮件时,基本身份验证不再有效

c# - Monitor.TryEnter/Monitor.Exit 和 SynchronizationLockException

wcf - 注入(inject) WCF REST 服务

c# - 聚合异常 "One or more errors occurred.An error occurred while sending the request."

c# - 有状态服务的依赖注入(inject)