0

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 = container;
        }

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

            base.OnOpening();
        }
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
katit
  • 17,375
  • 35
  • 128
  • 256
  • Take a look @ http://stackoverflow.com/questions/835613/wcf-how-can-i-programatically-recreate-these-app-config-values – Ben.Vineyard Aug 18 '11 at 22:22
  • That post doesn't describe how to use that code when hosted in ASP.NET. I have custom ServiceHost and ServiceHostProvider. Ideally it needs to go there – katit Aug 19 '11 at 16:17

1 Answers1

1

For this specific case, the equivalent binding is the following:

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Carlos, how do I insert this binding into my service? – katit Aug 19 '11 at 18:30
  • You can use the method `ServiceHost.AddServiceEndpoint` which takes the service contract type (usually an interface), the binding and the address. If you want to use routes, you'll need a `ServiceHostFactory` to be able to get a reference to the host. You can find more about the factory at http://blogs.msdn.com/b/carlosfigueira/archive/2011/06/14/wcf-extensibility-servicehostfactory.aspx – carlosfigueira Aug 19 '11 at 19:59
  • I added EDIT in my post. WIth this kind of setup, how do I add this binding? Where in my code? – katit Aug 19 '11 at 20:47
  • WebServiceHost, which you're using, is for REST services; BasicHttpBinding, which is what the config you mentioned has, is for SOAP services / endpoints. Which one do you need? – carlosfigueira Aug 19 '11 at 23:51
  • I need to somehow configure Basic Auth for REST services. If it's even possible.. Not sure how to do it, that config is the only thing I found. Right now I'm manually parsing Http header on each r4equest inside web service class – katit Aug 20 '11 at 00:31