PrepAway - Latest Free Exam Questions & Answers

What should you do?

A Windows Communication Foundation (WCF) solution uses the following contract to share a message across its clients.
(Line numbers are included for reference only.)

01 [ServiceContract]
02 public interface ITeamMessageService
03 {
04 [OperationContract]
05 string GetMessage();
07 [OperationContract]
08 void PutMessage(string message);
09 }

The code for the service class is as follows:

10 public class TeamMessageService: ITeamMessageService
11 {
12 Guid key = Guid.NewGuid();
13 string message = “Today’s Message”;
14 public string GetMessage()
15 {
16 return stringFormat(“Message:{0} Key:{1}”,
17 message, key);
18 }
19 public void PutMessage(string message)
20 {
21 this.message = message;
22 }
23 }

The service is self-hosted. The hosting code is as follows:

24 ServiceHost host = new ServiceHost(typeof(TeamMessageService));
25 BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None):
26 host.AddServiceEndpoint(MyApplication.ITeamMessageService, binding, “http://localhost:12345”);
27 host.Open();

You need to ensure that all clients calling GetMessage will retrieve the same string, even if the message is updated by clients calling PutMessage.
What should you do

PrepAway - Latest Free Exam Questions & Answers

A.
Add the following attribute to the TeamMessageService class, before line 10.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

B.
Add the following attribute to the TeamMessageService class, before line 10.
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
Then change the binding definition on the service at line 25, and on the client to the following
WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);
binding.ReliableSession.Enabled = true;

C.
Pass a service instance to the instancing code in line 24, as follows.
ServiceHost host = new ServiceHost(new TeamMessageService());

D.
Redefine the message string in line 13, as follows
static string message = “Today’s Message”;
Then change the implementation of PutMessage in lines 19-22 to the following
public void PutMessage(string message)
{
TeamMessageServiceMessage.PutMessage;
}

Explanation:
InstanceContextMode Enumeration
(http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx)

PerSession A new InstanceContext object is created for each session.
PerCall A new InstanceContext object is created prior to and recycled subsequent to each call. If the channel does not create a session this value behaves as if it were PerCall.
Single Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.

3 Comments on “What should you do?

  1. Igor says:

    …. GetMessage will retrieve the same string , even if the message is updated by clients calling PutMessage…….
    Could have to 2 meanings:

    1. Whatever client whenever calls GetMessage the responce always will be”Today’s Message”
    2. No matter what client and when it calls GetMessage it will get the value of the same string “message” which never got destroyed during the life time of the service host.

    For me #1 is what do they mean in the question. But this happens when InstanceContenxtMode is PerCall. Basically the sample code with no changes (BasicHttpBinding is sessionless so the default PerSession will be transformed to PerCall).

    For #2 the correct answer is “A”.




    0



    0

Leave a Reply