dthagard
dthagard

Reputation: 853

How do I Implement InstanceContextSharing in WCF Between Applications?

I'm implementing WCF into an existing application, and as part of the implementation I've created a singleton service called Manager which calls and instantiates another service (which we'll call LegacyApp). LegacyApp is per-session service that is nothing more than the existing application instantiated as a monolithic object (this design is due to the architecture of the existing application).

The idea is that any client (e.g. .NET, Java) can connect to the Manager service, get a list of preexisting LegacyApp instances, and either connect to an existing instance or get a new instance.

I've implemented InstanceContextSharing per the MSDN examples, but this appears only to work on a per application basis, i.e. if I create multiple LegacyApp clients at the Manager service, the Manager service can switch between the different LegacyApp contexts by passing in the custom header with the proper unique session ID. However, if I try and connect to the LegacyApp using a new client with the same unique session ID, the LegacyApp service behaves as if no other client has connected.

My question is this: how can one implement session sharing in WCF that works between different applications?

Edit(1) The Manager service and the LegacyApp service are being hosted by the same service host. Manager service uses a proxy client (defined in a separate proxy library) to connect to the LegacyApp service. In a sense, the service host is a client of itself as both client and server are in the same service host. If I create another client (say a WinForms client) that uses this same proxy library to create a proxy client, then I get the behavior I described above.

Below is some snippets of the code that I've implemented. The extension class and the Shareable attribute class are identical to those in the MSDN example. For brevity, I've left out the irrelevant parts of the various services and clients.

The LegacyApp service:

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
<Shareable()>
Public Class LegacyAppService
    Implements ILegacyApp

    Private _foo As String = "Not Set"

    Public Sub SetFoo(ByVal foo As String) Implements ILegacyApp.SetFoo
        _foo = foo
    End Sub

    Public Function GetFoo() As String Implements ILegacyApp.GetFoo
        Return _foo
    End Function

End Class

The LegacyApp client:

Public Class LegacyAppClient
    Inherits ClientBase(Of ILegacyApp)
    Implements ILegacyApp

    Public Sub New()
        Me.New(String.Empty)
    End Sub

    Public Sub New(ByVal id As String)
        If IsNothing(id) OrElse id = String.Empty Then
            _uniqueID = NewInstanceId()
        Else
            _uniqueID = id
        End If
        CreateContextHeader()
    End Sub

    Private Shared Function NewInstanceId() As String
        Dim random As Byte() = New Byte(CInt(256 / 8 - 1)) {}
        RandomNumberGenerator.GetBytes(random)
        Return Convert.ToBase64String(random)
    End Function

    Private Sub CreateContextHeader()
        _contextHeader = MessageHeader.CreateHeader(CustomHeader.HeaderName, CustomHeader.HeaderNamespace, _uniqueID)
    End Sub

    Public ReadOnly Property ID() As String
        Get
            Return _uniqueID
        End Get
    End Property

    Public Sub SetFoo(ByVal foo As String) Implements ILegacyApp.SetFoo
        Using New OperationContextScope(InnerChannel)
            OperationContext.Current.OutgoingMessageHeaders.Add(_contextHeader)
            Channel.SetFoo(foo)
        End Using
    End Sub

    Public Function GetFoo() As String Implements ILegacyApp.GetFoo
        Using New OperationContextScope(InnerChannel)
            OperationContext.Current.OutgoingMessageHeaders.Add(_contextHeader)
            Return Channel.GetFoo()
        End Using
    End Function

End Class

The Manager service:

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single)>
Public Class ManagerService
    Implements IManager

    Public Function CreateService() As String Implements IManager.CreateService
        Dim openError As Integer
        Dim legacyAppClient As New LegacyAppClient
        legacyAppClient.SetFoo(legacyAppService.ID)
        Return legacyAppClient.ID
    End Function

End Class

The test client:

<...>
Dim sessionID As String
Dim legacyApp As LegacyAppClient
sessionID = _managerClient.CreateService()
legacyAppClient = New LegacyAppClient(sessionID)
Dim foo As String = legacyAppClient.GetFoo()
<...>

The behavior I would expect is to get the sessionID value back when querying foo, but I'm the legacyAppClient.GetFoo() method called in the test client is returning "Not Set".

Edit(2) Am I thinking about this problem incorrectly? Should I have all calls from the test client go to the Manager service and have it act as a wrapper to the LegacyApp service?

Upvotes: 1

Views: 1157

Answers (2)

dthagard
dthagard

Reputation: 853

For those interested in our solution, we solved the problem by implementing the isolated service host architecture. Our solution was to have the Manager service provide several operations to instantiate, query, and close multiple LegacyApp services each isolated within their own AppDomain (this was necessary due to the prolific use of global variables in LegacyApp). Each LegacyApp service is hosted as a singleton instance, so any clients wishing to share the same LegacyApp session need only query the Manager service for the address and then connect to the proper instance. This solution works well for us since the LegacyApp behaves as a singleton anyways.

Also, a big thanks to Matt Brindley for his code to query the local machine for the next available TCP port. This code is used in the implementation of the StartInstance operation in the Manager service.

Another big thanks to Ladislav Mrnka for setting us straight on WCF instance context sharing.

Some code snippets are below:

IManager interface:

Imports System.ServiceModel

<ServiceContract()>
Public Interface IManager

    <OperationContract()>
    Function GetOpenInstances() As String()

    <OperationContract()>
    Function StartInstance() As String

    <OperationContract()>
    Sub CloseInstance(ByVal address As String)

End Interface

Upvotes: 0

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

Instance context sharing is feature of single process. What you are probably looking for is durable context stored for example in database.

Upvotes: 1

Related Questions