在Windows服务中调用Web服务 [英] Calling Web Service in a Windows Service

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

问题描述

我使用了一个简单的Windows服务来使方法在特定时间内工作,并且效果很好.在此之后,我已经尝试过:

I've used a simple windows service to make a method work in specific time and it works fine. Following that I've already tried:

protected override void OnStart(string[] args)
{
    this.WriteToFile("Simple Service started {0}");
    this.ScheduleService();
}

protected override void OnStop()
{
    this.WriteToFile("Simple Service stopped {0}");
    this.Schedular.Dispose();
}

private Timer Schedular;

public void ScheduleService()
{
    try
    {
        Schedular = new Timer(new TimerCallback(SchedularCallback));
        string mode = ConfigurationManager.AppSettings["Mode"].ToUpper();
        this.WriteToFile("Simple Service Mode: " + mode + " {0}");

        //Rest of the code here
    }
    catch(Exception ex)
    {
        WriteToFile("Simple Service Error on: {0} " + ex.Message + ex.StackTrace);

        //Stop the Windows Service.
        using (System.ServiceProcess.ServiceController serviceController = new System.ServiceProcess.ServiceController("SimpleService"))
        {
            serviceController.Stop();
        }
    }
}

这是在一个简单的Windows应用程序中完成的.所以我想做的是在Windows服务中调用Web服务(一种在特定时间运行的特定方法).我正在构建的应用程序是基于Web的,我有点困惑如何将Windows服务集成到其中?我是否需要其他选择或任何建议,将不胜感激.

This is done in a simple windows application. So what I am trying to do is to call a web service (A specific method to operate in a specific time) in a windows service. The application I am building is web-based and am little bit confused how would I integrate the windows service into it? Do I need any alternatives or any suggestions would be appreciated.

注意:我想知道是否需要在Web应用程序中为Windows服务创建另一个项目或以其他任何方式实现?

Note: What I would like to know is it required to create another project for windows service in the web application or any other way to implement?

推荐答案

回答您对我的评论的疑问;

In answer to your query on my comment;

另一种方法是使用IIS自动启动网站来定义Windows Service逻辑.IIS自动启动优于使用Windows服务,因为它包含所有IIS应用程序托管逻辑,包括自动重新启动和积极的资源管理.编写得不好的Windows服务可能会导致服务器宕机,但是ASP.net IIS托管的应用程序要拆除其主机会花费很多时间(这几乎是不可能的).

Another approach is to use an IIS Auto-Start website contaning your Windows Service logic. The IIS Auto-start is supierior to using a Windows Service as it contains all the IIS application hosting logic including auto-restart, and aggressive resource management. A poorly written Windows Service can take down a Server but it takes a lot for an ASP.net IIS hosted application to take down its host (its almost impossible).

您的自动启动网站不需要向外界显示-它只需要一个内部计时器即可在启动时保持活动状态.请注意,由于各种原因,IIS可能会启动和停止Web应用程序.但是结果是只要您的其他Web服务应用程序正在运行,它就会运行.内部计时器可以等待特定的时间来执行调用第二个Web服务所需的逻辑.

Your Auto-Start website need not be visibile to the outside world - it just needs to have an internal timer that keeps it alive when it starts up. Note that the web application might be started and stopped by IIS for various reasons; but the outcome is that it will be running whenever your other web service application is running. The internal timer can wait for a specific time to execute the logic you need to call your second web service.

要记住的关键是Windows服务被设计为由Windows托管并持续运行的应用程序.IIS应用程序旨在由Windows运行,但仅在被调用时运行.IIS自动启动网站概念使您可以提供一个持续运行"的网站,但由强大的IIS应用程序托管组件托管,而不是直接作为OS进程运行.

The key thing to remember is that a Windows Service is designed to be an application that is hosted by Windows and is continually running. An IIS application is designed to be run by Windows but runs only when called. The IIS Auto-Start website concept allows you to provide a "continually running" website but hosted by the robust IIS application hosting components, instead of it running directly as an OS process.

通常人们之所以不这样做,是因为他们要么对此一无所知,要么想避免需要IIS基础结构来运行"Windows Service"类型的应用程序,但是对于您而言,您已经支付了使用IIS来托管第二个应用程序的费用.Web服务,因此您最好充分利用IIS(并避免第二种技术堆栈和Windows Service部署的部署麻烦).

Generally people dont do this because either they dont know about it, or want to avoid needing the IIS infrastructure to run "Windows Service" type applications, but in your case you have already paid the cost of using IIS to host your second web service, so you may as well make full use of IIS (and avoid the second technology stack and deployment headaches of Windows Service deployment).

因此,我建议您根据情况优先使用IIS自动启动而不是Windows服务;

So I suggest using an IIS Auto Start in preference to a Windows Service in your situation because;

  1. 您只需要在解决方案中的技术堆栈上使用,这就是您的OP所要求的
  2. IIS对所有应用程序进行主动资源管理,如果应用程序无法正常运行,则终止它们,并在必要时重新启动.Windows服务不具备该功能.
  3. 您的基于IIS的服务代码可以XCOPY部署,而目标计算机上没有管理员访问凭据.
  4. 您的IIS服务可进行热升级,而无需操作系统级别的管理员权限-IIS无需进行任何操作即可处理升级时的停止和重新启动.
  1. You only need to use on tech stack in your solution, which was what your OP was asking about
  2. IIS carries out active resource management on all its applications, terminating, restarting as neccessary if they become non-functional. Windows Services do not have that capability.
  3. Your IIS based service code is XCOPY deployable with no administrator access credentials on the target machine.
  4. Your IIS service is hot upgradeable without needing OS level administrator rights - IIS handles the stopping and restarting on upgrade without you needing to do anything.

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

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