How to use ServerManager to read IIS sites, not IIS express, from class library OR how do elevated processes handle class libraries?

前端 未结 4 562
说谎
说谎 2020-12-25 10:45

I have some utility methods that use Microsoft.Web.Administration.ServerManager that I\'ve been having some issues with. Use the following dead simple code for

4条回答
  •  暖寄归人
    2020-12-25 11:29

    CAUTION! Using this approach we have seen seemingly random issues such as "unsupported operation" exceptions, failure to add/remove HTTPS bindings, failure to start/stop application pools when running in IIS Express, and other problems. It is unknown whether this is due to IIS being generally buggy or due to the unorthodox approach described here. In general, my impression is that all tools for automating IIS (appcmd, Microsoft.Web.Administration, PowerShell, ...) are wonky and unstable, especially across different OS versions. Good testing is (as always) advisable!

    The regular Microsoft.Web.Administration package installed from NuGet works fine. No need to copy any system DLLs.

    The obvious solution from the official documentation also works fine:

    ServerManager iisManager = new ServerManager(@"C:\Windows\System32\inetsrv\config\applicationHost.config");
    

    This works even if you execute the above from within the application pool of IIS Express. You will still see the configuration of the "real" IIS. You will even be able to add new sites, as long as your application runs as a user with permission to do so.

    Note, however that the constructor above is documented as "Microsoft internal use only":

    https://msdn.microsoft.com/en-us/library/ms617371(v=vs.90).aspx

提交回复
热议问题