在WCF服务应用程序项目初始化? [英] Initialize in WCF Service Application Project?

查看:563
本文介绍了在WCF服务应用程序项目初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些静态变量定义,如数据库地址,一些确实的数据格式静态方法库。

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

我的WCF服务将使用该库和开放式数据库连接。
中的问题是,如在此库数据库地址静态变量没有初始化,并具有通过读取服务器上的文件进行初始化时应用启动

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.

在ASP.NET我可以初始化在Global.asax的一切的Application_Start,在Silverlight中的客户端,我可以初始化app.xml中的Application_Startup一切。
,而我怎么在WCF服务应用程序项目initalize?

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?

这WCF服务应用程序将与Silverlight的使用,则可能包含多发WCF服务,我若在初始化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....

由于提前,

推荐答案

有这样做的多种方式。您可以:

There are multiple ways of doing this. You can:


  1. 使用它创建的ServiceHost之前初始化该库自定义ServiceHostFactory

  2. 使用静态构造函数(见 http://msdn.microsoft.com/en-us/库/ k9x6w0hc.aspx )在你的类ServiceLibrary里面的一个,并确保这个莫名其妙地被访问(因为这调用静态构造函数),你需要使用需要初始化图书馆前一种类型。

  3. 创建一个自定义的WCF ServiceBehavior由它来完成初始化,并应用到ServiceLibrary内的所有相关服务。 (恕我直言,最优雅的方式......)

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

这篇关于在WCF服务应用程序项目初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆