Microsoft Exam Questions

You need to implement CreateHost so that the service has a single endpoint hosted at…

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
Description

WebServiceHost()
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
Description

AddServiceEndpoint(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();
}
}