Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize in WCF Service Application Project?

Tags:

c#

wcf

I have a Library that defines some static variable such as Database address, some static method that does data formatting.

My WCF Service will use that library and open database connection. The problem is, the static variable such as Database address in this library is not initialized, and have to initialize by reading the files on server when application start up.

In ASP.NET I could initialize everything in the Global.asax's Application_Start, in Silverlight's Client side I can initialize everything in App.xml's Application_Startup. But how do I initalize in WCF Service Application Project?

This WCF Service Application will be used with Silverlight, might contains mutiple WCF Service, if I initialize in the WCF Service constructor, then I have to do the same for every Service....

Thanks in advance,

King

like image 451
King Avatar asked Oct 11 '22 03:10

King


1 Answers

There are multiple ways of doing this. You can:

  1. Use a custom ServiceHostFactory which initializes the library before creating a ServiceHost.
  2. Use a static constructor (see http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx) in one of your classes inside the ServiceLibrary and make sure that this a type which somehow gets accessed (as this invokes the static constructor) before you need to use the library that requires initialization.
  3. Create a custom WCF ServiceBehavior which does the initialization, and apply that to all relevant services inside the ServiceLibrary. (IMHO, the most elegant way...)
like image 167
mthierba Avatar answered Nov 12 '22 17:11

mthierba