Click here to Skip to main content
15,502,045 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am self-hosting WCF services and have a special(?) requirement: I need a service instance for each channel. I need some advice on how to do this.

I have surmised that I cannot use InstanceContextMode.PerSession since I want each channel to have its own service instance, not just per session. I cannot user PerCall since I want the same instance for a given channel. Therefore, it appears that I need to use Single, but with some special handling. Does this sound correct? Or, is there a way to satisfy my requirement with a combination of InstanceContextMode, SessionMode, etc.?

After some reading, it appears that my special handling needs to be in a custom IInstanceProvider. Does this sound correct? Is there something else I need to do?

I am feeling my way around a bit and just looking for advice on whether I am on the right path. If not, please point me in another direction and I'll check it out. If so, then any helpful advice (articles, examples) is most welcome.
Posted

One single question: why do you need that? Instantiation has some other aspects in mind (this is a good comparison: Three ways to do WCF instance management[^]). If you want something like per-channel, make the logic common, but create several different singleton services each bound[^] to that channel/protocol.
 
Share this answer
 
v2
Comments
kalkwarf 30-May-13 15:26pm    
Yes, I read that article. I understand the basics. However, none of the three options fits my needs.

On the server, I have multiple pieces of hardware each with the same interface. Some of the pieces are identical or ultimately use the same class for communication. Therefore, I need to indicate for which I need a separate instance of that class. The logical method seems to be a channel.
Zoltán Zörgő 30-May-13 15:41pm    
The channel concept is not related to the method. It is a matter of communication, like the tcp/ip stack. See: http://debugmode.net/2011/10/15/what-is-channel-in-wcf/
As I know, there is no way to make binding per method. You can however make multiple endpoints/bindings for the same service: http://msdn.microsoft.com/en-us/library/ms751515.aspx
kalkwarf 30-May-13 16:10pm    
Thanks for the Channel article.

So, there is no way to indicate that I want separate service instances within a single session?

I had contemplated creating separate endpoints for each piece of hardware, but that seemed to be a waste since I would have to create separate ServiceHost instances for each. However, I may have to do this.
I came up with a solution.

- I created a custom ServiceHost and InstanceProvider.

- I added multiple endpoints to my ServiceHost. For some reason I thought that a ServiceHost could only handle one endpoint. Not true, you can add many. This allowed me to handle all requests for a particular service in one place (my custom host).

- Remember that I wanted to create separate service instances per channel. Channel information can be found in the OperationContext.Current.Channel object. There are several properties one can use to distinguish one channel from another.

- In my custom InstanceProvider.GetInstance I create a service instance for each unique URI. I track them with a Dictionary. If I have already created an instance for a given URI, I simply return the one I already created.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900