简体   繁体   中英

Initialize in WCF Service Application Project?

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

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...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM