以编程方式启动和停止 IIS Express
我正在尝试用 C# 构建一个小型应用程序,它应该启动/停止 IIS Express 工作进程。为此,我想使用 MSDN 上记录的官方“IIS Express API”: http://msdn.microsoft.com/en-us/library/gg418415.aspx
据我了解,API(仅)基于 COM 接口。为了使用这个 COM 接口,我通过 Add Reference -> 添加了对 VS2010 中 COM 库的引用。通讯-> “IIS 安装版本管理器界面”:
到目前为止一切顺利,但下一步是什么?有一个可用的 IIISExprProcessUtility
接口,其中包括启动/停止 IIS 进程的两种“方法”。我必须编写一个实现该接口的类吗?
public class test : IISVersionManagerLibrary.IIISExprProcessUtility
{
public string ConstructCommandLine(string bstrSite, string bstrApplication, string bstrApplicationPool, string bstrConfigPath)
{
throw new NotImplementedException();
}
public uint GetRunningProcessForSite(string bstrSite, string bstrApplication, string bstrApplicationPool, string bstrConfigPath)
{
throw new NotImplementedException();
}
public void StopProcess(uint dwPid)
{
throw new NotImplementedException();
}
}
如您所见,我不是专业开发人员。有人能指出我正确的方向吗? 非常感谢任何帮助。
更新1: 根据建议,我尝试了以下代码,不幸的是它不起作用:
好的,它可以实例化,但我看不到如何使用这个对象...
IISVersionManagerLibrary.IIISExpressProcessUtility test3 = (IISVersionManagerLibrary.IIISExpressProcessUtility) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("5A081F08-E4FA-45CC-A8EA-5C8A7B51727C")));
Exception: Retrieving the COM class factory for component with CLSID {5A081F08-E4FA-45CC-A8EA-5C8A7B51727C} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
I am trying to build a small application in C# which should start/stop an IIS Express worker process. For this purpose I want to use the official "IIS Express API" which is documented on MSDN: http://msdn.microsoft.com/en-us/library/gg418415.aspx
As far as I understand, the API is based (only) on COM interfaces. To use this COM interfaces I've added a reference to the COM library in VS2010 via Add Reference -> COM -> "IIS Installed Versions Manager Interface":
So far so good, but what's next? There is an IIISExprProcessUtility
interface available which includes the the two "methods" to start/stop an IIS process. Do I have to write a class which implements this interface?
public class test : IISVersionManagerLibrary.IIISExprProcessUtility
{
public string ConstructCommandLine(string bstrSite, string bstrApplication, string bstrApplicationPool, string bstrConfigPath)
{
throw new NotImplementedException();
}
public uint GetRunningProcessForSite(string bstrSite, string bstrApplication, string bstrApplicationPool, string bstrConfigPath)
{
throw new NotImplementedException();
}
public void StopProcess(uint dwPid)
{
throw new NotImplementedException();
}
}
As you can see, I'm not a professional developer. Can someone point me in the right direction.
Any help is greatly appreciated.
Update 1:
According to the suggestions I've tried the following code which unfortunately doesn't work:
Ok, it can be instantiated but I cannot see how to use this object...
IISVersionManagerLibrary.IIISExpressProcessUtility test3 = (IISVersionManagerLibrary.IIISExpressProcessUtility) Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("5A081F08-E4FA-45CC-A8EA-5C8A7B51727C")));
Exception: Retrieving the COM class factory for component with CLSID {5A081F08-E4FA-45CC-A8EA-5C8A7B51727C} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我试图做类似的事情。我的结论是微软提供的COM库是不完整的。我不使用它,因为文档提到“注意:本主题是预发布文档,在未来版本中可能会发生变化”。
因此,我决定看看 IISExpressTray.exe 正在做什么。似乎也在做类似的事情。
我反汇编了 IISExpressTray.dll,发现列出所有 IISexpress 进程并停止 IISexpress 进程并没有什么神奇之处。
它不调用该 COM 库。它不会从注册表中查找任何内容。
所以,我最终得到的解决方案非常简单。要启动 IIS Express 进程,我只需使用 Process.Start() 并传入我需要的所有参数。
为了停止 IIS Express 进程,我使用 Reflector 从 IISExpressTray.dll 复制了代码。我看到它只是向目标 IISExpress 进程发送一条 WM_QUIT 消息。
这是我编写的用于启动和停止 IIS Express 进程的类。希望这可以帮助其他人。
我不需要列出所有现有的 IIS Express 进程。如果您需要的话,从我在反射器中看到的情况来看,IISExpressTray.dll 所做的是调用 Process.GetProcessByName("iisexpress", ".")
要使用我提供的类,这里有一个我用来测试它的示例程序。
这可能不是您问题的答案,但我认为对您的问题感兴趣的人可能会发现我的工作有用。请随意改进代码。有些地方您可能需要增强。
I was trying to do similar thing. I concluded that the COM library provided by Microsoft is incomplete. I don't use it because the doc mentioned that "Note: This topic is pre-release documentation and is subject to change in future releases".
So, I decided to take a look at what IISExpressTray.exe is doing. It seems to be doing similar things.
I disassemble the IISExpressTray.dll and found that there is no magic in listing out all the IISexpress processes and stoping the IISexpress process.
It doesn't call that COM library. It doesn't lookup anything from registry.
So, the solution I ended up is very simple. To start an IIS express process, I just use Process.Start() and pass in all the parameters I need.
To stop an IIS express process, I copied the code from IISExpressTray.dll using reflector. I saw it simply sends a WM_QUIT message to the target IISExpress process.
Here is the class I wrote to start and stop an IIS express process. Hope this can help somebody else.
I don't need to list all the existing IIS express process. If you need that, from what I saw in the reflector, what IISExpressTray.dll does is to call
Process.GetProcessByName("iisexpress", ".")
To use the class I provided, here is a sample program I used to test it.
This may not be an answer to your question but I think people interesting in your question may find my work useful. Feel free to improve the codes. There are some places that you might want to enhance.
虽然为时已晚,但我会回答这个问题。
就是这样。然后您可以在 util 对象上调用 StopProcess 方法。
但是,您必须得到 Microsoft 的通知。
Although, it's too late, I'll provide an answer to this question.
That's it. Then you can call StopProcess method on util object.
However, you have to get notice from Microsoft.
此实现适用于以编程方式启动/停止 IIS Express,可以在测试中使用。
This implementation works for starting/stopping IIS Express programmatically, can be used from tests.
我感觉你做事很艰难。从这个问题中获取提示 自动停止/重新启动 ASP.NET 开发Server on Build 并查看是否可以采用相同的流程。
回答你的问题,我认为 pinvoke.net 可能对你有帮助。他们还有很多示例可以帮助您构建解决方案。
I feel you are doing it in a hard way. Take a hint from this question Automatically stop/restart ASP.NET Development Server on Build and see if you can adopt the same process.
Answering your question, I think pinvoke.net might help you. They have lot of examples as well which can help you build your solution.
Harvey Kwok 提供了一个很好的提示,因为我想在运行集成测试用例时拆除和拆除服务。但 Harvey 代码对于 PInvoke 和消息传递来说太长了。
这是一个替代方案。
在我的 MS Test 集成测试套件中,我有
Harvey Kwok had provided a good hint, since I want to tear up and tear down the service when running integration test cases. But Harvey codes is too long with PInvoke and messaging.
Here's an alternative.
And in my integration test suit with MS Test, I have
这也是我的解决方案。它运行带有隐藏窗口的 IIS Express。 Manager 类控制多个 IIS Express 实例。
我需要几个实例。设计了manager类来控制它们。
Here is my solution too. It runs IIS Express with hidden windows. Manager class controls several IIS Express instances.
I need several instances. Designed manager class to control them.
我想我也会把我的解决方案放在这里。源自 SeongTae Jeong 的解决方案和另一篇文章(现在不记得在哪里了)。
Microsoft.Web.Administration
nuget。IIS Installed Versions Manager Interface
COM 类型库。添加以下类:
按如下方式设置测试装置。该路径相对于测试套件的 bin 文件夹。
<前><代码>[测试夹具]
公开课测试
{
私人网站_网站;
[测试夹具设置]
公共无效设置()
{
_website = Website.Create(@"..\..\..\TestHarness");
_website.Start();
}
[测试夹具拆解]
公共无效拆解()
{
_website.Stop();
}
[测试]
公共无效should_serialize_with_bender()
{
new WebClient().UploadString(_website.Url, "hai").ShouldEqual("hai");
}
}
如果这也将在构建服务器上运行,那么还有一点。首先,您需要在构建服务器上安装 IIS Express。其次,您必须在构建服务器上创建一个
applicationhost.config
。您可以从开发盒中的C:\Users\\Documents\IISExpress\config\
下复制一份。需要将其复制到构建服务器运行的用户的相应路径。如果它作为系统运行,则路径将为 C:\Windows\System32\config\systemprofile\Documents\IISExpress\config\。Figure I'd throw my solution in here too. Derived from the SeongTae Jeong's solution and another post (Can't remember where now).
Microsoft.Web.Administration
nuget.IIS Installed Versions Manager Interface
COM type library as noted above.Add the following class:
Setup your test fixture as follows. The path is relative to the bin folder of your test suite.
And one more point if this is going to also run on a build server. First you will need to install IIS Express on the build server. Second, you'll have to create an
applicationhost.config
on the build server. You can copy one from your dev box underC:\Users\<User>\Documents\IISExpress\config\
. It needs to be copied to the corresponding path of the user your build server is running as. If it is running as system then the path would beC:\Windows\System32\config\systemprofile\Documents\IISExpress\config\
.不,您不会继承该接口。您可以使用 new 关键字创建 IISVersionManager 的实例。如何让您获得对 IIISExpressProcessUtility 实例的引用尚不清楚。 MSDN 文档太糟糕了。也许您可以新一个,但它看起来并不支持这一点。
No, you don't inherit the interface. You can create an instance of IISVersionManager with the new keyword. How that gets you a reference to an IIISExpressProcessUtility instance is completely unclear. The MSDN docs are awful. Maybe you can new one but it doesn't look like it supports that.
如果修改Web应用程序的web.config文件,IIS(包括Express)将重新启动应用程序池。这将允许您部署更新的程序集。
修改 web.config 的一种方法是将其复制到新文件,然后将其移回。
您可能需要对 IIS Express 进行更多控制,而不仅仅是重新启动应用程序池。但如果这就是您所需要的,那么这将起作用。
If you modify the web.config file of the web application, IIS (including Express) will restart the app pool. This will allow you to deploy updated assemblies.
One way to modify web.config is to copy it to a new file, and then move it back.
You may want more control over IIS Express than simply restarting the app pool. But if that's all you need, this will work.
我采用了不同的解决方案。您可以使用“taskkill”和进程名称简单地终止进程树。
这在本地和 TFS 2013 上完美运行
I have adopted a different solution. You can simply kill the process tree using "taskkill" and the name of the process.
This works perfectly locally and on TFS 2013