Jakub Holovsky
Jakub Holovsky

Reputation: 6772

Microsoft.SqlServer.Types: Error loading msvcr120.dll (ErrorCode: 5)

In our project we are using the following nuget package:

Microsoft.SqlServer.Types

Everything has worked fine until recently without me obviously changing anything important the ASP.NET application breaks when starting with following exception:

Error loading msvcr120.dll (ErrorCode: 5)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception: Error loading msvcr120.dll (ErrorCode: 5)

The stacktrace:

[Exception: Error loading msvcr120.dll (ErrorCode: 5)]
SqlServerTypes.Utilities.LoadNativeAssembly(String nativeBinaryPath, String assemblyName) in E:\Dev\Jacobo\ServerApi\SqlServerTypes\Loader.cs:43
SqlServerTypes.Utilities.LoadNativeAssemblies(String rootApplicationPath) in E:\Dev\Jacobo\ServerApi\SqlServerTypes\Loader.cs:28
Jacobo.ServerApi.WebApiApplication.Application_Start() in E:\Dev\Jacobo\ServerApi\Global.asax.cs:26

[HttpException (0x80004005): Error loading msvcr120.dll (ErrorCode: 5)]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +529
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +185
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +421
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +359

[HttpException (0x80004005): Error loading msvcr120.dll (ErrorCode: 5)] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +534 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +117
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +726

The application breaks in following method:

private static void LoadNativeAssembly(string nativeBinaryPath, string assemblyName)
        {
            var path = Path.Combine(nativeBinaryPath, assemblyName);
            var ptr = LoadLibrary(path);
            if (ptr == IntPtr.Zero)
            {
                throw new Exception(string.Format(
                    "Error loading {0} (ErrorCode: {1})",
                    assemblyName,
                    Marshal.GetLastWin32Error()));
            }
        }

Which is part of loader.cs which is a part of the SqlServerTypes nuget package.

I checked the bin folder and located the SqlServerTypes assemblies existing there. I am really unsure what has gone wrong here.

enter image description here

Upvotes: 10

Views: 13849

Answers (3)

Saif Aqqad
Saif Aqqad

Reputation: 46

In my case, I solved it by giving the IIS_IUSRS user group permissions to the project directory.

This can be done by going into the folder's properties, then under the security tab, click Edit then Add, type IIS_IUSRS and click OK

Upvotes: 0

jocull
jocull

Reputation: 21115

I encountered this issue today. The fix I needed to implement was to ensure the SQL Server types files were part of my project so they would be produced in the publish output:

DLLs in solution explorer

Just ensure that Copy to Output Directory is set for each of the relevant DLL files. If you do not see them in your solution explorer, you may need to select to show all files and then right click them to Include in Project.

Added unreferenced files in solution explorer

Upvotes: 11

Mihail Stancescu
Mihail Stancescu

Reputation: 4138

Based on the error code 5 I would say the identity the web app is running doesn't have access to the path that the DLL is located.

The error code 5 means: ERROR_ACCESS_DENIED.
Please check that the identity has permissions to that folder/file.

Upvotes: 3

Related Questions