Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式停止应用程序池问题IIS7_C# - Fatal编程技术网

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

C# 以编程方式停止应用程序池问题IIS7,c#,C#,在我的应用程序中,我需要在IIS 7中以编程方式停止应用程序池。我已经创建了一个本地帐户,并给了他行政特权。这是密码 Private static void StopApplication() { string serviceHostDeploymentType = "local"; if (serviceHostDeploymentType.Equals("local")) { WindowsIdentityImpersonate

在我的应用程序中,我需要在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))


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

我也认为这是UAC的问题,但除了将用户添加到管理员组之外,我还需要做些什么才能使其正常运行当我从“运行方式”运行应用程序时,它确实会运行。但此代码位于IIS上的服务中。我怎样才能让它运行。从服务调用Impersonate时不起作用。请按照所引用问题的建议进行操作。它显示了如何在启用所需权限的情况下启动新流程。
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()