WCF Application and Instance Management by using 3 different application domains (Asp.net, C # Windows Application and WPF application as clients)

Plz refer to the previous article

Part-4 Instance Management

Now to understand Instance management we just have to configure few setting for different Instance managements techniques but its very important to understand what impact they have on application and service objects.

First we will use

PerCall---- When we configure a WCF service as per call, new service instances are created for every method call.

In order to configure our service as Per Call, we just need to write

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]

Over Class Defination in Service.cs file.

Now we will run all the three clients and we will see how many service instance have been created with per call setting.

Before we start executing our client we need to update service reference on each client(ASP.net, C# windows application,WPF client) so that we always get updated services and we will have to do this everytime when we change the InstanceContextMode setting in WCF service.

Below is the screen shot:

Below are the screen shot for per call execution.

 

As we can see in every client service object count we get value 1 which shows that new service instances are created for every method call.

Even when we will hit the search button then also our service count will be 1.

In other words, the WCF service instance is created for every method call made to the WCF service instance so the value will always be one.

Now we will see PerSession

PerSession-In this only one instance of a WCF service object is created for a session interaction.

In order to configure WCF service to Per Session we just need to write

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]

Over Class Defination in Service.cs file.

Now We will run all the three clients and we will see how many service instance have been created with PerSession setting.

Below Are the Screen Shot:

When you will first run all the three clients you will get the value of Service object count as 1, but once when you will hit the search button the values will increment.

Below is the screen shot.

As we can see the value of Service object count has been incremented by 1 when we have hit the search button.

Again in the below screen we can see the value has been incremented by 1 when we have hit the search button on ASP.net client.

Same follows with the WPF client.

In per session every single client will be served with one server object,So when we call a method multiple times on same client the value of server object will be incremented ie. Only one server object will serve the same single client, but when we execute the different client the server object count will start with initial value.

Now we will see Single instance management.

Single- Often we would like to create one global WCF instance for all WCF clients. To create a single instance of a WCF service, we need to configure the WCF service as Single instance mode.

In order to configure WCF service to Single we just need to write

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]

Over Class Defination in Service.cs file.

Now we will run all the three clients and we will see how many service instance have been created with Single setting.

Below Are the Screen Shot:

As we can see that Every client service object count values is incremented

Here my order of execution was Windows Application client then ASP.net client and WPF client, so the value of Windows Application client is 1, ASP.net client is 2 and WPF client is 3

 

When we will hit search button each client the values of service object count will get incremented to 4,6 and 7 for C# Windows application client,ASP.net client and WPF client respectively.

Therefore, Single suggest that

Only one Service object will serve multiple client calls.

If you call the WCF service from a different client, you will see the counter incrementing. The counter becomes a global variable.

Tags:

WCF