ML.Net sentimental analysis prediction of comments not working in ASP.NET MVC web application


I am trying to make project in .NET framework in which the controller code is as below:

public ActionResult Analysis()
    return View();

public ActionResult Analysis(ModelInput input)
    // Load the model  
    MLContext mlContext = new MLContext();
    ITransformer mlModel = mlContext.Model.Load(@"C:\Users\samya\source\repos\riya123\riya123ML.Model\", out var modelInputSchema);

    // Create prediction engine related to the loaded train model
    var predEngine = mlContext.Model.CreatePredictionEngine<ModelInput, ModelOutput>(mlModel);

    // Input  
    input.Year = DateTime.Now.Year;

    // Try model on sample data and find the score
    ModelOutput result = predEngine.Predict(input);

    // Store result into ViewBag
    ViewBag.Result = result;

    return View();

And when I try to run it shows error as below although the dll is seen in the solution explorer:

Exception thrown: 'System.DllNotFoundException' in Microsoft.ML.CpuMath.dll

An exception of type 'System.DllNotFoundException' occurred in Microsoft.ML.CpuMath.dll but was not handled in user code

Unable to load DLL 'CpuMathNative': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

asked on Stack Overflow Nov 16, 2019 by samjm • edited Nov 16, 2019 by marc_s

1 Answer


There is a bug with the v1.4.0 version of ML.NET that breaks projects using packages.config. See:

To workaround this, try one of the following possible workarounds:

  1. Use PackageReference instead of packages.config.
  2. Fallback to v1.3.1 of ML.NET until a new version comes out with the fix.
  3. Alternatively, you can copy the CpuMathNative.dll from the nuget package into your output folder. You can do this manually, or with a change to your .csproj like the following:
    <Content Include="..\packages\Microsoft.ML.CpuMath.1.4.0\runtimes\win-x64\nativeassets\netstandard2.0\*.dll" Condition="'$(PlatformTarget)' == 'x64'">
    <Content Include="..\packages\Microsoft.ML.CpuMath.1.4.0\runtimes\win-x86\nativeassets\netstandard2.0\*.dll" Condition="'$(PlatformTarget)' == 'x86'">

(NOTE: if your packages folder is somewhere other than ..\packages, you will need to adjust the path above.)

Also note one more thing: You can't use AnyCPU with ML.NET on .NET Framework. Since ML.NET uses native assemblies, you need to pick either x86 or x64 explicitly.

answered on Stack Overflow Nov 21, 2019 by Eric Erhardt

User contributions licensed under CC BY-SA 3.0