简体   繁体   English

在WCF服务应用程序项目中初始化?

[英]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. 我的WCF服务将使用该库并打开数据库连接。 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. 在ASP.NET中我可以初始化Global.asax的Application_Start中的所有内容,在Silverlight的Client端我可以初始化App.xml的Application_Startup中的所有内容。 But how do I initalize in WCF Service Application Project? 但是我如何在WCF服务应用项目中初始化?

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.... 这个WCF服务应用程序将与Silverlight一起使用,可能包含多个WCF服务,如果我在WCF服务构造函数中初始化,那么我必须对每个服务执行相同的操作....

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. 使用自定义ServiceHostFactory在创建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. 在ServiceLibrary中的一个类中使用静态构造函数(请参阅http://msdn.microsoft.com/en-us/library/k9x6w0hc.aspx ),并确保这是一个以某种方式被访问的类型(因为这会调用静态构造函数)之前需要使用需要初始化的库。
  3. Create a custom WCF ServiceBehavior which does the initialization, and apply that to all relevant services inside the ServiceLibrary. 创建一个执行初始化的自定义WCF ServiceBehavior,并将其应用于ServiceLibrary中的所有相关服务。 (IMHO, the most elegant way...) (恕我直言,最优雅的方式...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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