简体   繁体   English

在Windows服务中调用Web服务

[英]Calling Web Service in a Windows Service

I've used a simple windows service to make a method work in specific time and it works fine. 我使用了一个简单的Windows服务来使方法在特定时间工作,它工作正常。 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();
        }
    }
}

This is done in a simple windows application. 这是在一个简单的Windows应用程序中完成的。 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. 所以我要做的是在Windows服务中调用Web服务(在特定时间运行的特定方法)。 The application I am building is web-based and am little bit confused how would I integrate the windows service into it? 我正在构建的应用程序是基于Web的,我有点困惑如何将Windows服务集成到其中? Do I need any alternatives or any suggestions would be appreciated. 我是否需要任何替代方案或任何建议将不胜感激。

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

To call a web service from a Windows Service application, you would first generate a DLL from that web service, then instantiate its namespace. 要从Windows服务应用程序调用Web服务,首先要从该Web服务生成DLL,然后实例化其命名空间。 Assuming you have the code for that web service and/or know its namespace, you can perform these commands to do this: 假设您拥有该Web服务的代码和/或知道其命名空间,您可以执行以下命令来执行此操作:

  1. Perform these lines on a command line: 在命令行上执行以下行:

    cd C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools
    wsdl /l:CS /protocol:SOAP %svc%?WSDL

    where %svc% is the URL for your web service, ie http://localhost:777/MyWebService.asmx 其中%svc%是您的Web服务的URL,即http://localhost:777/MyWebService.asmx

    If the code is in VB instead of C#, change /l:CS to /l:VB . 如果代码是VB而不是C#,则将/l:CS更改为/l:VB This will output a proxy class file that can be converted to a DLL. 这将输出可以转换为DLL的代理类文件。

  2. Move the MyWebService.cs file from C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools to the C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\ directory. MyWebService.cs文件从C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 ToolsC:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\目录。

  3. Run these two commands on the command line: 在命令行上运行以下两个命令:
    cd C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727
    csc /t:library %name%.cs /reference:System.Web.Services.dll /optimize

    where %name% is the name of the class (without the .cs , since the command will append this). 其中%name%是类的名称(没有.cs ,因为命令会附加这个)。 In our case, we'd use MyWebService . 在我们的例子中,我们使用MyWebService (Change .cs to .vb for a VB class.) (对于VB类,将.cs更改为.vb。)

  4. Navigate to C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 via Windows Explorer. 通过Windows资源管理器导航到C:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727 You should see a DLL created in that folder with the name of the class ( MyWebService.dll ). 您应该看到在该文件夹中创建的DLL具有类的名称( MyWebService.dll )。 Copy this file to the bin folder of your Service project. 将此文件复制到Service项目的bin文件夹中。 You will need to set the bin folder to be included in your project, and right-click the folder to Add > Existing Item . 您需要将bin文件夹设置为包含在项目中,然后右键单击该文件夹以添加>现有项目 Select the DLL. 选择DLL。 Once imported, select the DLL and change its properties to: 导入后,选择DLL并将其属性更改为:

    Build Action: Content 构建行动:内容
    Copy to Output Directory: Copy if newer (or Copy always, as you prefer) 复制到输出目录:如果更新则复制(或根据需要复制)

  5. Right-click References > Add References . 右键单击“ 引用”>“添加引用” Navigate to the DLL in the bin folder for your web service. 导航到Web服务的bin文件夹中的DLL。

  6. Right-click References > Add Service References . 右键单击“ 引用”>“添加服务引用” Assuming your web service is running, take its full URL (ie http://localhost:777/MyWebService.asmx ) and put that on the Address line. 假设您的Web服务正在运行,请获取其完整URL(即http://localhost:777/MyWebService.asmx )并将其放在地址行上。 In the Namespace textbox, give it something more meaningful than ServiceReference1 , but it should not be the same as MyWebService (the name/namespace of the ASMX file). 在Namespace文本框中,给它一些比ServiceReference1更有意义的东西,但它不应该与MyWebService (ASMX文件的名称/命名空间)相同。 Perhaps MWS . 也许是MWS

  7. Instantiate your web service in your Windows Service: 在Windows服务中实例化您的Web服务:

    MWS.MyWebServiceSoapClient webService = new MWS.MyWebServiceSoapClient();
    webService.Open();
    string someDataYouWant = webService.SomeMethodToGetData();
    webService.Close();

    Or you can probably do: 或者你可以这样做:

    MyWebService webService = new MyWebService();
    string someDataYouWant = webService.SomeMethodToGetData();
    webService.Dispose();

In answer to your query on my comment; 回答你对我的评论的询问;

Another approach is to use an IIS Auto-Start website contaning your Windows Service logic. 另一种方法是使用IIS自动启动网站来支持您的Windows服务逻辑。 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. IIS自动启动优先于使用Windows服务,因为它包含所有IIS应用程序托管逻辑,包括自动重启和积极的资源管理。 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). 写得不好的Windows服务可以取消服务器,但是ASP.net IIS托管的应用程序需要花费很多东西来取下它的主机(这几乎是不可能的)。

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; 请注意,由于各种原因,IIS可能会启动和停止Web应用程序; but the outcome is that it will be running whenever your other web service application is running. 但结果是,只要您的其他Web服务应用程序正在运行,它就会运行。 The internal timer can wait for a specific time to execute the logic you need to call your second web service. 内部计时器可以等待特定时间来执行调用第二个Web服务所需的逻辑。

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. 要记住的关键是Windows服务被设计为由Windows托管并且不断运行的应用程序。 An IIS application is designed to be run by Windows but runs only when called. IIS应用程序旨在由Windows运行,但仅在调用时运行。 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自动启动网站概念允许您提供“持续运行”的网站,但由强大的IIS应用程序托管组件托管,而不是直接作为操作系统进程运行。

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服务”类型的应用程序,但在您的情况下,您已经支付了使用IIS来托管您的第二个Web服务的成本,所以你也可以充分利用IIS(并避免Windows服务部署的第二个技术堆栈和部署难题)。

So I suggest using an IIS Auto Start in preference to a Windows Service in your situation because; 所以我建议在你的情况下使用IIS自动启动优先于Windows服务,因为;

  1. You only need to use on tech stack in your solution, which was what your OP was asking about 您只需要在解决方案中使用技术堆栈,这就是您的OP所询问的内容
  2. IIS carries out active resource management on all its applications, terminating, restarting as neccessary if they become non-functional. IIS对其所有应用程序执行活动资源管理,如果它们不起作用则终止,重新启动。 Windows Services do not have that capability. Windows服务没有该功能。
  3. Your IIS based service code is XCOPY deployable with no administrator access credentials on the target machine. 基于IIS的服务代码是XCOPY可部署的,目标计算机上没有管理员访问凭据。
  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. 您的IIS服务可以热升级而无需操作系统级别的管理员权限 - IIS可以在升级时处理停止和重新启动,而无需执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM