views:

321

answers:

3

i have a wcf service in c# (like the calculatorservice from msdn examples), and i was wondering is it possilble to limit it to serve only 1 client per IP?

i want the possibility of a few IPs to be connected at once (at least 5-6 active sessions), and that works for now, but i don't want more than 1 connection per unique IP (or unique computer)

A: 

You might want to have a look at the service throttling options on the service behavior,

e.g

<behavior name="throttled">
  <serviceThrottling 
    maxConcurrentCalls="10"     // # of messages the host can process (default = 10)
    maxConcurrentInstances="1"  // # of host instances
    maxConcurrentSessions="16"  // # of host sessions (default = 16)
    />
</behavior>

http://www.michaelflanakin.com/Weblog/tabid/142/articleType/ArticleView/articleId/904/WCF-Service-Throttling.aspx

ForeverDebugging
thanks, but unfortunately, that does not help, because when i set maxConcurrentInstances="1", then i can work just with one client at a time -- as i said, i need, for example, 5 opened sessions at all time, they all use the service every now and then, until they close the client.
avance70
A: 

If you need 5 opened sessions at one time and you are saying that maxConcurrentInstances causes only 1 usuable client, have you set the InstanceContextMode to = PerSession? You most likely have it set to Single, in that case maxConcurrentCalls has no effect. Change it to PerSession and the throttling config ForeverDebugging has shown will work.

RandomNoob
everything is pretty much on default setting, instancecontextmode is persession... and if i set maxConcurrentInstances="1" i only get 1 usable session, and if i leave maxConcurrentInstances to default, i can use around 10 sessions i think (which is enough for now).
avance70
A: 

i made some progress. when the client invokes the service for the first time, in the constructor i save his ip address and his current operation context.

then, if someone from the same ip connects, i abort the previous channel that that ip address used (newest connection has the priority):

operationContext.Channel.Abort();

i tested it, and it appears to free up the session so others can use it, but i am not sure -- is this the best solution?

avance70
this appears to be working satisfactory for now, please comment if you have any suggestions. i kept googling for a similar solution, but couldn't find anything useful.
avance70