简体   繁体   中英

ASP.NET Core application debugging without publishing on IIS

I used to use asp.net mvc4 and in IIS my website's physical path would point my solution directory, and every time I update my code, I just re-build my solution and then I can use "Attach to process" (w3wp) to start debugging.

In asp.net core, when I publish my website to file system, I can run my website using IIS with no-managed code. But when I point my IIS Website to my solution code of website, it shows 502 error.

You don't need to run .Net Core in IIS to get easy debugging etc like we used to do as you described.

With .Net Core you can just open a command line at your project root and type "dotnet run"

DotNet Run uses environment variables to drive what it does. So if you want your site to run on a specific URL or port you Type:

SET ASPNETCORE_URLS=http://example.com

Or if you just want it to run on a different port

SET ASPNETCORE_URLS=http://localhost:8080

Then to set the Environment

SET ASPNETCORE_ENVIRONMENT=Development

Once all your environment variables are set, you type

dotnet run

Now to debug it, you attach to cmd.exe with dotnet run in it's Title. You'll be able to debug your code that way.

Now, if you are using Visual Studio There is a file called "launchSettings.JSON" under Properties in your project. You can configure profiles here and I have my default profiles set to Kestrel Development and then Kestrel Production, with IIS dead last so that I don't F5 run in IIS Express.

My LaunchSettings.json looks like this:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:56545/",
      "sslPort": 0
    }
  },
  "profiles": {
    "Kestrel Development": {
      "executablePath": "dotnet run",
      "commandName": "Project",
      "commandLineArgs": "dotnet run",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_URLS": "http://localhost:8080"
      }
    },
    "Kestrel Production": {
      "commandLineArgs": "dotnet run",
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_URLS": "http://localhost:8080",
        "ASPNETCORE_ENVIRONMENT": "Production"
      },
      "executablePath": "dotnet"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

The first Profile is what F5 uses when you press it. So when I press F5 Visual Studio launches dotnet run for me and set's the Environment and URLS as specified by the environmentVariables section of my profile in launchSettings.JSON.

Now because I have multiple Profiles I get a drop down next to the run button so I can select Kestrel Production if I want to run in Production mode locally.

在此处输入图片说明

Simple answer : when you do publish, you call a script that launches the publish-iis tool (see script section in project.json ).


In your project you have a web.config file with something like this:

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" 
 stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/

As you see, there are placeholders "%LAUNCHER_PATH%" and %LAUNCHER_ARGS% parameters. Keep these in mind.

Now open your project.json file and you will see a "scripts" section looking something like this:

"scripts":
{  
    "postpublish":"dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%"
}

It tells dotnet to run the publish-iis tool after the application is published. How it works :

publish-iis tool goes to the folder where the application was published (not your project folder) and checks if it contains a web.config file. If it doesn't, it will create one. If it does, it will check what kind of application you have (ie whether it is targeting full CLR or Core CLR and – for Core CLR – whether it is a portable or standalone application) and will set the values of the processPath and arguments attributes removing %LAUNCHER_PATH% and %LAUNCHER_ARGS% placeholders on the way.

Follow these steps to be able to achieve what you want.

  1. In launchSettings.json, add a property named iis under iisSettings , like so:

     "iis": { "applicationUrl": "http://my.aspnetcoreapp.com" }
  2. Under the profiles section, add a new profile having commandName set to IIS . I am calling mine Local IIS . This will add a new option to the Run drop down named Local IIS .

     "Local IIS": { "commandName": "IIS", "launchBrowser": true, "launchUrl": "http://my.aspnetcoreapp.com", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
  3. Create a website in IIS. Set host name to my.aspnetcoreapp.com . Also create/use an app pool for this website that has .NET CLR version set to "No Managed Code".

  4. Set physical path of that website to the location of your asp.net core project, not the solution, unless of course if you have the project in the same folder as the solution.
  5. Add a loop back entry in the hosts file (for Windows C:\\Windows\\System32\\drivers\\etc\\hosts )

    127.0.0.1 my.aspnetcoreapp.com

  6. Go back to Visual Studio, and run the application. Make sure you have "Local IIS" profile selected from Run drop-down. This will launch the application in the browser, at the URL, after a brief loading message that says "Provisioning IIS..." .

  7. Done! From now on, you can launch your application at that URL, and can also debug by attaching to process w3wp .

You could also host your app under "Default Web Site" , by specifying the ULR like localhost/MyAspNetCoreApp instead of my.aspnetcoreapp.com . If you do that, a new app pool will be created with the name MyAspNetCoreApp AppPool .

My medium article about this.

Why need this? This process will help while continuous development & debugging of the code, Here We do not need to deploy the application again & again and don't require to press F5 to run the application, Just change the code & build you can see the application working with the recent changes.

Description:

  1. Prerequisite: Install the ASP.NET Core Runtime(Hosting Bundle) from the official Microsoft website.

    • Search for the.Net core Hosting bundle and choose Microsoft's official site.
    • Download the suitable version from the list
  2. Create the.Net core Web applications and made the following changes.

    • Go to the folder > Properties > launchsettings.json > open the file and edit as below.
    • Add the below section under the existing "profiles" JSON. "IIS": { "commandName": "IIS", "launchBrowser": true, "launchUrl": "http://localhost:5000", //ie whatever the port set for iis "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }
    • Now, change the "iisSettings" as below. "windowsAuthentication": false, "anonymousAuthentication": true, "iis": { "applicationUrl": "http://localhost:5000", "sslPort": 0 }
  3. Go to the Solution explorer > Select the Project > "Alt + Enter" or "Right Click and Select Properties" - Go to the Debug > General > Open debug launch profiles UI. - Select the IIS profile from the list - Make sure the AppURL, URL is correctly set as the IIS profile launch URL (ie http://localhost:5000)

  4. Open the IIS > Sites > Add WebSite - Site Name: Any Name of site - Application Pool > Create or select a App pool with .NET CLR Version = No Managed Code - Physical Path > Project folder path location - Port: any port number ie 5000

  5. Build the application and run the URL - http://localhost:5000

  6. Now, to Attach & debug the application, Go to the Visual Studio and Press Ctrl+Alt+P

    • Find the Process > W3wp.exe ie In the search section enter 'W3'
    • Mark the checkbox checked > Show processes for all users
    • And Press the attach button.
    • Set the debug point.

Note: Visit the official Microsoft site for more detail. (Debug ASP.NET Core apps section) https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2022

只需运行Ctrl + F5 ,您就可以在运行站点时更改代码,而无需重新启动它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM