2 years ago

#51513

test-img

richaux

Could not load assembly 'Newtonsoft.Json' using ml.net in Azure Web Api

TL;DR

In an Azure-hosted Web API, an ML.net model fails to load due to a missing Newtonsoft.Json dependency.


Details

I've a C# net6.0 Azure hosted Web API that loads and calls a pre-built ML.net (v1.7.0) model (multi-classification using SdcaMaximumEntropy, in case that's relevant).

I've tested it locally using a console app to call the localhost web api. This is then deployed, via a GitHub action, to an Azure Api App (action yaml snippet below).

      - name: Set up .NET Core
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '6.0.x'
          include-prerelease: true

Once deployed, I can call the relevant endpoint but an exception is generated from the Model.Load method below. [The api returns a 500 ... the detail is available via Azure App Insights.]

private readonly MLContext _mlContext;
...
var loadedModel = _mlContext.Model.Load(_modelPath, out var _);
Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'.
The system cannot find the file specified.

Any pointers as to why that's happening gratefully received; let me know if there's any further info required.


Further Info

  1. In the project, Newtonsoft.Json is only required/referenced by the Microsoft.ML package.

  2. I've checked in the Advanced Tools (kudu) to ensure NewtonSoft.Json is being deployed.

  3. I've tried adding an explicit reference to the Newtonsoft.Json package; I've also tried adding another package that has a reference to Newtonsoft; I've switched the Model.Load to the more robust AddPredictionEnginePool variation. None of these had any impact (but still work locally).

  4. I've checked that the _modelPath of the model was being set to the right location [D:\home\site\wwwroot\SupporterCategoriser\Data\model.zip], and that the model exists, in case the missing model was somehow being mis-reported.

  5. n.b. There have been numerous Newtonsoft FileNotFoundException questions posted over the last decade, none seemed to be related.

  6. n.b. There are also plans to remove the Newtonsoft dependency from the ML package but not for any related reason.


Stack trace

System.IO.FileNotFoundException:
   at System.Reflection.CustomAttribute._CreateCaObject (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Reflection.CustomAttribute.CreateCaObject (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Reflection.CustomAttribute.AddCustomAttributes (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Reflection.CustomAttribute.GetCustomAttributes (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Reflection.CustomAttribute.GetCustomAttributes (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Reflection.RuntimeAssembly.GetCustomAttributes (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Attribute.GetCustomAttributes (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.ML.Runtime.ComponentCatalog.RegisterAssembly (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.EnsureLoaderAssemblyIsRegistered (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.TryLoadModelCore (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.TryLoadModel (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.LoadModel (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.LoadModelOrNull (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelLoadContext.LoadModel (Microsoft.ML.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelOperationsCatalog.Load (Microsoft.ML.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at Microsoft.ML.ModelOperationsCatalog.Load (Microsoft.ML.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51)
   at GolfGuruApi.SupporterCategoriser.Services.CategoryService.PredictIssue (XxxApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\a\xxx-api\xxx-api\XxxApi\SupporterCategoriser\Services\CategoryService.cs:143)
   at XxxApi.SupporterCategoriser.Services.CategoryService.GetCategory (XxxApi, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\a\xxx-api\xxx-api\XxxApi\SupporterCategoriser\Services\CategoryService.cs:31)
   at lambda_method2 (Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory+<>c__DisplayClass46_3+<<HandleRequestBodyAndCompileRequestDelegate>b__2>d.MoveNext (Microsoft.AspNetCore.Http.Extensions, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware+<<Invoke>g__AwaitRequestTask|6_0>d.MoveNext (Microsoft.AspNetCore.Routing, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult (System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1+<ProcessRequestAsync>d__2.MoveNext (Microsoft.AspNetCore.Server.IIS, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)

csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.ML" Version="1.7.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="SupporterCategoriser\Data\model.zip">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>

ml.net

azure-webapps

0 Answers

Your Answer

Accepted video resources