Which code segment should you insert at line 03?

You are developing a client application that uses the following code to consume a Windows Communication Foundation (WCF) service.
(Line numbers are included for reference only.)

01 BasicHttpBinding myBinding = new BasicHttpBinding();
02 EndpointAddress myEndpointAddress = new EndpointAddress(“http://contoso.com/TaxService.svc”);
03 …
04 ITaxService client = channelFactory.CreateChannel();
05 string data = client.GetData(1);

You need to consume the service. Which code segment should you insert at line 03?

You are developing a client application that uses the following code to consume a Windows Communication Foundation (WCF) service.
(Line numbers are included for reference only.)

01 BasicHttpBinding myBinding = new BasicHttpBinding();
02 EndpointAddress myEndpointAddress = new EndpointAddress(“http://contoso.com/TaxService.svc”);
03 …
04 ITaxService client = channelFactory.CreateChannel();
05 string data = client.GetData(1);

You need to consume the service. Which code segment should you insert at line 03?

A.
var channelFactory = new ChannelFactory<ITaxService>();

B.
var channelFactory = new ChannelFactory<ITaxService>(myBinding);

C.
var channelFactory = new ChannelFactory<ITaxService>(myBinding, myEndpointAddress);

D.
var channelFactory = new ChannelFactory<ITaxService>(“http://contoso.com/TaxService.svc”);

Explanation:
ChannelFactory<TChannel> Class
(http://msdn.microsoft.com/en-us/library/ms576132.aspx)

ChannelFactory<TChannel>() Initializes a new instance of the ChannelFactory<TChannel> class.
ChannelFactory<TChannel>(Binding) Initializes a new instance of the ChannelFactory<TChannel> class.
ChannelFactory<TChannel>(ServiceEndpoint) Initializes a new instance of the ChannelFactory<TChannel> class that produces channels with a specified endpoint.
ChannelFactory<TChannel>(String) Initializes a new instance of the ChannelFactory<TChannel> class with a specified endpoint configuration name.
ChannelFactory<TChannel>(Type) Initializes a new instance of the ChannelFactory<TChannel> class.
ChannelFactory<TChannel>(Binding, EndpointAddress) Initializes a new instance of the ChannelFactory<TChannel> class with a specified binding and endpoint address.
ChannelFactory<TChannel>(Binding, String) Initializes a new instance of the ChannelFactory<TChannel> class with a specified binding and remote address.
ChannelFactory<TChannel>(String, EndpointAddress) Initializes a new instance of the ChannelFactory<TChannel> class associated with a specified name for the endpoint configuration and remote address.

Delegation and Impersonation with WCF
(http://msdn.microsoft.com/en-us/library/ms730088(v=vs.90).aspx)

Example:
public class HelloService : IHelloService
{
[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string Hello(string message)
{
WindowsIdentity callerWindowsIdentity = ServiceSecurityContext.Current.WindowsIdentity;
if (callerWindowsIdentity == null)
{
throw new InvalidOperationException
(“The caller cannot be mapped to a Windows identity.”);
}
using (callerWindowsIdentity.Impersonate())
{
EndpointAddress backendServiceAddress = new EndpointAddress(“http://localhost:8000/ChannelApp”);
// Any binding that performs Windows authentication of the client can be used.
ChannelFactory<IHelloService> channelFactory = new ChannelFactory<IHelloService>(new NetTcpBinding(), backendServiceAddress);
IHelloService channel = channelFactory.CreateChannel();
return channel.Hello(message);
}
}
}



Leave a Reply 1

Your email address will not be published. Required fields are marked *