【问题标题】:.NET Azure Functions - Dependency Injection Issue.NET Azure Functions - 依赖注入问题
【发布时间】:2020-02-03 14:59:00
【问题描述】:

在我的 Azure Function v2 项目中的 Startup.cs 上:

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
    }
}

}

上面的代码带有我自己在函数应用项目中的自定义类,比如“EmailLogic”、“Logger”工作正常。

但是,当我将自定义类添加到服务容器中时,例如“BillingLogic”来自外部 C# 库项目,它是作为参考项目从现有的 Visual Studio 解决方案添加的,它引发了以下问题:

在启动操作 '945918c0-af3a-4d50-ab1d-ac405d4f1c7b' 期间发生主机错误。[2/3/2020 2:11:02 PM] MyFunction.FunctionApp:无法加载文件或程序集'MyCompany.MyLib.Contracts,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null'。找不到或加载特定文件。(HRESULT 异常:0x80131621)。System.Private。 CoreLib:无法加载文件或程序集“MyCompany.MyLib.Contracts,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null

如果“引用的外部项目”中的这些行被删除,

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

startup.cs 按预期工作,但我的解决方案必须从引用的项目中引用这个外部类。

我的 Azure 函数 csproj 文件:

     <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 文件:

      <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>

【问题讨论】:

  • 嗨@prkat,你能把IBillingLogic 的接口放在它的实现上,只是为了确保你没有错过任何东西。
  • 但在加载 startup.cs 时发生错误,并且错误消息显示“无法加载文件或程序集 'MyCompany.MyLib.Contracts”,这是由文件顶部的“using”关键字引用的命名空间。由于这个错误,保留在“启动”类中的断点没有被命中。接口 IBillingLogic 及其实现没有问题,它可以被其他非 Azure 功能项目正确使用,没有任何问题,只有在 Azure 功能项目中使用它会产生问题。
  • @prkat 你能告诉我这两个项目的csproj 吗?
  • @HariHaran,附上两个 csproj 文件
  • 根据报错,我想你需要添加对另一个项目MyCompany.MyLib.Contracts的引用。请尝试将项目的dill添加到Azure函数项目中

标签: c# dependency-injection azure-functions azure-function-app


【解决方案1】:

MyCSharpLib.DataStore

.\MyContractLib.Contracts\MyContractLib.Contracts.csproj

我的 Azure 函数 csproj 文件:

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

所以

using MyCompany.MyLib.Contracts;

通过对 DataStore 的引用,然后引用到 MyContractLib.Contracts

但它并没有把 dll 当作愚蠢的东西来处理,所以要么让 Azure 函数 csproj 引用 MyLib.Contracts 或者这样做 How to set dependencies when I use .NET Standard 2.0 DLL libraries with a .NET Framework console application?

在你所有的标准库上添加

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

你的两个标准库都这样

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

如果这不起作用,我将删除

【讨论】:

  • 它不工作。我可以在 Azure 函数的 bin 文件夹中看到 MyLib.Contracts.dll,但在运行函数时显示“无法加载文件或程序集 'MyLib.Contracts'”
  • 嗯,我以为我有它。如果你把它加载到var DLL = Assembly.LoadFile("MyLib.Contracts")
  • 不确定&lt;Private&gt;true&lt;/Private&gt;
猜你喜欢
  • 2019-10-15
  • 2018-06-28
  • 2020-12-03
  • 1970-01-01
  • 2015-11-21
  • 2021-06-25
  • 2023-03-03
  • 1970-01-01
  • 2021-06-03
相关资源
最近更新 更多