Avatar of Marianne VAN WYK
Marianne VAN WYKFlag for South Africa asked on

Self Hosting a dotnet core 2.1 api as a application instead of using IIS.

I have built a simple api in dotnet core 2.1, using the following example:  ("https://www.codingame.com/playgrounds/35462/creating-web-api-in-asp-net-core-2-0/part-1---web-api")

I am in the process of trying to build and host this api on my local machine. I have published it, created a new website in IIS, with an application pool, coppied the files over, etc. When I try to run it locally on my chrome, I get "This page isn’t working 192.168.1.110 is currently unable to handle this request.
HTTP ERROR 500"

I tried following this guide: "https://www.c-sharpcorner.com/article/publish-asp-net-core-2-0-application-on-iis/" but to no help.

When I run this example in visual studio, it works fine. Am I missing something in the publishing steps or config file that is causing this to happen?


As a side note, I have also installed the .net core hosting package, etc.
.NET MVC.NET ProgrammingMicrosoft Visual Studio

Avatar of undefined
Last Comment
Marianne VAN WYK

8/22/2022 - Mon
Chinmay Patel

Hi,

Share the web.config from your published folder please. Remove any sensitive information from that file before posting it here.

Regards,
Chinmay.
ASKER
Marianne VAN WYK

I hope this is the file you are looking for. (its small enough to just copy and paste the contents)   web.config from my published file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\API.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>
Chinmay Patel

Yeah that's the one. Please use Code tags from the toolbar to publish your code, it makes it easily readable.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\API.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

Open in new window


Once you replace the web.config as per the above one, check your logs folder (you will have to manually created it in your web apps' root. logs and then a child folder stdout). If you have configured everything properly, you will start seeing entries in stdout folder. It might give you the real error why it is not running OR it will simply start working.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Marianne VAN WYK

After a lot of struggling I found that the configuration was not correct in my Startup.cs file.  Without the env.IsProduction() option, when building and deploying it to iis, it simply was not configured to work on a released version.


While reading up about this,  I have seen that it is possible to self host an API,  instead of hosting it in IIS, but rather as a application (which seems to be very possible with .net core). This would be the true answer to my question, although I am still looking as to how to configure and build it to self host.

I am changing the title of the Question to hopefully ask a more spesific question about self hosting.

Regards.
ASKER CERTIFIED SOLUTION
Chinmay Patel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Marianne VAN WYK

I will do that.  Thank you very much.