7

I am trying to get an Azure function running, but am encountering the following error on deployment --

Microsoft.Azure.WebJobs.Script: Error building configuration in an external startup class. 
System.Private.CoreLib: Could not load file or assembly '<MyAssembly>, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null'.

But my deployment was successful and I can see the DLL in App Files. I'm running C# / .net 6.0 on Functions v4.0 and using the GitHub Workflow action generated by Deployment Center to deploy.

What can cause the runtime to be unable to see the main function DLL?

Adding image of App Files:

app files

My project package references:

  <ItemGroup>
    <PackageReference Include="Azure.Identity" Version="1.8.2" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
    <PackageReference Include="Azure.Storage.Blobs" Version="12.15.1" />
    <PackageReference Include="Cosmonaut" Version="2.11.3" />
    <PackageReference Include="Cosmonaut.ApplicationInsights" Version="2.1.1" />
    <PackageReference Include="Cosmonaut.Extensions.Microsoft.DependencyInjection" Version="2.3.0" />
    <PackageReference Include="Cosmonaut.NestedQueries" Version="2.11.4" />
    <PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="6.0.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />
    <PackageReference Include="Microsoft.Azure.Services.AppAuthentication" Version="1.6.2" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.SendGrid" Version="3.0.3" />
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="5.1.1" />
    <PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.1">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
    <PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
  </ItemGroup>

Deployment GitHub Action from Deployment Center:

# Docs for the Azure Web Apps Deploy action: https://github.com/azure/functions-action
# More GitHub Actions for Azure: https://github.com/Azure/actions

name: Build and deploy dotnet core app to Azure Function App - brokentoysapi3

on:
  push:
    branches:
      - master
  workflow_dispatch:

env:
  AZURE_FUNCTIONAPP_PACKAGE_PATH: './api/_new/BrokenToys.Api/' # set this to the path to your web app project, defaults to the repository root
  DOTNET_VERSION: '6.0.x' # set this to the dotnet version to use

jobs:
  build-and-deploy:
    runs-on: windows-latest
    steps:
      - name: 'Checkout GitHub Action'
        uses: actions/checkout@v2

      - name: Setup DotNet ${{ env.DOTNET_VERSION }} Environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: ${{ env.DOTNET_VERSION }}

      - name: 'Resolve Project Dependencies Using Dotnet'
        shell: pwsh
        run: |
          pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}'
          dotnet build --configuration Release --output ./output
          popd

      - name: 'Run Azure Functions Action'
        uses: Azure/functions-action@v1
        id: fa
        with:
          app-name: 'brokentoysapi3'
          slot-name: 'Production'
          package: '${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}/output'
          publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_E20EFE8EF7D84C4AA5B5C75BE0B66D0F }}

Here's my startup class:

using BrokenToys.Api.Models.Games;
using Cosmonaut;
using Cosmonaut.Extensions.Microsoft.DependencyInjection;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;

[assembly: FunctionsStartup(typeof(BrokenToys.Api.Startup))]

namespace BrokenToys.Api
{
    public class Startup : FunctionsStartup
    {
        public static IConfiguration Configuration { set; get; }


        public override void Configure(IFunctionsHostBuilder builder)
        {
 
            var cosmosSettings = 
                    new CosmosStoreSettings
                    ("BrokenToys", "https://<mycosmosdb>.documents.azure.com:443/", "<mykey>");

            builder.Services.AddCosmosStore<Score>(cosmosSettings);
            builder.Services.AddCosmosStore<Player>(cosmosSettings);

        }
    }
}

jps
  • 20,041
  • 15
  • 75
  • 79
Jeffrey Meyer
  • 5,410
  • 7
  • 30
  • 27

1 Answers1

6

Microsoft.Azure.WebJobs.Script: Error building configuration in an external startup class. System.Private.CoreLib: Could not load file or assembly ', Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.

This error indicates that the runtime is unable to load the assembly for your function. This could be due to a few reasons such as incorrect file path or incorrect function name. Check if below steps helps to fix the issue:

  1. Validate the assembly's name and version in the error message match the name and version of the assembly in your App Files folder. If they do not match, you may need to rebuild your project and redeploy it.

  2. Check if the assembly is in the correct location in your App Files folder. It should be in the bin folder, which is located in the root of your function app.

  3. Check if the assembly is compatible with the runtime version of your function and also check if your function app is configured to use the correct version of the runtime in the Azure Portal. enter image description here

  4. Check if your GitHub Workflow action is configured correctly to deploy your function app. Make sure that it is deploying all the files are getting deployed to the correct location.

I have reproduced the same in my environment and it is working without any issues as shown below:

DLL files: enter image description here

enter image description here

And also I observed that the package which you are using in your applicatioon Microsoft.Azure.WebJobs.Script is deprecated.

enter image description here

Pravallika KV
  • 2,415
  • 2
  • 2
  • 7
  • 2
    hmm i am not sure where that Script package is coming from. I'll post a pic of my packages from the project, but i do not have a reference to it... – Jeffrey Meyer Apr 06 '23 at 15:40
  • 3
    All these packages are internally linked to the Function SDK. `Microsoft.Azure.WebJobs.Script` has dependencies as shown in this [NuGet Site](https://i.imgur.com/aKWyVVm.png) so those packages should be updated to compatible versions according to your function SDK (v3 or v4) and .NET Version. Refer to SO Issue [67338863](https://stackoverflow.com/a/67338863) – Pravallika KV Apr 07 '23 at 06:22
  • 1
    I do not have any of those packages at this point, and even if I create a brand new project with only a package ref to Microsoft.NET.Sdk.Functions 4.1.3, I still see the same error on Startup – Jeffrey Meyer Apr 08 '23 at 13:13
  • Try building from a freshly created VS project from another dev machine. Also, check if there is any post-build step that unintentionally adds[not seen] a dependency in your Github workflow. – Anand Sowmithiran Apr 13 '23 at 13:30
  • Just as a way of poking at the issue, you could try implementing IWebJobsStartup instead of extending FunctionsStartup in the startup class. I used Microsoft.Extensions.Logging 6.0.0 (NOT the newest 7.0.0) with this code: public class AzureFunctionAppStartup : IWebJobsStartup { public void Configure(IWebJobsBuilder builder) { ConfigureServices(builder.Services); } private void ConfigureServices(IServiceCollection services) { services.AddLogging(); } } – Mikael Suokas Apr 14 '23 at 17:10
  • I never did figure this out, but I ended up rewriting to be a dotnet-isolated set of functions. After I created a whole new project using "func init" I was able to get the functions running again successfully. I suspect that the problem here was some reference inside one of my references tripping some kind of condition inside the Azure Functions runtime to use the Microsoft.Azure.Web.Script deprecated manner of execution. And from there everything broke and bubbled out as a startup issue. But man, the debug story with this type of problem is just not there at this point, what a pain – Jeffrey Meyer Apr 14 '23 at 19:32