如何使用参数制作Windows服务? [英] How to make a Windows service with parameters?

查看:37
本文介绍了如何使用参数制作Windows服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经编写了Windows服务,我希望每个客户有1个实例在运行.这是因为每个客户都有各自的具有相同架构的数据库;Windows服务之间的唯一区别是,它们各自将具有与指定服务的客户数据库相对应的不同参数.(而且我无法通过多个工作线程来提供一项服务,因为数据库连接使用了一个静态变量,而我无法在多个线程之间进行调皮.)

I have written a Windows service, of which I want to have 1 instance running per customer. This is because the customers each have their own DB with identical schemas; the only difference between the Windows services is that they will each have a different parameter corresponding to the customer DB that they're designated to serve. (And I can't have one service with multiple worker threads, because the DB connection uses a static variable, which I can't fiddle with across threads.)

我发现了这个简洁的小教程关于如何制作Windows服务的信息,但是它仅向我展示了如何为单个服务进行设置.我想设置服务的 n 个实例,每个实例的显示名称都包括客户名称,并使用表示客户ID的命令行参数运行.

I found this neat little tutorial about how to make a Windows Service, but it only shows me how to set it up for a single service. I want to set up n instances of the service, each one with a display name that includes the customer name, running with the command line parameter that denotes the customer ID.

上面链接的教程有一个名为 MyWindowsServiceInstaller 的类,该类将Windows服务安装在本地系统上,我猜这将是设置 foreach 遍历我所有的客户,为每个客户设置一项服务.但是我在提供的界面上看不到任何地方,无法让我为新服务设置命令行参数.

The tutorial linked above has a class called MyWindowsServiceInstaller, which installs the windows service on the local system, and I'm guessing this would be a logical place to set up a foreach loop through all my customers, setting up one service for each. But I can't see anywhere on the interfaces provided that would allow me to set up a command line parameter for the new service.

你如何做到的?

推荐答案

WilPeck写了一篇很好的文章 ,内容涉及如何在一个盒子上安装Windows服务的多个实例.基本思想是,必须给安装程序起不同的名称,以诱使安装程序认为它们是不同的服务.

Wil Peck wrote a good article about how to install multiple instances of a windows service on a single box. The basic idea is that you have to trick the installer into thinking they are different services by giving them different names.

话虽如此,重新设计数据库连接代码以使其可以支持多个工作线程似乎更容易(并且更易于维护).

Having said that, it seems like it would be easier (and more maintainable) to redesign your database connection code so that it can support multiple worker threads.

这篇关于如何使用参数制作Windows服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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