Link to home
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.
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Hi,

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

Regards,
Chinmay.
Avatar of Marianne VAN WYK

ASKER

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>
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.
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
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
I will do that.  Thank you very much.