Migrating Functions V1 to V4 dotnet-isolated System.IO.FileNotFoundException: Could not load file or assembly Functions.dll

132 views Asked by At

We have a Functionapp project (class library) with 7 functions (6 timetriggers and 1 servicebustrigger) running on the ,Net Framework 4.8. I tried to migrate the project to V4 using the learn.microsoft.com migration guide.

The diagram in the documentation shows support for the net48 functions running in V4 under the dotnet-isolated run-environment.

My proj file has been altered according the documentation:|

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <RootNamespace>My.Namespace</RootNamespace>
    <OutputType>Exe</OutputType>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.19.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http.AspNetCore" Version="1.0.0" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
    <!-- Other packages may also be in this list -->
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Using Include="System.Threading.ExecutionContext" Alias="ExecutionContext"/>
  </ItemGroup>
</Project>

The output type clearly shows that the host will be looking for the function.exe.
The template has been set to Console application, making sense with an output type of .exe, and the functions.exe is present!

Problem (one of many) which I can not figure out is that it is looking for a functions.dll:

Here is the stacktrace:

[2023-12-06T19:36:38.225Z] Worker process started and initialized.
[2023-12-06T19:36:38.252Z] Worker failed to load function: 'DbCleanFunction' with functionId: '3350649044'.
[2023-12-06T19:36:38.253Z] Result: Failure
Exception: System.IO.FileNotFoundException: Could not load file or assembly 'file:///C:\Users\fkr22238\source\repos\PAS\Source\NL.NS.NSRA\NL.NS.NSRA.FunctionsIsolated\bin\output\NL.NS.NSRA.Fuctions.Isolated.dll' or one of its dependencies. The system cannot find the file specified.
[2023-12-06T19:36:38.254Z] File name: 'file:///C:\Users\fkr22238\source\repos\PAS\Source\NL.NS.NSRA\NL.NS.NSRA.FunctionsIsolated\bin\output\NL.NS.NSRA.Fuctions.Isolated.dll'
[2023-12-06T19:36:38.254Z]    at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
[2023-12-06T19:36:38.256Z]    at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
[2023-12-06T19:36:38.256Z]    at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
[2023-12-06T19:36:38.257Z]    at System.Reflection.Assembly.LoadFrom(String assemblyFile)
[2023-12-06T19:36:38.257Z]    at Microsoft.Azure.Functions.Worker.Invocation.DefaultMethodInfoLocator.GetMethod(String pathToAssembly, String entryPoint) in D:\a\_work\1\s\src\DotNetWorker.Core\Invocation\DefaultMethodInfoLocator.cs:line 27
[2023-12-06T19:36:38.258Z]    at Microsoft.Azure.Functions.Worker.Definition.GrpcFunctionDefinition..ctor(FunctionLoadRequest loadRequest, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\Definition\GrpcFunctionDefinition.cs:line 56
[2023-12-06T19:36:38.258Z]    at Microsoft.Azure.Functions.Worker.GrpcWorker.FunctionLoadRequestHandler(FunctionLoadRequest request, IFunctionsApplication application, IMethodInfoLocator methodInfoLocator) in D:\a\_work\1\s\src\DotNetWorker.Grpc\GrpcWorker.cs:line 269

Due to all these little failures, with this one being the major failure, I tried to setup a new Function App and just copied one of the functions in it (DBClean) to see if the error would go away. It didn't work.

Anyone having the same problem or having a solution to this problem? Any help is highly appreciated!

1

There are 1 answers

0
Pravallika KV On
  • Modify your .csproj as shown below:

.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>  
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
    <OutputType>Exe</OutputType>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.Functions.Worker" Version="1.20.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Extensions.Http" Version="3.1.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.Sdk" Version="1.16.2" />
    <PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.21.0" />
    <PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" Version="1.0.0" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>
</Project>

local.settings.json:

  • Add Application setting "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated" in your local.settings.json.
{
    "IsEncrypted": false,
    "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
    }
}
  • Also check if your function code is valid and compatible with the Version V4.

  • I have created a .NET 4.8 Framework Http Triggered Azure Function code with version V4.

Function.cs:

public class Function1
{
    private readonly ILogger _logger;

    public Function1(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Function1>();
    }

    [Function("Function1")]
    public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequestData req)
    {
        _logger.LogInformation("C# HTTP trigger function processed a request.");

        var response = req.CreateResponse(HttpStatusCode.OK);
        response.Headers.Add("Content-Type", "text/plain; charset=utf-8");

        response.WriteString("Welcome to Azure Functions!");

        return response;
    }
}

Program.cs:

public class Program
{
    static void Main(string[] args)
    {
        FunctionsDebugger.Enable();

        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .ConfigureServices(services =>
            {
                services.AddApplicationInsightsTelemetryWorkerService();
                services.ConfigureFunctionsApplicationInsights();
            })
            .Build();

        host.Run();
    }
}
  • Delete all the assemblies and rebuild the project.
  • Create a Sample .NET 4.8 Azure function app with Version V4 and compare with your Function project's structure and files, check if there is any missing file or configuration in your project.