Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get a reference to a WCF ServiceHost Singleton Instance before the first WCF client request comes in?

Tags:

.net

wcf

I am trying to access the singleton instance created by my WCF service but .SingletonInstance seems to be null even after .Open is called on the ServiceHost.

Is there any way to force the ServiceHost to create the instance before the first WCF client request comes in (which presumably automatically spawns the singleton instance)?

like image 574
InvertedAcceleration Avatar asked Dec 18 '22 02:12

InvertedAcceleration


2 Answers

I know this question is a bit old, but I'm facing a similar problem and think I found a solution. There is an version of the ServiceHost constructor that takes a reference to the instance, rather than the type: http://msdn.microsoft.com/en-us/library/ms585487.aspx#Y342

So you can create the instance first, and then pass it to the ServiceHost constructor. I haven't tried this yet, but it looks like it should do the trick. Be sure to read the remarks about releasing the instance. when you use this constructor.

like image 51
Cesar Maiorino Avatar answered May 25 '23 01:05

Cesar Maiorino


You could make the service call itself...
The instance is indeed only created when a request comes in, you can't access what isn't there yet.

But seriously, try to work around this. What do you need it for that you can't do in the constructor of the implementing class?

like image 31
Henk Holterman Avatar answered May 25 '23 02:05

Henk Holterman