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>
7
  • Hi @prkat, can you put the interface IBillingLogic with, it's implementation, just to make sure that you haven't missed anything. Feb 3, 2020 at 15:40
  • But error occurs while loading startup.cs and error message says "Could not load file or assembly 'MyCompany.MyLib.Contracts", which is namespace referred by "using" keyword on top of the file. Because of this error breakpoint kept at "Startup" class is not getting hit. No problem with interface IBillingLogic & its implementation it is consumed properly by other non-Azure-function project without any issues only consuming it in Azure function project creates issues.
    – 191180rk
    Feb 3, 2020 at 15:49
  • @prkat can you show me the csproj of both the projects ?
    – HariHaran
    Feb 4, 2020 at 4:12
  • @HariHaran, attached both csproj files
    – 191180rk
    Feb 4, 2020 at 4:16
  • According to the error, I think you need to add reference to another project MyCompany.MyLib.Contracts. please try to add the project's dill to the Azure function project
    – Jim Xu
    Feb 4, 2020 at 4:57

1 Answer 1

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

3
  • It is not working. I can see MyLib.Contracts.dll in bin folder of the Azure function but while running function it says "Could not load file or assembly 'MyLib.Contracts'"
    – 191180rk
    Feb 4, 2020 at 8:49
  • mmm thought i had it. and if you load it into the var DLL = Assembly.LoadFile("MyLib.Contracts")
    – Seabizkit
    Feb 4, 2020 at 10:41
  • unsure what <Private>true</Private>
    – Seabizkit
    Feb 4, 2020 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.