[WCF] Programing Services

WCF has services and clients. This post shows the basic steps to create services.

 

1. Steps to Program Services

To create a WCF service, you need to do the following tasks:

  • Define Data Contracts (data classes)
  • Define Service Contracts (interfaces)
  • Implement the Service (concrete classes that implement service interfaces)
  • Configure the Service (endpoints: address and binding)
  • Host the Service

 

2. Defining Data Contracts

In general, you are creating a .Net type (class) for a message. It is called a typed message:

  • The type is annotated with attributes
  • At runtime, a .Net type is serialized into a message

A default serializer is “DataContractSerializer“.

  • looks for the [DataContract] and [DataMemeber] attributes during serialization
  • Attributes are located in the “System.Runtime.Serialization” namespace
[DataContract] public class Product
{
  [DataMember]
  public string Name { get; set; }

  [DataMember]
  public string Description { get; set; }
}

[Note] “DataContractSerializer” adopts the “opt-in” model. You need to explicitly specify the [DataMember] attribute. Any properties which miss the attribute will not be serialized even though they are “public”.

 

3. Defining Service Contracts

.Net interface is used to model service contract.

  • Interface defines a group of operations : [ServiceContract]
  • Method defines a message exchange pattern : [OperationContract]
[ServiceContract]
public interface IProductService
{
  [OperationContract(IsOneWay=true)]
  void SubmitProduct(Product p);

  [OperationContract]
  Product GetProduct(string Name);

  [OperationContract]
  List<Product> GetAllProducts();
}

 

4. Implementing the Contract

Now, you need to implement a service interface in a concrete class.

  • It is possible to implement multiple service contracts in a single class.
  • You can modify local execution details with behavior attributes: [ServiceBehavior], [OperationBehavior]
[ServiceBehavior(
  InstanceContextMode = InstanceContextMode.Single,
  ConcurrencyMode = ConcurrencyMode.Multiple )]
public class ProductService : IProductService
{
  ...
}

 

5. Configuring Service Behaviors

The [ServiceBehavior] is used to control service instancing and threading.

The “InstanceContextMode” has the following instancing mode:

  • PerCall : a new instance for every incoming message
  • PerSession : a unique instance for each user
  • Single : a singleton instance for all incoming messages

The “ConcurrencyMode” specifies the threading model:

  • Single : a single thread for a service instance
  • Multiple : multiple threads will be allowed for a service instance
  • Reentrant : a special case of Single and accepts reentrant calls

 

6. Hosting Services

In general, you host services in IIS. But you can host WCF services in any . NET applications with the “System.ServiceModel.ServiceHost” class.

  • Managed Host : IIS, WAS(Windows Process Activation Service), Windows Services
  • Self Host : any .NET application

 

7. Configuring Service Endpoints

You can configure endpoints in 2 ways : .config or code

The “ServiceHost.AddServiceEndpoint()” method is used to add an endpoint to the hosted service in code.

ServiceHost host = new ServiceHost(typeof(CalculatorService));

host.AddServiceEndpoint(
  typeof(ICalculator), // contract
  new BasicHttpBinding(), // binding
  "http://localhost:8000/service/basic " // address
);

host.AddServiceEndpoint(
  typeof(ICalculator), // contract
  new WSHttpBinding(), // binding
  "http://localhost:8000/service/ws " // address
);

host.Open();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s