Which two actions should you perform?

You are developing a Windows Communication Foundation (WCF) service to provide an in-memory cache.
The following code is part of your solution. (Line numbers are included for reference only.)

01
02 public interface IInMemoryCacheService
03 {
04 [OperationContract()]
05 string GetCachedItem(string key);
06
07 [OperationContract()]
08 void CacheItem(string key, string item);
09
10 }
11
12 [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
13 public class CacheService : IInMemoryCacheService
14 {
15
16 Hashtable cache = new Hashtable();
17
18 public string GetCachedItem(string key)
19 {
20 return cache(key).ToString();
21 }
22
23 public void CacheItem(string key, string item)
24 {
25 if (cache.Contains(key))
26 cache.Remove(key);
27 cache.Add(key, item);
28 }
29 }

Users report that the cache is getting updated with cache changes of other users.
You need to ensure that each user’s cache is maintained and isolated from other users.
Which two actions should you perform? (Each correct answer presents part of the solution. Choose two.)

You are developing a Windows Communication Foundation (WCF) service to provide an in-memory cache.
The following code is part of your solution. (Line numbers are included for reference only.)

01
02 public interface IInMemoryCacheService
03 {
04 [OperationContract()]
05 string GetCachedItem(string key);
06
07 [OperationContract()]
08 void CacheItem(string key, string item);
09
10 }
11
12 [ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
13 public class CacheService : IInMemoryCacheService
14 {
15
16 Hashtable cache = new Hashtable();
17
18 public string GetCachedItem(string key)
19 {
20 return cache(key).ToString();
21 }
22
23 public void CacheItem(string key, string item)
24 {
25 if (cache.Contains(key))
26 cache.Remove(key);
27 cache.Add(key, item);
28 }
29 }

Users report that the cache is getting updated with cache changes of other users.
You need to ensure that each user’s cache is maintained and isolated from other users.
Which two actions should you perform? (Each correct answer presents part of the solution. Choose two.)

A.
Insert the following code at line 01.
[ServiceContract(SessionMode=SessionMode.NotAllowed)]

B.
At line 12, replace InstanceContextMode.Single with InstanceContextMode.PerSession.

C.
At line 12, replace InstanceContextMode.Single with InstanceContextMode.PerCall.

D.
Insert the following code at line 01.
[ServiceContract(SessionMode=SessionMode.Required)]

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.



Leave a Reply 1

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