如何使用从单独项目创建的 windows 服务
How to use windows service created from a separate project
假设我有一个现有的 Windows 表单应用程序并且我创建了一个新的 Windows 服务项目。
windows 表单应用程序将作为我的应用程序的 UI,windows 服务包含一些我想稍后调用的方法。
我的问题是我如何才能从我的 windows 服务调用方法,因为它是从一个单独的项目创建的?应该采取什么方法?
假设 Windows 服务包含以下内容:
static Boolean connectionStatus = false;
protected override void OnStart(string[] args)
{
....
//Connect to Server
//Once connected change the "connectionStatus" to true
}
protected override void OnStop()
{
connectionStatus = false;
}
//If I want to check if this Service is Connected into Server, I just call this
public Boolean isConnected() {
return connectionStatus;
}
并且来自 Windows 表单应用程序,该应用程序与 Windows 服务分开的项目:
...
//Let's say I want to show a MessageBox containing if the Service is connected to Server
MyService myservice = new MyService();
MessageBox.Show("Client Status: " + myservice.isConnected());
将 connectionStatus 写入 file/DB 并从 UI 应用程序读取它(如果它们在同一台机器上)。否则,您应该考虑在 windows 服务中实现 WCF 服务。
简单的 WCF 实现
主要的初始 link 将指导您完成第一次实施。
根据你的问题,你会有一份合同
[ServiceContract]
public interface IConnService
{
[OperationContract]
bool IsConnected();
}
及其实现
public class ConnService: IConnService
{
public bool IsConnected()
{
return connectionStatus; //I don't understand if it is a static variable, sorry
}
}
然后您继续教程,构建项目并使用 installutil 设置服务(但如果您已经有服务,您就会知道)。
此时,您从普通互联网浏览器检查 WCF URL 服务(serviceModel
配置中的 baseAddress
)。
由于您在您的配置中还有一个所谓的 mex 端点,因此在互联网浏览器的响应中,您将找到一个简单、普通的 WCF 客户端 C# 示例:-)
在您的应用程序中尝试 MessageBox
行之前。
最后的笔记
让我提醒你我最初的评论。
It's unclear why you need a windows service. It looks like a
communication library is enough. You need a windows service if you
want to share those methods with other applications/servers or running
them also when the app/client is down: in that case (but it's not what
you are describing in this question) consider implementing a WCF
service
@Seynal
我认为我们的时区略有不同,但现在:
Have you ever tried to add your Service Project to the Forms
application and add reference to it? As here
msdn.microsoft.com/en-us/library/f3st0d45.aspx
假设我有一个现有的 Windows 表单应用程序并且我创建了一个新的 Windows 服务项目。
windows 表单应用程序将作为我的应用程序的 UI,windows 服务包含一些我想稍后调用的方法。
我的问题是我如何才能从我的 windows 服务调用方法,因为它是从一个单独的项目创建的?应该采取什么方法?
假设 Windows 服务包含以下内容:
static Boolean connectionStatus = false;
protected override void OnStart(string[] args)
{
....
//Connect to Server
//Once connected change the "connectionStatus" to true
}
protected override void OnStop()
{
connectionStatus = false;
}
//If I want to check if this Service is Connected into Server, I just call this
public Boolean isConnected() {
return connectionStatus;
}
并且来自 Windows 表单应用程序,该应用程序与 Windows 服务分开的项目:
...
//Let's say I want to show a MessageBox containing if the Service is connected to Server
MyService myservice = new MyService();
MessageBox.Show("Client Status: " + myservice.isConnected());
将 connectionStatus 写入 file/DB 并从 UI 应用程序读取它(如果它们在同一台机器上)。否则,您应该考虑在 windows 服务中实现 WCF 服务。
简单的 WCF 实现
主要的初始 link 将指导您完成第一次实施。
根据你的问题,你会有一份合同
[ServiceContract]
public interface IConnService
{
[OperationContract]
bool IsConnected();
}
及其实现
public class ConnService: IConnService
{
public bool IsConnected()
{
return connectionStatus; //I don't understand if it is a static variable, sorry
}
}
然后您继续教程,构建项目并使用 installutil 设置服务(但如果您已经有服务,您就会知道)。
此时,您从普通互联网浏览器检查 WCF URL 服务(serviceModel
配置中的 baseAddress
)。
由于您在您的配置中还有一个所谓的 mex 端点,因此在互联网浏览器的响应中,您将找到一个简单、普通的 WCF 客户端 C# 示例:-)
在您的应用程序中尝试 MessageBox
行之前。
最后的笔记
让我提醒你我最初的评论。
It's unclear why you need a windows service. It looks like a communication library is enough. You need a windows service if you want to share those methods with other applications/servers or running them also when the app/client is down: in that case (but it's not what you are describing in this question) consider implementing a WCF service
@Seynal 我认为我们的时区略有不同,但现在:
Have you ever tried to add your Service Project to the Forms application and add reference to it? As here msdn.microsoft.com/en-us/library/f3st0d45.aspx