|
-
Nov 12th, 2010, 08:44 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Multi thread WCF
I'm really baffled and am hoping someone can help., I have the following code.
Code:
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple)> _
Public Class eMPmain
Implements IIVR
Dim myx As Integer
Private Function testloop() As String
Dim mystr As String = ""
For times As Integer = 1 To 10
myx = myx + 10
mystr = mystr & myx.ToString & Now.ToLongTimeString & vbCrLf
Thread.Sleep(500)
Next
testloop = mystr
End Function
Public Function CreateIVRSession(ByVal ddi As String, ByVal cli As String) As String Implements IIVR.CreateIVRSession
CreateIVRSession = testloop()
End Function
So the response is something like the below.
110 13:20:06
120 13:20:06
130 13:20:07
140 13:20:07
150 13:20:08
160 13:20:08
170 13:20:09
180 13:20:09
190 13:20:10
200 13:20:10
I assumed though if two people called this at the same time that I'd get a mixed up reply with something like (as i have ConcurrencyMode:=ConcurrencyMode.Multiple set)
110 13:20:06
130 13:20:06
150 13:20:07
170 13:20:07
As both clients would be altering the integer myx at the same time. This is not the case though one client runs, while the other client just has to wait. The first client finishes then the other client starts. Which seems single threaded to me.
Am i missing something? Does anything else have to be set to make a new thread fire when poeple call my wcf service?
-
Nov 15th, 2010, 03:25 PM
#2
Re: Multi thread WCF
This link explains in detail. Because there is only one instance of a singleton service all clients have to wait until the previous is done.
-
Nov 16th, 2010, 10:40 AM
#3
Thread Starter
Hyperactive Member
Re: Multi thread WCF
Um, it says
Having a singleton implies the singleton has some valuable state that you want to share across multiple clients. The problem is that when multiple clients connect to the singleton, they may all do so concurrently on multiple worker threads. The singleton must synchronize access to its state to avoid state corruption. This in turn means that only one client at a time can access the singleton. This may degrade responsiveness and availability to the point that the singleton is unusable as the system grows.
Which I suppose ties in with what I'm seeing.
I'll have a look at per call\per session and see what happens then.
-
Nov 16th, 2010, 02:28 PM
#4
Re: Multi thread WCF
This line:
This in turn means that only one client at a time can access the singleton
says it all. The second client just waits until the first is finished. It would be a major WCF bug if you could do what you want to see.
Why do you want a sinle instance anyway?
-
Nov 17th, 2010, 06:18 AM
#5
Thread Starter
Hyperactive Member
Re: Multi thread WCF
Okay the plot thinkens.
As for initial testing purposes I was self hosting my WCF service, which I though might be casuing some of the single threaded problems. As I'd read in other places that a multithreaed singleton should behave as I belived it to. So I moved it to being hosted in IIS and now when I run two clients with teh above code I get
17/11/2010 11:09:59
410 11:10:01
420 11:10:01
430 11:10:02
450 11:10:02
470 11:10:03
490 11:10:03
510 11:10:04
530 11:10:04
550 11:10:05
570 11:10:05
17/11/2010 11:10:00
440 11:10:02
460 11:10:02
480 11:10:03
500 11:10:03
520 11:10:04
540 11:10:04
560 11:10:05
580 11:10:05
590 11:10:06
600 11:10:06
Which is the mixed up response I expect. So even with singletons you can apparently get multiple clients accessing them at the same time.
Anyway in answer to your question I'm not tied to singletons as is quite early stages of the project so more investigating the best solution. I would suspect though with all I've read I'll probably decide against using a singleton anyway, as it does seem to have some limitations.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|