简体   繁体   中英

One WCF service on two addresses

My situation:

I have one WCF service on address http://0.0.0.0:10004/Service.svc . Using Interface_1 , Service_1_Class , ServiceHost_1_Class .

Now I need second version of this service with authorisation. Choosing address https://0.0.0.0:10004/Service2.svc , using Interface_1 , Service_1_Class , ServiceHost_2_Class .

Got the error HTTP could not register URL https://+:10004/Service2.svc/ . Another application has already registered this URL with HTTP.SYS.

If copy-paste and use Interface_2 , Service_2_Class , ServiceHost_2_Class (same classes with different name) everything works.

Not the big problem, but want to know: how to bind same service class on two addresses? So which step may I miss? Ie binding one to one works, binding one to two - not, where is difference? If there should not be differences (ie problem in code), I'll close this question.

PS: sorry, cant post code: too big, too complicated, not clear. Question not about code, but conception

As suggested, you need to setup multiple service endpoints each with a different binding. One binding with no security enabled and the other binding with security enabled.

Here is a working example of how to host the same service at two different endpoints. One endpoint has no security at all and the other endpoint provides HTTP-based client authentication.

var baseUri = new Uri("http://localhost:10004/");
var serviceHost = new ServiceHost(typeof(HelloWorldService), baseUri);
try
{
    var unsecureServiceEndpoint = serviceHost.AddServiceEndpoint(typeof(IHelloWorldService), new WebHttpBinding(), "Service.svc");
    unsecureServiceEndpoint.Behaviors.Add(new WebHttpBehavior());
    unsecureServiceEndpoint.Name = "UnsecureEndpoint";

    var secureBinding = new WebHttpBinding(WebHttpSecurityMode.TransportCredentialOnly);
    secureBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
    var secureServiceEndpoint = serviceHost.AddServiceEndpoint(typeof(IHelloWorldService), secureBinding, "Service2.svc");
    secureServiceEndpoint.Behaviors.Add(new WebHttpBehavior());
    secureServiceEndpoint.Name = "SecureEndpoint";

    serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
    serviceHost.Open();

    Console.WriteLine("Hosting - {0} @ {1}", unsecureServiceEndpoint.Name, unsecureServiceEndpoint.Address);
    Console.WriteLine("Hosting - {0} @ {1}", secureServiceEndpoint.Name, secureServiceEndpoint.Address);

    Console.WriteLine("Press <Enter> to stop the services.");
    Console.ReadLine();

    serviceHost.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("An exception occurred: {0}", ce.Message);
    serviceHost.Abort();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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