簡體   English   中英

.NET Azure Functions - 依賴注入問題

[英].NET Azure Functions - Dependency Injection Issue

在我的 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”工作正常。

但是,當我將自定義類添加到服務容器中時,例如來自外部 C# 庫項目的“BillingLogic” 該項目作為參考項目從現有的 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>

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 要么這樣做當我將 .NET Standard 2.0 DLL 庫與 .NET Framework 控制台應用程序一起使用時如何設置依賴項?

這是你所有的標准庫添加

<RestoreProjectStyle>PackageReference</RestoreProjectStyle>

所以在你的標准庫上

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

如果這不起作用,我將刪除

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM