我想创建一个windows服务来验证数据,并从另一个windows应用程序访问它,但我对服务是新手,我不知道如何开始。
因此,当服务运行时,windows应用程序应该以某种方式连接到服务,发送一些数据,并得到一个响应,对或错。
发布于 2013-09-05 13:22:49
通过执行以下操作,我可以成功地处理(几乎)与您相同的问题:
在代表服务类的class : ServiceBase中,您可能具有:
public Class () //constructor, to create your log repository
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("YOURSource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"YOURSource", "YOURLog");
}
eventLog1.Source = "YOURSource";
eventLog1.Log = "YOURLog";
}
现在实现::
protected override void OnStart(string[] args)
{...}
和
protected override void OnStop()
{...}
要处理自定义命令调用:
protected override void OnCustomCommand(int command)
{
switch (command)
{
case 128:
eventLog1.WriteEntry("Command " + command + " successfully called.");
break;
default:
break;
}
}
现在,在将要调用服务的应用程序中使用以下代码:
引用您的方法的枚举:(请记住,服务自定义方法总是接收一个int32 (128到255)作为参数,使用枚举可以更容易地记住和控制您的方法
private enum YourMethods
{
methodX = 128
};
调用特定方法的步骤:
ServiceController sc = new ServiceController("YOURServiceName", Environment.MachineName);
ServiceControllerPermission scp = new ServiceControllerPermission(ServiceControllerPermissionAccess.Control, Environment.MachineName, "YOURServiceName");//this will grant permission to access the Service
scp.Assert();
sc.Refresh();
sc.ExecuteCommand((int)YourMethods.methodX);
通过执行此操作,您可以控制您的服务。
您可以查看如何创建和安装Here服务。关于ExecuteCommand方法的More。
祝好运!
发布于 2010-12-15 15:02:06
如果您使用的是.Net Framework4,那么内存映射文件提供了一种实现跨进程通信的相当简单的方法。
它相当简单,在文档中有很好的描述,并且避免了使用WCF或其他基于连接/远程的交互的开销(在运行时,但也避免了开发工作),或者将共享数据写入中央位置和轮询(数据库、文件等)。
有关概述,请参阅here。
发布于 2010-12-15 14:58:28
通过使服务宿主WCF服务并从您的应用程序连接到它,您可以非常容易地实现这一点。
https://stackoverflow.com/questions/4451216
复制相似问题