从Web应用程序Asp.Net调用Windows服务 [英] Calling a windows service from web application Asp.Net

查看:529
本文介绍了从Web应用程序Asp.Net调用Windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从asp.net调用windows服务我使用下面的代码但是当我运行它时说:



无法控制服务名称计算机上的服务计算机名



I want to call a windows service from asp.net i use below code but when i run it says :

Cannot control "Service Name" service on computer "Computer Name"

//Asp.net
ServiceController service = new ServiceController("ABPS");
service.ExecuteCommand(1);

//Service
protected override void OnCustomCommand(int command)
        {
            base.OnCustomCommand(command);
            if (command == 1)
            {
                //Do something
            }//if
        }

推荐答案

请参阅我的评论这个问题。您的ASP.NET没有使用服务控制器的权限?这样做有一些很好的理由。通常,Web应用程序在沙盒环境中执行,这会阻止访问主机系统的其余部分。这是Web服务器的安全关键架构特性。



整个想法都是错误的。即使某些应用程序可以通过服务控制器启动和停止服务,但这并不是整个设计的方式。该服务实际上是为了永久执行(通常是自动启动),并且通常,其线程花费大部分时间处于等待状态,而不是浪费任何CPU时间,直到某些IPC方法接收到服务请求为止。支持等待状态的线程阻塞。这可能是通过套接字,命名管道等发送一些数据块。请求唤醒线程,服务完成其工作并再次休眠。这就是为什么永远不需要停止和启动它。



在请求中启动和停止的应用程序通常不是服务而是常规应用程序。此类应用程序可能在您的网站上,这不会导致您的权限问题。更好的是,它可能只是你的一些页面代码的一部分。什么,这不能做服务可以做什么?但我已经解释过:Web服务旨在保护其主机系统免受您在网站上的艺术影响......因此,如果您解释了活动的最终目的,您可能有机会获得更具体的建议。



-SA
Please see my comment to the question. Your ASP.NET does not have permissions to use the service controller? This is done for some good reasons. Normally a Web application is executed in a sandboxed environment which prevents accessing the rest of the host system. This is safety-critical architectural feature of the Web server.

The whole idea is wrong. Even though some application can start and stop a service through the service controller, this is not how the whole thing is designed. The service is really designed to be executed permanently (most usually, it is automatically started) and, very typically, its threads spend most time in a wait state, not wasting any CPU time, until a service request is received by some IPC method which supports thread blocking with wait state. This could be sending some block of data through a socket, named pipe, etc. The request wakes the thread up, the service does its work and sleeps again. That's why stopping and starting it is never needed.

The application which are started and stopped on requests are usually not services but regular applications. Such applications could be on your site, which would not cause your permission problem. Better yes, it could be just a part of you code behind of some of your pages. What, this cannot do what a service could do? But I already explained: the Web service is designed to protect its host system from your artistry on the site… So, you may have a chance to get more specific advice if you explain the ultimate purpose of you activity.

—SA


这篇关于从Web应用程序Asp.Net调用Windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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