【问题标题】:How do you add wcf bindings section to web.config programmatically如何以编程方式将 wcf 绑定部分添加到 web.config
【发布时间】:2011-05-14 01:15:21
【问题描述】:

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

ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)_webConfig.GetSectionGroup("system.serviceModel");
            ServiceBehaviorElement SerBeh3 = new ServiceBehaviorElement();
            SerBeh3.Name = "AuthenticationSvcWrapBehavior";
            secgroup.Behaviors.ServiceBehaviors.Add(SerBeh3);

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

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

【问题讨论】:

    标签: c# wcf configuration


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      我个人不再为 WCF 使用 XML 配置。对我来说,纯代码解决方案的优点远远超过了缺点。

      【讨论】:

      【解决方案3】:

      嗯..我应该将此添加为评论,但显然我的大脑刚刚走入深渊,我无法再在 cmets 上找到回复链接:((悲伤的大脑) Anyhoo,我很惊讶您断言 M$ 不赞成从代码中设置 wcf 配置,直到我阅读了链接——我想说他们的意图是配置文件比硬编码的代码配置更可取 在代码中。 当您的绑定值来自动态 sw 配置系统时,代码配置比文件配置要好得多。 这是我的代码来创建一个基本的 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-05
        相关资源
        最近更新 更多