Single Instance Management in WCF

Objective

This article is part # 3 of Instance Management in WCF.

This article will explain about Singleton Service Instance mode. This article will give theoretical introduction and explanation of coding sample also. It will also discuss about various disadvantages of Singleton Service Instance mode and its potential place of uses.

Part # 1 of this series could be found Here.

Part # 2 of this series could be found Here. 

Singleton Service


  1. The Singleton Service Instance created only once.
  2. It created when the service host is created.
  3. It lives forever.
  4. It got disposed only when host shuts down.
  5. Singleton service instance does not require client to maintain any logical session.
  6. It does not required to use binding that supports transport level session.


Code

[ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]


public
class
Service1 : IService1,IService2 ,IDisposable

{

}

Explanation with example

Service Contract 1

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SingleServiceInstanceService

{

[ServiceContract(SessionMode=SessionMode.Required)]


public
interface
IService1

{

[OperationContract]


int GetDataFromRequiredContract();

}

}

 Service Contract 2

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SingleServiceInstanceService

{

[ServiceContract(SessionMode = SessionMode.NotAllowed)]


public
interface
IService2

{

[OperationContract]


int GetDataFromNotAllowedContract();

}

}

 Service

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace SingleServiceInstanceService

{

[ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]


public
class
Service1 : IService1,IService2 ,IDisposable

{


int _Counter = 0;


public Service1()

{

}


public
int GetDataFromRequiredContract()

{

_Counter++;


return _Counter;

}


public
int GetDataFromNotAllowedContract()

{

_Counter++;


return _Counter;

}


public
void Dispose()

{

}

}

} 

  1. There are two contracts IService1 and IService2.
  2. For IService1 session mode is set to SessionMode=SessionMode.Required
  3. For IService2 session mode is set to SessionMode = SessionMode.NotAllowed
  4. Service is implanting both the contracts.
  5. Service Behavior is configured to single instance mode

ServiceBehavior(InstanceContextMode =InstanceContextMode.Single)]

 

Web.Config 

<system.serviceModel>

<services>

<service
name=SingleServiceInstanceService.Service1
behaviorConfiguration=SingleServiceInstanceService.Service1Behavior>

<!– Service Endpoints –>

<endpoint
address=RequiredService
binding=wsHttpBinding
contract=SingleServiceInstanceService.IService1>

</endpoint>

<endpoint
address=AllowedService
binding=wsHttpBinding
contract=SingleServiceInstanceService.IService2>

</endpoint>

<endpoint
address=mex
binding=mexHttpBinding
contract=IMetadataExchange/>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behavior
name=SingleServiceInstanceService.Service1Behavior>

<serviceMetadata
httpGetEnabled=true/>

<serviceDebug
includeExceptionDetailInFaults=false/>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

.

  1. In System.ServiceModel, we need to expose two addresses. One each for individual contracts.

Client

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Test.ServiceReference1;

namespace Test

{


class
Program

{


static
void Main(string[] args)

{


Service1Client proxy = new
Service1Client();


Console.WriteLine(proxy.GetDataFromRequiredContract().ToString());


Service2Client proxy2 = new
Service2Client();


Console.WriteLine(proxy2.GetDataFromNotAllowedContract().ToString());


Console.ReadKey(true);

}

}

}

Output

 

 

Explanation of output 

Since service instance is configured for Singleton, all the proxy will share the same service instance. That is why all proxy is incrementing the same value. And as the output, each time client is getting incremented value

Disadvantage 

  1. Having scalability problem.
  2. All requests to service will run on different worker thread, so it will cause synchronization problem.
  3. Only one client could have access to singleton service instance at a time. It will decrease throughput of the service.
  4. Responsiveness is very low
  5. Singleton Service instance should only be used, when no other options are available. For instance Global Log Book, Single Communication Port etc.

Conclusion 

This article explained about, Singleton Service Instance mode. This article gave theoretical introduction and explanation of coding sample also. It also discussed about various disadvantages of Singleton Service Instance mode and its potential place of uses.

Happy Coding


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 )

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

Create a website or blog at WordPress.com