I created a WCF service, and want to be used by multiple VB clients.
I need only a single instance of the service, even if multiple clients start, use it, or stop.
But the result seems to be against my expectation. My code is:
WCF service:
namespace xComm
{
[ServiceBehavior(
InstanceContextMode = InstanceContextMode.Single,
ConcurrencyMode = ConcurrencyMode.Single)]
public class SvcACS : ISvcACS
{
private int m_nCounter = 0;
public int CounterUp()
{
m_nCounter++;
return m_nCounter;
}
public int CounterGet()
{
return m_nCounter;
}
}
}
After building the WCF service, I can use it by COM reference from VB application, as the following one, a simple form application calling CountUp method and checking return value:
Option Explicit
Dim svcAcs As xComm.ISvcACS
Private Sub btnCmd_Click(Index As Integer)
Dim nW1 As Integer
nW1 = svcAcs.CounterUp()
lblCounter.Caption = CStr(nW1)
End Sub
Private Sub Form_Load()
Set svcAcs = New xComm.svcAcs
End Sub
I started for example 2 VB clients and press the btnCmd button respectively. What I want is:
- client 1's btnCmd is clicked: client 1's lblCount shows 1
- client 2's btnCmd is clicked: client 2's lblCount shows 2
- client 1's btnCmd is clicked: client 1's lblCount shows 3 ...
But the result turned out that clients are serviced independently, instead of by using a single instance of service, which the counter would have been shared.
I have used InstanceContextMode.Single, is this not enough?
Thanks in advance.
Ting
some new findings(2022/7/26):
Instead of using COM reference directly, I tried the following VB code:
Dim Client As New MSSOAPLib30.SoapClient30
Set Client = CreateObject("MSSOAP.SoapClient30") 'xcomm.svcACS")
Call Client.mssoapinit("http://localhost:8733/SvcACS?wsdl")
'localhost:8733/SvcACS" binding="basicHttpBinding"
MsgBox (Client.CounterUp())
The result shows that several VB application instances consume a same service instance.
And I also tested C# clients by using SvcUtilexe generated client code, which shows the same result.
But in both the cases, changing InstanceContextMode to PerCall or PerSession makes not difference.
I guess, maybe it is the behavior of self hosting service, in which the active service works for all.
But still I dont understand how COM interface works. Seemingly by using COM interface, each time, a new service instance is created.