Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:57:08+00:00 2026-05-31T10:57:08+00:00

I’m implementing WCF into an existing application, and as part of the implementation I’ve

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:57:09+00:00Added an answer on May 31, 2026 at 10:57 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.