.NET Azure Functions - Dependency Injection Issue

2

On Startup.cs in my Azure Function v2 project:

using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using MyCompany.MyLib.Contracts; //namespace from external library

[assembly: FunctionsStartup(typeof(Startup))]
namespace Test
{
    public class Startup : FunctionsStartup
    {
       public override void Configure(IFunctionsHostBuilder builder)
      {            
        builder.Services.AddTransient(typeof(Logging.ILogger<>), typeof(Logging.Logger<>));
        builder.Services.AddTransient<IUserLogic, UserLogic>();
        builder.Services.AddTransient<IBillingLogic, BillingLogic>(); //---> loads up from above referenced "MyCompany.MyLib.Contracts" namespace and this namespace is from externally referenced class library but with in same solution
    }
}

}

The above code with my own custom classes within function app project like "EmailLogic", "Logger" works fine.

But the moment I added up custom classes to services container like "BillingLogic" from external C# library project which is added as reference project from the existing visual studio solution it throws up below issue:

"A host error has occurred during startup operation '945918c0-af3a-4d50-ab1d-ac405d4f1c7b'. [2/3/2020 2:11:02 PM] MyFunction.FunctionApp: Could not load file or assembly 'MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly ''MyCompany.MyLib.Contracts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

If these lines from "referenced external projects" are removed,

using MyCompany.MyLib.Contracts;
builder.Services.AddTransient<IBillingLogic, BillingLogic>();

startup.cs works as expected but referring this external class from referenced project is must for my solution.

My Azure function csproj file:

     <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <AzureFunctionsVersion>v2</AzureFunctionsVersion>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.0.0" />
        <PackageReference Include="Microsoft.Azure.Storage.Queue" Version="11.1.2" />
        <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Storage" Version="3.0.8" />
        <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0" />
        <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.29" />
        <PackageReference Include="NLog" Version="4.6.8" />
        <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
        <PackageReference Include="NLog.Extensions.AzureStorage" Version="1.1.4" />
        <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />

      </ItemGroup>
      <ItemGroup>
        <ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">
          <Private>true</Private>
        </ProjectReference>
      </ItemGroup>
      <ItemGroup>
        <None Update="appsettings.json">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
          <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        </None>
        <None Update="host.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
        <None Update="local.settings.json">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
          <CopyToPublishDirectory>Never</CopyToPublishDirectory>
        </None>
        <None Update="nlog.config">
          <CopyToOutputDirectory>Always</CopyToOutputDirectory>
          <CopyToPublishDirectory>Always</CopyToPublishDirectory>
        </None>
      </ItemGroup>
    </Project>

MyCSharpLib.DataStore.csproj file:

      <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <RuntimeIdentifier>win7-x64</RuntimeIdentifier>
        <Platforms>x64</Platforms>
      </PropertyGroup>

      <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" />

      <ItemGroup>
        <PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.6" />
        <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="2.9.2" />
        <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.1" />
        <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
        <PackageReference Include="Newtonsoft.Json.Bson" Version="1.0.1" />
        <PackageReference Include="Polly" Version="5.3.1" />
        <PackageReference Include="StackExchange.Redis" Version="1.2.6" />
      </ItemGroup>

      <ItemGroup>
        <ProjectReference Include="..\MyContractLib.Contracts\MyContractLib.Contracts.csproj" />          
      </ItemGroup>

    </Project>
c#
dependency-injection
azure-functions
azure-function-app
asked on Stack Overflow Feb 3, 2020 by 191180rk • edited Feb 4, 2020 by 191180rk

1 Answer

0

MyCSharpLib.DataStore

.\MyContractLib.Contracts\MyContractLib.Contracts.csproj

My Azure function csproj file:

<ProjectReference Include="..\MyCSharpLib.DataStore\MyCSharpLib.DataStore.csproj">

so

using MyCompany.MyLib.Contracts;

is coming through the ref to DataStore which then has ref to MyContractLib.Contracts

But it is not coping the dll as its silly, so either get Azure function csproj to ref MyLib.Contracts or do this How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?

which is on all your std libs add

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

so on both your standard libs

<PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>

if this does not work i will delete

answered on Stack Overflow Feb 4, 2020 by Seabizkit

User contributions licensed under CC BY-SA 3.0