【问题标题】:STOP Application Pool Issue Programmatically IIS7以编程方式停止应用程序池问题 IIS7
【发布时间】:2018-08-05 00:09:30
【问题描述】:

在我的应用程序中,我需要在 IIS 7 中以编程方式停止应用程序池。我创建了一个本地帐户并授予他管理权限。这是代码

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("...");
        }
    }

我收到错误访问被拒绝。 (来自 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()

【问题讨论】:

    标签: c#


    【解决方案1】:

    可能的问题是 UAC 阻止您控制 IIS。看看这个question。选择的答案与您自己的问题无关,但其他答案指向一个 SDK 示例,该示例使用提升的权限启动一个新进程。

    【讨论】:

    • 我也认为它是 UAC 的问题,但除了将用户添加到管理员组之外,我还需要做什么才能使其正常工作
    • 当我从“运行方式”运行应用程序时,它会运行。但是此代码位于 IIS 上的服务中。我怎样才能让它运行。从服务调用时模拟不起作用。
    • 请按照所引用问题的建议进行操作。它显示了如何在启用所需权限的情况下启动新进程。
    猜你喜欢
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 2013-12-01
    • 2010-09-17
    • 2010-11-12
    • 2023-04-06
    相关资源
    最近更新 更多