A Windows Communication Foundation (WCF) service implements the following contract.
[ServiceContract]
public interface IHelloService
{
[OperationContract(WebGet(UriTemplate=”hello?name={name}”))]
string SayHello(string name);
}
The implementation is as follows:
public class HelloService: IHelloService
{
public string SayHello(string name)
{
return “Hello ” + name;
}
}
The service is self-hosted, and the hosting code is as follows:
WebServiceHost svcHost = CreateHost();
svcHost.Open();
Console.ReadLine();
svcHost.Close();
You need to implement CreateHost so that the service has a single endpoint hosted at http://localhost:8000/HelloService.
Which code segment should you use?
A.
WebServiceHost svcHost = new WebServiceHost(typeof(HelloService));
svcHost.AddServiceEndpoint(typeof(IHelloService),
new WebHttpBinding(WebHttpSecurityMode.None),
“http://localhost:8000/HelloService”);
return svcHost;
B.
Uri baseAddress = new Uri(“http://localhost:8000”);
WebServiceHost svcHost = new WebServiceHost(typeof(HelloService), baseAddress);
svcHost.AddServiceEndpoint(typeof(IHelloService),
new WebHttpBinding(WebHttpSecurityMode.None),
“HelloService”);
return svcHost;
C.
WebServiceHost svcHost = new WebServiceHost(new HelloService());
svcHost.AddServiceEndpoint(typeof(IHelloService),
new WebHttpBinding(WebHttpSecurityMode.None),
“http://localhost:8000/HelloService”);
retumn svcHost
D.
Uri baseAddress = new Uri(“http://localhost:8000/”);
WebServiceHost svcHost = new WebServiceHost(new HelloService(), baseAddress);
svcHost.AddServiceEndpoint(typeof(IHelloService),
new WebHttpBinding(WebHttpSecurityMode.None),
“HelloService”);
retumn svcHost;
Explanation:
WebServiceHost Class
(http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost.aspx)Name
DescriptionWebServiceHost()
Initializes a new instance of the WebServiceHost class.WebServiceHost(Object, Uri[])
Initializes a new instance of the WebServiceHost class with the specified singleton server instance and base address.WebServiceHost(Type, Uri[])
Initializes a new instance of the WebServiceHost class with the specified service type and base address.WebServiceHost.AddServiceEndpoint() Method :
Name
DescriptionAddServiceEndpoint(ServiceEndpoint)
Adds the specified service endpoint to the hosted service. (Inherited from ServiceHostBase.)AddServiceEndpoint(String, Binding, String)
Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address. (Inherited from ServiceHostBase.)AddServiceEndpoint(String, Binding, Uri)
Adds a service endpoint to the hosted service with a specified contract, binding, and a URI that contains the endpoint address. (Inherited from ServiceHostBase.)AddServiceEndpoint(Type, Binding, String)
Adds a service endpoint to the hosted service with a specified contract, binding, and endpoint address. (Inherited from ServiceHost.)AddServiceEndpoint(Type, Binding, Uri)
Adds a service endpoint to the hosted service with a specified contract, binding, and URI that contains the endpoint address. (Inherited from ServiceHost.)AddServiceEndpoint(String, Binding, String, Uri)
Adds a service endpoint to the hosted service with a specified contract, binding, endpoint address and URI that contains the address at which it listens. (Inherited from ServiceHostBase.)AddServiceEndpoint(String, Binding, Uri, Uri)
Adds a service endpoint to the hosted service with the specified contract, binding, and URIs that contain the endpoint and listening addresses. (Inherited from ServiceHostBase.)AddServiceEndpoint(Type, Binding, String, Uri)
Adds a service endpoint to the hosted service with a specified contract, binding, an endpoint address, and a URI on which the service listens. (Inherited from ServiceHost.)AddServiceEndpoint(Type, Binding, Uri, Uri)
Adds a service endpoint to the hosted service with a specified contract, binding, a URI that contains the endpoint address, and a URI on which the service listens. (Inherited from ServiceHost.)WebserviceHost doesn’t have a single param constructor.
public ServiceEndpoint AddServiceEndpoint(
Type implementedContract,
Binding binding,
string address
)Example:
static void Main(string[] args)
{
Uri baseAddress = new Uri(“http://localhost:8000/”);WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);
try
{
svcHost.Open();Console.WriteLine(“Service is running”);
Console.WriteLine(“Press enter to quit…”);
Console.ReadLine();svcHost.Close();
}
catch (CommunicationException cex)
{
Console.WriteLine(“An exception occurred: {0}”, cex.Message);
svcHost.Abort();
}
}
the motivation seems to reveal that B is the correct answer, as in A single constructor is used
The answer is B.
http://msdn.microsoft.com/fr-fr/library/system.servicemodel.web.webservicehost.aspx
Why answer B is only an answer? A is also answer. Even it has two parameters, it is possible to use it in the same way as in Answer A. I’ve tested it… works!
Ok, the correct response is B because:
We want a single endpoint. If you read MSDN, if you create a webServiceHost without a baseaddress, it will create 2 endpoints: one for HTTP and one for HTTPS.
“If WebServiceHost finds no endpoints in the service description, it automatically creates a default endpoint at the service’s base address for HTTP and HTTPS base addresses. It does not create an endpoint automatically if the user has configured an endpoint explicitly at the base address.”
Next, it’s true than WebServiceHost doesn’t have a single parameter constructor, but if you read in MSDN the signature, URI’s are params. And params is not mandatory. So, you can create a WebServiceHost with only one parameter => The explanation is not good, only what I said before is the good explanation.
So now, why B and not D? Because if we want to be coherent with the service description, no ServiceBehavior attribute is set, we don’t explicit said: [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]. So, we don’t want to have a singleton service (because the default value is PerSession). That’s why we use the constructor with the service type and not the service instance.
Applause!