I am hosting a service within a Windows Service.
我在Windows服务中托管服务。
The following snippet instantiates the ServiceHost object:
以下代码段实例化ServiceHost对象:
Host = new ServiceHost(typeof(Services.DocumentInfoService));
The DocumentInfoService class implements a contract interface that has methods that invoke business objects requiring initialization (actually a connection string). Ideally, I'd like the hosting process to get the connection string from the config file and pass it to a constructor for my service object, DocumentInfoService, which would hold onto it and use it to pass to business objects as needed.
DocumentInfoService类实现一个契约接口,该接口具有调用需要初始化的业务对象(实际上是连接字符串)的方法。理想情况下,我希望托管过程从配置文件中获取连接字符串,并将其传递给我的服务对象DocumentInfoService的构造函数,该构造函数将保留在其上并根据需要使用它传递给业务对象。
However, the ServiceHost constructor takes a System.Type object -- so instances of DocumentInfoService are created via the default constructor. I did note that there is another constructor method for ServiceHost that takes an object instance -- but the docs indicate that is for use with singletons.
但是,ServiceHost构造函数采用System.Type对象 - 因此DocumentInfoService的实例是通过默认构造函数创建的。我确实注意到ServiceHost还有另一个构造函数方法,它接受一个对象实例 - 但文档表明它与单例一起使用。
Is there a way for me to get to my object after it is constructed so that I can pass it some initialization data?
有没有办法让我在构造它之后到达我的对象,以便我可以传递一些初始化数据?
2 个解决方案
#1
4
ServiceHost will create the service instances based on the binding and behaviors configured for the endpoint. There's no particular point in time, where you can rely there is a service instance. Hence, ServiceHost does not expose the service instances.
ServiceHost将根据为端点配置的绑定和行为创建服务实例。没有特定的时间点,您可以依赖于服务实例。因此,ServiceHost不会公开服务实例。
What you could do is add code to your service object constructor to read the relevant configuration values itself through the ConfigurationManager class.
您可以做的是将代码添加到服务对象构造函数中,以通过ConfigurationManager类读取相关的配置值。
Of course, if you don't keep your configuration in the app.config, that won't work for you. Alternative approach would be to have a well-known singleton object that the service instances access when created to get the necessary configuration.
当然,如果你没有在app.config中保留你的配置,那将不适合你。替代方法是拥有一个众所周知的单例对象,服务实例在创建时访问该对象以获得必要的配置。
And there's also the option of creating your own ServiceHost or your own ServiceHostFactory to control the service instantiation explicitly. This would give you acess to the new service instances at the moment of creation. I would stay away from that option though. It's not worth the effort for your scenario.
此外,还可以选择创建自己的ServiceHost或自己的ServiceHostFactory来显式控制服务实例化。这将使您在创建时获得新服务实例。我会远离那个选项。对你的场景来说,这不值得。
#2
3
Implement your own ServiceHost
. See also http://hyperthink.net/blog/servicehostfactory-vs-servicehostfactorybase/
实现自己的ServiceHost。另见http://hyperthink.net/blog/servicehostfactory-vs-servicehostfactorybase/
#1
4
ServiceHost will create the service instances based on the binding and behaviors configured for the endpoint. There's no particular point in time, where you can rely there is a service instance. Hence, ServiceHost does not expose the service instances.
ServiceHost将根据为端点配置的绑定和行为创建服务实例。没有特定的时间点,您可以依赖于服务实例。因此,ServiceHost不会公开服务实例。
What you could do is add code to your service object constructor to read the relevant configuration values itself through the ConfigurationManager class.
您可以做的是将代码添加到服务对象构造函数中,以通过ConfigurationManager类读取相关的配置值。
Of course, if you don't keep your configuration in the app.config, that won't work for you. Alternative approach would be to have a well-known singleton object that the service instances access when created to get the necessary configuration.
当然,如果你没有在app.config中保留你的配置,那将不适合你。替代方法是拥有一个众所周知的单例对象,服务实例在创建时访问该对象以获得必要的配置。
And there's also the option of creating your own ServiceHost or your own ServiceHostFactory to control the service instantiation explicitly. This would give you acess to the new service instances at the moment of creation. I would stay away from that option though. It's not worth the effort for your scenario.
此外,还可以选择创建自己的ServiceHost或自己的ServiceHostFactory来显式控制服务实例化。这将使您在创建时获得新服务实例。我会远离那个选项。对你的场景来说,这不值得。
#2
3
Implement your own ServiceHost
. See also http://hyperthink.net/blog/servicehostfactory-vs-servicehostfactorybase/
实现自己的ServiceHost。另见http://hyperthink.net/blog/servicehostfactory-vs-servicehostfactorybase/