InstanceContextMode Enumeration Home
This page is specific to:.NET Framework Version:3.03.54.0
.NET Framework Class Library
InstanceContextMode Enumeration

Specifies the number of service instances available for handling calls that are contained in incoming messages.

Namespace:  System.ServiceModel
Assembly:  System.ServiceModel (in System.ServiceModel.dll)
Syntax

'Usage

Dim instance As InstanceContextMode

'Declaration

Public Enumeration InstanceContextMode
Members

Member nameDescription
PerSessionA new InstanceContext object is created for each session.
PerCallA new InstanceContext object is created prior to and recycled subsequent to each call. If the channel does not create a session this value behaves as if it were PerCall.
SingleOnly one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.
NoteNote:
For singleton lifetime behavior (for example, if the host application calls the ServiceHost constructor and passes an object to use as the service), the service class must set InstanceContextMode to InstanceContextMode..::.Single, or an exception is thrown when the service host is opened.
Remarks

The System.ServiceModel..::.InstanceContext manages the association between the channel and the user-defined service objects. Use the InstanceContextMode enumeration with the ServiceBehaviorAttribute..::.InstanceContextMode property to specify the lifetime of the InstanceContext object. Windows Communication Foundation (WCF) can create a new InstanceContext object for every call, every session or specify that the InstanceContext object is bound to a single service object. For a working example, see the Instancing Sample.

The Single value specifies that a single InstanceContext object should be used for the lifetime of the service.

NoteNote:

If the InstanceContextMode value is set to Single the result is that your service can only process one message at a time unless you also set the ConcurrencyMode value to Multiple.

Examples

The following code illustrates how to set the InstanceContextMode for a service class:

' Service class which implements the service contract.
Public Class CalculatorService
Implements ICalculator

    Public Function Add(n1 As Double, n2 As Double) As Double Implements ICalculator.Add
        Return n1 + n2
    End Function

    Public Function Subtract(n1 As Double, n2 As Double) As Double Implements ICalculator.Subtract
        Return n1 - n2
    End Function

    Public Function Multiply(n1 As Double, n2 As Double) As Double Implements ICalculator.Multiply
        Return n1 * n2
    End Function

    Public Function Divide(n1 As Double, n2 As Double) As Double Implements ICalculator.Divide
        Return n1 / n2
    End Function

End Class



Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0
See Also

Reference

Community Content

Content mess
Added by:Zverev Eugene
PerCall A new InstanceContext object is created prior to and recycled subsequent to each call. If the channel does not create a session this value behaves as if it were PerCall.

I think the second sentense is intended for anther option.

InstanceContextMode: VB example
Added by:clesso
The VB example above does not even include the ServiceBehavior attribute as does the C# example. Should this be implemented as...

' Service class which implements the service contract.
<ServiceBehavior(InstanceContextMode:=System.ServiceModel.InstanceContextMode.PerSession)> _
Public Class CalculatorService
Implements ICalculator
...

PerSession WCF Service
Added by:A W Developer
For a WCF service to use PerSession make sure you use a Binding that supports it. In the App.config file:
binding="basicHttpBinding"
will not use PerSession. In the App.config file:
binding="wsHttpBinding"
will use PerSession.
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View