简体   繁体   English

如何以编程方式配置WCF绑定?

[英]How to configure WCF binding programmatically?

I need to implement following using code: 我需要使用代码实现以下内容:

<basicHttpBinding>

        <binding name="NewBinding">

          <security mode="TransportCredentialOnly">

            <transport clientCredentialType="Basic" />

          </security>

        </binding>

      </basicHttpBinding>

Is there any samples available? 有样品吗? I'm working on WCF REST service and register routes manually. 我正在使用WCF REST服务并手动注册路由。 Placing this config into configuration doesn't work. 将此配置放入配置中不起作用。 I'd like to get it setup programmatically if possible. 如果可能的话,我想以编程方式进行设置。 Also, at what point in code I should do it? 另外,我应该在代码的什么时候这样做?

EDIT: 编辑:

My service Routed in Global.asax like so: 我的服务在Global.asax中路由如下:

foreach (var account in cmc.Accounts.Where(aa => aa.IsActive).ToList())
                {
                    RouteTable.Routes.Add(
                        new ServiceRoute(
                            account.AccountId + "/mobile", new MyServiceHostFactory(), typeof(MobileService)));
                }

And I have my own ServiceHost 我有自己的ServiceHost

public class MyServiceHost : WebServiceHost
    {
        private readonly Type _serviceType;
        private readonly CompositionContainer _container;

        public MyServiceHost(Type serviceType, CompositionContainer container, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
            _serviceType = serviceType;
            _container = container;
        }

        protected override void OnOpening()
        {
            if (Description.Behaviors.Find<MyServiceBehavior>() == null)
            {
                Description.Behaviors.Add(new MyServiceBehavior(_serviceType, _container));
            }

            base.OnOpening();
        }
    }

For this specific case, the equivalent binding is the following: 对于这种特定情况,等效绑定如下:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM