15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

service implementation ❘ 1295<br />

serViCebehaVior ProPerTy<br />

ConcurrencyMode<br />

UseSynchronizationContext<br />

IncludeExceptionDetailInFaults<br />

MaxItemsInObjectGraph<br />

ValidateMustUnderst<strong>and</strong><br />

desCriPTion<br />

Because stateful objects can be used by multiple clients (or<br />

multiple threads of a single client), you must pay attention to<br />

concurrency issues with such object types. If the property<br />

ConcurrencyMode is set to Multiple, multiple threads can<br />

access the object, <strong>and</strong> you must deal with synchronization. If<br />

you set the option to Single, only one thread accesses the<br />

object at a time. Here, you don’t have to do synchronization;<br />

however, scalability problems can occur with a higher number of<br />

clients. The value Reentrant means that only a thread coming<br />

back from a callout might access the object. For stateless<br />

objects, this setting has no meaning, because new objects are<br />

instantiated with every method call <strong>and</strong> thus no state is shared.<br />

With Windows Forms <strong>and</strong> WPF, members of controls can be<br />

invoked only from the creator thread. If the service is hosted<br />

in a Windows application, <strong>and</strong> the service methods invoke<br />

control members, set the UseSynchronizationContext<br />

to true. This way, the service runs in a thread defined by the<br />

SynchronizationContext.<br />

With .<strong>NET</strong>, errors show up as exceptions. SOAP defines<br />

that a SOAP fault is returned to the client in case the server<br />

has a problem. For security reasons, it’s not a good idea to<br />

return details of server-side exceptions to the client. Thus, by<br />

default, exceptions are converted to unknown faults. To return<br />

specific faults, throw an exception of type FaultException.<br />

For debugging purposes, it can be helpful to return the real<br />

exception information. This is the case when changing the<br />

setting of IncludeExceptionDetailIn Faults to true. Here<br />

a FaultException is thrown where the original<br />

exception contains the detail information.<br />

With the property MaxItemsInObjectGraph, you can limit<br />

the number of objects that are serialized. The default limitation<br />

might be too low if you serialize a tree of objects.<br />

The property ValidateMustUnderst<strong>and</strong> set to true means<br />

that the SOAP headers must be understood (which is the<br />

default).<br />

To demonstrate a service behavior, the interface IStateService defines a service contract with two<br />

operations to set <strong>and</strong> get state. With a stateful service contract, a session is needed. That’s why the<br />

SessionMode property of the service contract is set to SessionMode.Required. The service contract<br />

also defines methods to initiate <strong>and</strong> close the session by applying the IsInitiating <strong>and</strong> IsTerminating<br />

properties to the operation contract:<br />

[ServiceContract(SessionMode=SessionMode.Required)]<br />

public interface IStateService<br />

{<br />

[OperationContract(IsInitiating=true)]<br />

void Init(int i);<br />

[OperationContract]<br />

void SetState(int i);<br />

[OperationContract]<br />

int GetState();<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!