wcf
  1. wcf-percall-instance-management

PerCall Instance Management - (WCF)

Instance management is the process of creating and destroying instances of a service in Windows Communication Foundation (WCF). There are three types of instance management available in WCF: PerCall, PerSession, and Singleton. In this page, we will focus on PerCall instance management.

Syntax

To use PerCall instance management in WCF, you need to set the InstanceContextMode property of your service to InstanceContextMode.PerCall. Here is an example of how to do this in C#:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
  // Your service implementation code here
}

Example

Here's an example of how to use PerCall instance management in WCF:

[ServiceContract]
public interface IMyService
{
  [OperationContract]
  string GetMessage();
}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
  public string GetMessage()
  {
    return "Hello, world!";
  }
}

Explanation

In the example, we define a WCF service called MyService with a single method, GetMessage(), that returns a string. We use the [ServiceBehavior] attribute to set the InstanceContextMode property to InstanceContextMode.PerCall, which means that a new instance of the service will be created for each client request. This is useful if your service needs to store state that is specific to each client request.

Use

PerCall instance management is useful when you need to create a new instance of your service for each client request. This can be useful if you need to store state that is specific to each request, or if you have long-running operations that could interfere with other client requests.

Important Points

  • PerCall instance management creates a new instance of your service for each client request.
  • PerCall instance management is useful when you need to store state that is specific to each request, or if you have long-running operations that could interfere with other client requests.
  • PerCall instance management can be less efficient than PerSession or Singleton instance management, since a new instance of the service must be created for each client request.

Summary

In this page, we discussed how to use PerCall instance management in WCF. We covered the syntax, example, explanation, use, and important points of using PerCall instance management in WCF. PerCall instance management can be useful when you need to store state that is specific to each client request, or if you have long-running operations that could interfere with other client requests.

Published on: