The Framework
WCF provides the framework to comprehensively manage and utilize the following 6 broad aspects of service orientation and development:
- Service – the core of the framework and an almost independent entity
- Host – independent of the service and is independently scalable
- Client – aware of the service API only via the proxy
- Communication – connects clients to service hosts via channels
- Security – tiers of out-of-the-box protection
- Debugging – tools and techniques
The Service
A WCF service promotes the separation of state and behavior. It’s important for two main reasons:
- Behavior encapsulation and centralization – lower TCO in terms of maintainability and reusability as well as raising the potential scalability by orders of magnitude.
- State portability without much fear of unintentional or other malice.
The Object Model
Services are supported by the framework by a set of classes primarily from the System.ServiceModel namespace.
Figure 1 – core service framework object model
System.ServiceModel.ServiceContractAttribute – Service Interface Decorator
CallbackContract – Gets or sets the type of callback contract when the contract is a duplex contract. Defaults to null.
ConfigurationName – Gets or sets the name used to locate the service in an application configuration file. Defaults to fully qualified interface type name.
HasProtectionLevel – Gets a value that indicates whether the member has a protection level assigned. Defaults to false.
Name – Gets or sets the name for the <portType> element in Web Services Description Language (WSDL). Defaults to Managed type name.
Namespace – Gets or sets the namespace of the <portType> element in Web Services Description Language (WSDL). Defaults to http://tempuri.org/.
ProtectionLevel – Specifies whether the binding for the contract must support the value of the ProtectionLevel property. Defaults to None.
SessionMode – Gets or sets whether sessions are allowed, not allowed or required. Defaults to Allowed.
System.ServiceModel.ServiceBehaviorAttribute – Service Implementation Decorator
AddressFilterMode – Gets or sets the AddressFilterMode that is used by the dispatcher to route incoming messages to the correct endpoint. Defaults to Exact.
- Available options
- Any – Indicates a filter that matches on any address of an incoming message. Using this value turns off the address filter check. Any message, no matter what its WS-Adressing:To identity is accepted
- Exact – Indicates a filter that does an exact match on the address of an incoming message
- Prefix – Indicates a filter does the longest prefix matches on the address of an incoming message
- AddressFilterMode.Prefix
- Understanding Address Filtering
- WCF Addressing In Depth
AutomaticSessionShutdown – Specifies whether to automatically close a session when a client closes an output session. Defaults to true.
ConcurrencyMode – Gets or sets whether a service supports one thread, multiple threads, or reentrant calls. Defaults to Single.
- Available options
- Single – The service instance is single-threaded and does not accept reentrant calls. If the InstanceContextMode property is Single, and additional messages arrive while the instance services a call, these messages must wait until the service is available or until the messages time out.
- Reentrant – The service instance is single-threaded and accepts reentrant calls. The reentrant service accepts calls when you call another service; it is therefore your responsibility to leave your object state consistent before callouts and you must confirm that operation-local data is valid after callouts. Note that the service instance is unlocked only by calling another service over a channel. In this case, the called service can reenter the first service via a callback. If the first service is not reentrant, the sequence of calls results in a deadlock. For details, see ConcurrencyMode.
- Multiple – The service instance is multi-threaded. No synchronization guarantees are made. Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times.
ConfigurationName – Gets or sets the value used to locate the service element in an application configuration file. Defaults to fully qualified implementation type name.
IgnoreExtensionDataObject – Gets or sets a value that specifies whether to send unknown serialization data onto the wire. Defaults to false.
IncludeExceptionDetailInFaults – Gets or sets a value that specifies that general unhandled execution exceptions are to be converted into a System.ServiceModel.FaultException<TDetail> of type System.ServiceModel.ExceptionDetail and sent as a fault message. Set this to true only during development to troubleshoot a service. Defaults to false.
InstanceContextMode – Gets or sets the value that indicates when new service objects are created. Defaults to PerSession.
- Available options
- 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. For singleton lifetime behavior (for example, if the host application calls the ServiceHost constructor and passes an object to use as the service), the service class must set InstanceContextMode to InstanceContextMode.Single, or an exception is thrown when the service host is opened.
MaxItemsInObjectGraph – Gets or sets the maximum number of items allowed in a serialized object. Defaults to 64KB.
Name – Gets or sets the value of the name attribute in the service element in Web Services Description Language (WSDL). Defaults to service implementation class name.
Namespace – Gets or sets the value of the target namespace for the service in Web Services Description Language (WSDL). Defaults to http://tempuri.org/.
ReleaseServiceInstanceOnTransactionComplete – Gets or sets a value that specifies whether the service object is released when the current transaction completes. Defaults to true.
TransactionAutoCompleteOnSessionClose – Gets or sets a value that specifies whether pending transactions are completed when the current session closes without error. Defaults to false.
TransactionIsolationLevel – Specifies the transaction isolation level for new transactions created inside the service, and incoming transactions flowed from a client. Defaults to Unspecified.
- Available options
- Serializable – Volatile data can be read but not modified, and no new data can be added during the transaction.
- RepeatableRead – Volatile data can be read but not modified during the transaction. New data can be added during the transaction.
- ReadCommitted – Volatile data cannot be read during the transaction, but can be modified.
- ReadUncommitted – Volatile data can be read and modified during the transaction.
- Snapshot – Volatile data can be read. Before a transaction modifies data, it verifies if another transaction has changed the data after it was initially read. If the data has been updated, an error is raised. This allows a transaction to get to the previously committed value of the data. When you try to promote a transaction that was created with this isolation level, an InvalidOperationException is thrown with the error message "Transactions with IsolationLevel Snapshot cannot be promoted".
- Chaos – The pending changes from more highly isolated transactions cannot be overwritten.
- Unspecified – A different isolation level than the one specified is being used, but the level cannot be determined. An exception is thrown if this value is set.
TransactionTimeout – Gets or sets the period within which a transaction must complete. Defaults to 0.
UseSynchronizationContext – Gets or sets a value that specifies whether to use the current synchronization context to choose the thread of execution. Defaults to true.
ValidateMustUnderstand – Gets or sets a value that specifies whether the system or the application enforces SOAP MustUnderstand header processing. Defaults to true.
GetWellKnownSingleton – Retrieves an object that implements the service and that is used as the singleton instance of the service, or null if there is no singleton instance.
SetWellKnownSingleton – Specifies an object that implements the service and that is used as the singleton instance of the service.
ShouldSerializeConfigurationName – Returns a value that indicates whether the ConfigurationName property has changed from its default value and should be serialized.
ShouldSerializeTransactionTimeout – Returns a value that indicates whether the TransactionTimeout property has changed from its default value and should be serialized.
System.ServiceModel.OperationContractAttribute – Service Operation Interface Decorator
Action – Gets or sets the WS-Addressing action of the request message.
AsyncPattern – Indicates that an operation is implemented asynchronously using a Begin<methodName> and End<methodName> method pair in a service contract.
HasProtectionLevel – Gets a value that indicates whether the messages for this operation must be encrypted, signed, or both.
IsInitiating – Gets or sets a value that indicates whether the method implements an operation that can initiate a session on the server (if such a session exists).
IsOneWay – Gets or sets a value that indicates whether an operation returns a reply message.
IsTerminating – Gets or sets a value that indicates whether the service operation causes the server to close the session after the reply message, if any, is sent.
Name – Gets or sets the name of the operation.
ProtectionLevel – Gets or sets a value that specifies whether the messages of an operation must be encrypted, signed, or both.
ReplyAction – Gets or sets the value of the SOAP action for the reply message of the operation.
System.ServiceModel.OperationBehaviorAttribute – Service Operation Implementation Decorator
System.Runtime.Serialization.DataContractAttribute – Service Payload Decorator
IsReference – Gets or sets a value that indicates whether to preserve object reference data. Defaults to false.
Name – Gets or sets the name of the data contract for the type. Defaults to class name.
Namespace – Gets or sets the namespace for the data contract for the type. Defaults to class namespace.
System.Runtime.Serialization.DataMemberAttribute – Service Payload Member Decorator
- Members must be both readable and writable otherwise System.Runtime.Serialization.InvalidDataContractException is raised.
EmitDefaultValue – Gets or sets a value that specifies whether to serialize the default value for a field or property being serialized. Defaults to true.
IsRequired – Gets or sets a value that instructs the serialization engine that the member must be present when reading or deserializing. Defaults to false.
Name – Gets or sets a data member name. Default is the actual member name.
- Separates CLR member name from externally exposed contract member name, which bear security and versioning benefits.
- Allows member names, otherwise not allowed in CLR
Order – Gets or sets the order of serialization and deserialization of a member.
Other references:
Professional WCF Programming