[英]How to pass an object to Web Service from its hosting Windows Service
I have a web service, say MyWebService
, and I need to host it on a windows service, say MyWindowsService
. 我有一个Web服务,例如
MyWebService
,我需要将其托管在Windows服务中,例如MyWindowsService
。 So in OnStart
method in MyWindowsService
, I start MyWebService
. 因此,在
MyWindowsService
OnStart
方法中,我启动MyWebService
。 When MyWindowsService
starts, I need to send a log message (from within MyWindowsService
) to a listener saying the application has been started. 当
MyWindowsService
启动时,我需要向MyWindowsService
发送一条日志消息(从MyWindowsService
内部),以表明应用程序已启动。 Such a log message is generated by a LogGenerator
. 这样的日志消息是由
LogGenerator
生成的。
In MyWebService
, it also needs to send log messages to the listener as well when certain events are triggered, and those log messages (sent from within MyWebService
) are also generated by LogGenerator
. 在
MyWebService
,当触发某些事件时,它还需要将日志消息发送到侦听器,并且这些日志消息(从MyWebService
内部发送)也由LogGenerator
生成。
The LogGenerator
has a few properties to be configured. LogGenerator
具有一些要配置的属性。 Now I am facing some issues. 现在我面临一些问题。
I do not know whether I need to construct an instance of LogGenerator
, configure this instance in MyWindowsService
, then pass it to MyWebService
. 我不知道是否需要构造
LogGenerator
实例,在MyWindowsService
配置该实例,然后将其传递给MyWebService
。 If so, then how I can pass an object to the web service from its hosting windows service. 如果是这样,那么如何将对象从其托管Windows服务传递给Web服务。 Or,
要么,
I construct an instance of LogGenerator
in MyWindowsService
and then I construct a second one in MyWebService
, so I do not need to pass any object to the web service. 我在
MyWindowsService
构造了LogGenerator
的实例,然后在MyWebService
构造了第二个实例,因此不需要将任何对象传递给Web服务。 Or, 要么,
I can make LogGenerator
and all its properties and methods static, so I do not need to create any instances at all. 我可以将
LogGenerator
及其所有属性和方法LogGenerator
静态,因此根本不需要创建任何实例。 If so, is it thread safe when I create log messages? 如果是这样,那么在创建日志消息时线程安全吗?
Thank you for any input !!! 谢谢您的任何投入!
You could use the singleton pattern when starting the web service to identify it and add event listeners on the windows service. 您可以在启动Web服务时使用单例模式来识别它,并在Windows服务上添加事件侦听器。 Of course, this only works if you have only one instance of the web service.
当然,这仅在您只有一个Web服务实例的情况下有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.