简体   繁体   English

以编程方式停止应用程序池问题IIS7

[英]STOP Application Pool Issue Programmatically IIS7

In my application I needs to stop a application pool programmatically in IIS 7. I have created a local account and given him Administrative Priviledges. 在我的应用程序中,我需要在IIS 7中以编程方式停止应用程序池。我已创建了一个本地帐户并为其授予了管理权限。 Here is the code 这是代码

Private static void StopApplication()
{
    string serviceHostDeploymentType = "local";
        if (serviceHostDeploymentType.Equals("local"))
        {
            WindowsIdentityImpersonate newIdentity = new WindowsIdentityImpersonate();
            if (newIdentity.Impersonate("AccountName", Environment.MachineName, "Password"))
            {
                try
                {
                    ServerManager serverManager = ServerManager.OpenRemote("Server");
                    string siteName = GetWebSiteNameById(serverManager, 1);
                    Site parentWebsite = serverManager.Sites[siteName];

                    if (parentWebsite != null)
                    {
                        ApplicationPool apppool = serverManager.ApplicationPools["Application Pool Name"];

                        if (apppool != null)
                        {
                            apppool.Stop();
                        }
                    }
                }
                finally
                {
                    newIdentity.Undo();
                }
            }
        }
        else
        {
           throw new ConfigurationErrorsException("...");
        }
    }

I am getting an error Access is denied. 我收到错误访问被拒绝。 (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)). (来自HRESULT的异常:0x80070005(E_ACCESSDENIED))。

Stack Trace

    at Microsoft.Web.Administration.Interop.IAppHostMethodInstance.Execute()
       at Microsoft.Web.Administration.ConfigurationElement.ExecuteMethod(String methodName)
       at Microsoft.Web.Administration.ApplicationPool.Stop()
       at ServerManagerTesting.Form1.StopApplication()
       at ServerManagerTesting.Form1.button1_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at ServerManagerTesting.Program.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

The likely problem is that UAC is preventing you to control IIS. 可能的问题是UAC阻止您控制IIS。 Have a look at this question . 看看这个问题 The chosen answer has nothing to do with your own question, but other answers point to an SDK sample that launches a new process with elevated privileges. 所选答案与您自己的问题无关,但其他答案指向一个SDK示例,该示例使用提升的权限启动新进程。

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

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