A fatal error occurred. The required library hostfxr.dll could not be found

Today in this article, we will see how to fix error “A fatal error occurred. The required library hostfxr.dll could not be found”

We will cover below aspects,

Issue Description

The application throws the below error in the runtime,

A fatal error occurred. The required library hostfxr.dll could not be found

The required library hostfxrdll could not be found

Resolution

The issue I found to be due to the .NET runtime not being available in the target machine.

There could be few basic reasons for the error.

Generally, developers tend to deploy the application in a new environment assuming it will work as-is.

When installing the application to the new environment, if the target machine has the required .NET version already installed then the application should work without any issue.

If the required .NET core SDK/runtime is not installed then one can follow any of the below approaches discussed.

An approach using Self-content deployment doesn’t require a .NET Core SDK/Runtime to be installed on the target machines.

Let’s see both the approaches in detail,

Resolution 1 – Build Application as FDD(Framework dependent deployment)

If you are building an application using an approach called Framework dependent deployment (FDC) approach, then it is expected that the target server or environment has the .NET Core SDK and runtime installed.

For more details, please refer Self Contained Vs Framework Dependent Deployment

Typically, your application supported Runtime/SDK will be available in the below folder on the target machine,

C:\Program Files\dotnet

hostfxr.dll location

hostfxr.dll can be found below shared location depending on the target .NET version used,

hostfxrdll location
hostfxrdll

Resolution: Install NET Core on the target machine/environment. Your application will work just fine as a developer machine.

If you want an alternative solution without installing the .NET Core version, please see below,

Resolution 2 – Build Application as Self-Content Deployment

If you are building an application using an approach, then you make sure the application deployable is self-content and can run on its own.

For more details, please refer Self Contained Vs Framework Dependent Deployment

This can be achieved by building your application using the below build command,

For Linux

dotnet publish -r linux-x64 --self-contained true -c release

For Windows10

dotnet publish -r win10-x64 --self-contained true -c release

OR

For Windows Server

dotnet publish -r win-x64 --self-contained true -c release

With this approach, you achieve the below advantage,

  • App doesn’t required to download and install. NET.
  • Application is isolated from other .NET apps
  • Application doesnt use a locally installed shared runtime if available
  • You are able to control which target version you want to use.

The user of your app isn’t required to download and install. NET.

With the above-discussed approaches, you should be all set to run your application without any issues.

hostfxr.dll location

Post resolving the issue, the missing hostfxr.dll can be found in the publish target folder along with the other binaries.

blank

Configuration for Cloud as Host

In cloud based application, you use Docker image for a .NET application.

This base image will already include the required runtime files, eliminating the need to manually handle hostfxr.dll in the container.

Please see here for more details,

Role of hostfxr.dll In Application Lifecycle

References:

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



7 thoughts on “Resolved: The required library hostfxr.dll could not be found

  1. Hello!
    Thank you for the information provided!
    In my case I use “Setup” extension to pack the app into .msi package and give it to customer. Do you know if in such a case it’s possible to create a kind of self-contained package that will not require .NET Core SDK to be installed on the production machine?
    Looking forward to read from you

      1. thank you for the answer!
        Somehow, though, the publishing process changes the whole app.config to web.config approach. Haven’t you encountered such behavior?

Leave a Reply

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