简体   繁体   English

.NET Azure Functions - 依赖注入问题

[英].NET Azure Functions - Dependency Injection Issue

On Startup.cs in my Azure Function v2 project:在我的 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
    }
}

} }

The above code with my own custom classes within function app project like "EmailLogic", "Logger" works fine.上面的代码在函数应用程序项目中使用我自己的自定义类,如“EmailLogic”、“Logger”工作正常。

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

" 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 " "启动操作期间发生主机错误 '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 "

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. startup.cs 按预期工作,但我的解决方案必须从引用的项目中引用此外部类。

My Azure function csproj file:我的 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 file: 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>

MyCSharpLib.DataStore MyCSharpLib.DataStore

.\MyContractLib.Contracts\MyContractLib.Contracts.csproj

My Azure function csproj file:我的 Azure 函数 csproj 文件:

<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正在通过对 DataStore 的引用,然后对 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?但它并没有把 dll 当作它的愚蠢来处理,所以要么让 Azure 函数 csproj 引用 MyLib.Contracts 要么这样做当我将 .NET Standard 2.0 DLL 库与 .NET Framework 控制台应用程序一起使用时如何设置依赖项?

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如果这不起作用,我将删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM