简体   繁体   English

将.asmx Web服务作为Windows服务运行

[英]Running a .asmx Web service as a Windows Service

I'm a total novice on asking questions on this forum - I hope I ask this question correctly. 我是在这个论坛上提问的新手-希望我能正确提问。

The problem I'm trying to tackle is this: I have a Visual Studio 2010 project which is for a Web Service. 我要解决的问题是:我有一个针对Web服务的Visual Studio 2010项目。 The class which does the work is defined in a ".asmx" file. 工作的类在“ .asmx”文件中定义。 The class inherits from the "System.Web.Services.WebService" class. 该类继承自“ System.Web.Services.WebService”类。

It runs happily on IIS on a server. 它可以在服务器上的IIS上愉快地运行。 Clients can make SOAP calls to the Web Service and it actions the requests and sends a response. 客户端可以对Web服务进行SOAP调用,它可以处理请求并发送响应。 That's all fine. 没关系的

The problem is that I want it to run on a Windows 7 PC on which I can't install IIS. 问题是我希望它在无法安装IIS的Windows 7 PC上运行。 So I wish it to run as a Windows Service (if possible). 因此,我希望它可以作为Windows服务运行(如果可能)。 The only "client" of the service will be the user on that PC. 该服务的唯一“客户端”将是该PC上的用户。

In other words, I wish to run all the code that is in my .asmx as a Windows Service (if possible). 换句话说,我希望将.asmx中的所有代码作为Windows服务运行(如果可能)。

In an ideal world I'd be able to re-use my existing .asmx (if possible), and not have to copy and paste all my code across to a new Windows Service solution. 在理想的情况下,我将能够重复使用现有的.asmx(如果可能),而不必将所有代码复制并粘贴到新的Windows Service解决方案中。

So my goal is to find the up-to-date way of running an .asmx Web Service as a Windows Service. 因此,我的目标是找到将.asmx Web服务作为Windows服务运行的最新方法。 I'm hoping someone could point me in the right direction on how I accomplish this. 我希望有人可以为我指出正确的方向。

I've done quite a bit of searching, and it looks as though this is very much possible to do. 我已经做了很多搜索,看起来这很有可能做到。 The problem is that a lot of solutions out there, appear to be slightly out of date. 问题在于,那里的许多解决方案似乎有些过时了。 Or I've got my concepts rather mixed up. 还是我的概念比较混乱。

Based on " How can I deploy my WCF Service without IIS? ", I've had a look at a few things. 基于“ 如何没有IIS的情况下部署WCF服务? ”,我看了几件事。

The closest I've come is this: " https://msdn.microsoft.com/en-us/library/ms751414.aspx ". 我最接近的是:“ https://msdn.microsoft.com/en-us/library/ms751414.aspx ”。 However, in this example, the actual class which does the work (the calculator) is actually contained in the service itself. 但是,在此示例中,完成工作的实际类(计算器)实际上包含在服务本身中。 I would have thought you could have separated them out. 我本以为你可以把他们分开。 In other words, have the service create instances of a class that comes from my .asmx file. 换句话说,让服务创建来自我的.asmx文件的类的实例。

The second closes I've come across is " https://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic4 ", however this is .NET 3.0 and VS 2005. I'm also struggling to understand how to link my existing class into the service. 我遇到的第二次关闭是“ https://msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic4 ”,但这是.NET 3.0和VS2005。我也很难理解如何将我现有的课程链接到服务。

The other solutions I encounter suggest adding references to "Microsoft.Web.Services3.dll", "System.Web.dll", and "System.Web.Services.dll", although a few people seem to think this is called WSE which was updated to WCF in .net 4.0 and so I should not be using it. 我遇到的其他解决方案建议添加对“ Microsoft.Web.Services3.dll”,“ System.Web.dll”和“ System.Web.Services.dll”的引用,尽管一些人似乎认为这称为WSE,已在.net 4.0中更新为WCF,所以我不应该使用它。

Any pointers as to where I get started? 关于我从哪里开始的任何指示?

In an ideal world, you would switch to WCF, as it can easily self-host, host in a service, etc. And there are plenty of examples out there. 在理想的世界中,您将切换到WCF,因为它可以轻松地自托管,托管服务等。并且这里有很多示例。 It is easy enough to migrate the code from ASMX to WCF. 将代码从ASMX迁移到WCF很容易。 But this is 3.5, as NetFX (WCF, WF and WPF were released under 3.5). 但这是3.5,因为NetFX(WCF,WF和WPF在3.5下发布)。

With ASMX, you need a server to host with. 使用ASMX,您需要托管服务器。 You can create a simple "server" with the HttpListener class. 您可以使用HttpListener类创建一个简单的“服务器”。 This may be heavy enough for your purpose. 这可能足以满足您的目的。

https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.85).aspx https://msdn.microsoft.com/zh-CN/library/system.net.httplistener(v=vs.85).aspx

IIS now has a hostable core, which can be used with newer versions of .NET, but if you cannot install the newer versions and have to leave the client machines at 3.0, it is probably not your best option. IIS现在具有可托管的内核,可以与.NET的较新版本一起使用,但是如果您无法安装较新的版本,而不得不将客户端计算机的版本设置为3.0,则它可能不是您的最佳选择。

You may be able to dig up old source for Cassini, which was Microsoft's implementation that eventually became the hostable web core. 您也许可以挖掘Cassini的旧资源,这是Microsoft的实现,最终成为可托管的Web核心。

Back in 2008, someone snagged the server in VS, which might be an option. 早在2008年,有人在VS中抢夺了服务器,这也许是一个选择。 Note that the version on this page is for VS 2008, which means 4.0: http://www.codeproject.com/Articles/23939/Light-IIS-Run-Asp-net-without-IIS 请注意,此页面上的版本适用于VS 2008,即4.0: http : //www.codeproject.com/Articles/23939/Light-IIS-Run-Asp-net-without-IIS

Ultidev has a server that is reported to self-host: http://ultidev.com/download/default.aspx Ultidev有一个报告为自托管的服务器: http ://ultidev.com/download/default.aspx

Hope one of these helps. 希望这些帮助之一。

Just for completeness, here's how I solved this: 为了完整起见,这是我解决的方法:

In the end I went with running IIS on client PCs and just continued using the asmx. 最后,我继续在客户端PC上运行IIS,然后继续使用asmx。

I found that converting the asmx to WCF to be quite tricky. 我发现将asmx转换为WCF非常棘手。 To be precise, it was the thread handling within the asmx (thread handling coded by someone else), that I found to be quite hard to convert. 确切地说,我发现很难转换asmx中的线程处理(其他人编码的线程处理)。 To explain further the asmx which I was trying to convert, did all its work in a class which inherited from "System.Web.Services.WebService". 为了进一步解释我试图转换的asmx,它的所有工作都在继承自“ System.Web.Services.WebService”的类中进行。 It could therefore, create an "Application" instance, which it then used to do its thread management. 因此,它可以创建一个“应用程序”实例,然后将其用于执行线程管理。

So rather than try and do the same thing, using "System.ServiceModel" (which doesn't allow me to create an "Application" instance), I thought it easier to just to use IIS on the client PCs. 因此,与使用“ System.ServiceModel”(不允许我创建“ Application”实例)来尝试执行相同的操作相比,我认为在客户端PC上仅使用IIS会更容易。 I figured this was a better use of time than recoding a fairly complex asmx. 我认为这比重新编码相当复杂的asmx更好地利用了时间。 A bit defeatist I know. 我知道有点失败主义者。

Kind regards to all. 致以诚挚的问候。

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

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