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
- The Singleton Service Instance created only once.
- It created when the service host is created.
- It lives forever.
- It got disposed only when host shuts down.
- Singleton service instance does not require client to maintain any logical session.
-
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()
{
}
}
}
- There are two contracts IService1 and IService2.
- For IService1 session mode is set to SessionMode=SessionMode.Required
-
For IService2 session mode is set to SessionMode = SessionMode.NotAllowed
- Service is implanting both the contracts.
- 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>
.
-
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
- Having scalability problem.
- All requests to service will run on different worker thread, so it will cause synchronization problem.
- Only one client could have access to singleton service instance at a time. It will decrease throughput of the service.
- Responsiveness is very low
-
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