Notes to self: Publishing .NET Core web services

 I generally create .NET Core web services in Visual Studio. I’ve heard good things about the more light-weight and easy-to-configure Visual Studio Code, but I’ve only barely tried it myself yet. I’m sure I will give it another go sometime soon, but for now I’m fairly content with VS – with one exception.

Several times now, I’ve got myself into the same problem when needing to publish a new web service to a file-system. Since I tend to work on different kinds of project with different types of technology, a couple of months may pass between each time I need to do this, and so I’ll typically forget what I need to do: I keep thinking I can publish quickly and easily by right clicking the service-node in the Solution Explorer, selecting Publish, and providing a folder path: 

Hang on there, not so fast…

This “works” in the sense that it pushes out a bunch of files into that folder – files that look as if though they should contain all that is needed to run the service – but when the files are copied to another server and I try to connect to it, it always tends to fail miserably.

I don’t even want to get into all the problems (missing references, HTTP 503-errors, and more). Suffice it to say, it’s been troublesome. I tried tweaking the publish-settings to see if it would make a difference, but it was just more time wasted.

What to do instead

Short and super simle answer: Use the command line instead.

dotnet publish -c release


Run this from the root folder of the project containing your service. That should output a functioning service into a folder like this:

Your-Project\bin\Release\netcoreapp2.1\publish

Now you should be able to copy the generated files onto your server and get it up and running without too much fuss.

Just remember

…to make sure you’ve got the right values (production-values!) in your appsettings.json file and in your web.config. In particular, you might want to make sure you’ve set the value of ASPNETCORE_ENVIRONMENT to Release instead of Development:

<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />

Simple mistakes like overlooking the above can result in a lot of wasted time, and may cause some embarrassment, but I’ll be the first to admit that even after years of experience, they are still easy to make…

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.