开发者

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. 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:

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

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 = co开发者_高级运维ntainer;
        }

        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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜