. NET obtains IIS7.0 and later managed service information, iis7.0 Service Information

Source: Internet
Author: User

. NET obtains IIS7.0 and later managed service information, iis7.0 Service Information

Recently I wrote a small tool that scans the IIS hosting site and regularly registers it to Consul. I copied a help class on the Internet. After testing the local machine, I threw it to the server and found a huge exception ..

System. Runtime. InteropServices. COMException (0x80005000): Unknown error (0x80005000)
In System. DirectoryServices. DirectoryEntry. Bind (Boolean throwIfFail)
In System. DirectoryServices. DirectoryEntry. Bind ()
In System. DirectoryServices. DirectoryEntry. get_IsContainer ()
In System. DirectoryServices. DirectoryEntries. ChildEnumerator .. ctor (DirectoryEntry container)

After checking the information, we found that:

This exception Code indicates that the ADSI provider of IIS: // localhost/W3SVC/1 does not exist or cannot be accessed.

Open the IIS manager and you can see that the localhost (default site) of the server exists and is running, and the master site ID is indeed 1. This indicates that the problem occurs in the ADSI provider of IIS: // localhost.

Frankly speaking, ADSI provider is not installed in IIS 7 by default.

Solution: "Control Panel"-> "programs and functions"-> "enable or disable windows functions"-> "Web Server (IIS)" on the left side of the Panel) "->" Administrative Tools "->" IIS 6 management compatibility "->" IIS metabase compatibility ".

The code is relatively simple:

Using System. serviceProcess; using System. directoryServices; public class IISManager {public static List <IISWebServiceInfo> destroy () {DirectoryEntry rootfolder = new DirectoryEntry ("IIS: // localhost/W3SVC"); foreach (DirectoryEntry child in rootfolder. children) {if (child. schemaClassName = "IIsWebServer") {child. properties ["ServerComment"]. value. toString (); // service name child. properties ["ServerState"]. value; // service status child. properties ["ServerBindings"]. value; // binding information }}}}

The disadvantage is that it is too troublesome to only support IIS6 and earlier versions (install ADSI provider ..

You can use Microsoft. Web. Administration to obtain service information from IIS7.0 and later versions, so that Nuget can find the service information.

I have to say that Microsoft is still a hard nut to crack .. This is the pace of cross-platform development. (is Linux IIS just around the corner ...)

The code is simpler:

Using Microsoft. web. administration; class Program {static void Main (string [] args) {ServerManager sm = new ServerManager (); foreach (var s in sm. sites) {Console. writeLine ("website name: {0}", s. name); Console. writeLine ("running status: {0}", s. state. toString (); foreach (var tmp in s. bindings) {System. console. writeLine ("\ t type: {0}", tmp. protocol); System. console. writeLine ("\ tIP address: {0}", tmp. endPoint. address. toString (); System. console. writeLine ("\ t port: {0}", tmp. endPoint. port. toString (); System. console. writeLine ("\ t Host Name: {0}", tmp. host) ;}} Console. readKey ();}}

Of course, the administrator privilege is required to obtain IIS information in the windows operating system.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.