To find process Id of the worker process which is serving an IIS application, you can simply use Internet Information Service (IIS) Manager console. To do so, first make sure the worker process of the application is running (by browsing the application) then:
- Go to IIS manager and click on server node.
- In feature view, open Worker Processes.
- You can see the worker processes and related information including Process Id:
But how to do it using C# code?
Here is what I’ve tried to get process Id of a web site using code:
//using Microsoft.Web.Administration;
//…
var server = new ServerManager();
var site = server.Sites.Where(x => x.Name == "mysite.local").FirstOrDefault();
var application = site.Applications.Where(x => x.Path == "/").FirstOrDefault(); ;
var applicationPoolName = application.ApplicationPoolName;
var processId = server.WorkerProcesses
.Where(x => x.AppPoolName== applicationPoolName)
.Select(x => x.ProcessId).FirstOrDefault();
To make it working, don’t forget to add Microsoft.Web.Administration
reference. Also you should run the application or Visual Studio by “Run as Administrator”.