将.net Framework项目与.net Core项目(和语言)混合-System.IO.FileNotFoundException

时间:2019-06-06 22:37:03

标签: c# vb.net .net-standard-2.0 .net-core-2.2 .net-4.7.1

我有一个解决方案,其中大多数项目都是VB.net项目,它们都针对.NET Framework 4.8。所有这些特定项目都可以正确编译并在生产中运行。

我添加了一个针对.net标准2.0的C#项目。这个特定项目的主要原因是创建一个可以处理剃刀模板的组件(当然是给定的模型),以便使用RazorLight_wik8wz nuget包创建输出文件(我并不在乎我使用哪个包,只要我可以在内存中渲染剃刀模板,因为模型/模板对生成的输出在不同情况下可能会保存在不同的位置。

理论上,根据我的理解,我应该能够从我的其他.net Framework 4.8项目中引用和使用.net标准2.0库项目。但是,我似乎无法使它正常工作(即使所有内容都能编译并运行)。

我已经尝试了几种基于RazorLight的软件包-但是当我尝试使用它们时会得到相似的结果。我根本不确定是遇到错误还是做得不好。

C#项目的名称为“ JobOutputGenerator”。我添加了名为“ RazorLight_wik8wz”的nuget包,并创建了一个名为JobOutput的对象,该对象当前如下所示:

using System.Threading.Tasks;
using RazorLight;

public class JobOutput
{
    public static async Task<byte[]> CreateOutputAsync(string RazorTemplate, object Model)
    {
        RazorLightEngine engine = new RazorLightEngineBuilder().UseMemoryCachingProvider().Build();
        return System.Text.Encoding.ASCII.GetBytes(await engine.CompileRenderAsync(RazorTemplate.GetHashCode().ToString() + Model.GetHashCode().ToString(), RazorTemplate, Model));
    }

}

我创建了另一个名为“ TesterApp”的项目,这是一个针对.NET Framework 4.8的VB.NET项目。这是一个引用JobOutputGenerator的简单Windows窗体项目。

我有一个执行以下操作的按钮:

Private Sub btnRenderSample_Click(sender As Object, e As EventArgs) Handles btnRenderSample.Click
    Dim template As String = "Hello @Model.Name"
    Dim Model1 = New With {.Name = "John Doe"}
    Dim bytesTask = JobOutput.CreateOutputAsync(template, Model1)
    Dim str = System.Text.Encoding.ASCII.GetString(bytesTask.Result)
    MsgBox(str)
End Sub

当我在Visual Studio中以调试模式启动TesterApp时,所有项目都可以正常编译,然后当我单击示例按钮时,Visual Studio会在行上断开(同时以绿色突出显示):

Dim bytesTask = JobOutput.CreateOutputAsync(template, Model1)

带有一个标题为“未处理的异常”的框,其中显示:

  

System.IO.FileNotFoundException:'无法加载文件或程序集'RazorLight,版本= 2.0.0.0,区域性=中性,PublicKeyToken =空'或其依赖项之一。系统找不到指定的文件。'

我已经运行了fuslogvw,并且TesterApp可以毫无问题地找到JobOutputGenerator.dll:

<meta http-equiv="Content-Type" content="charset=unicode-1-1-utf-8"><!-- saved from url=(0015)assemblybinder: --><html><pre>
*** Assembly Binder Log Entry  (6/7/2019 @ 9:37:08 AM) ***

The operation was successful.
Bind result: hr = 0x0. The operation completed successfully.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\TesterApp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = JobOutputGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = TesterApp.exe
Calling assembly : TesterApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\TesterApp.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/JobOutputGenerator.DLL.
LOG: Assembly download was successful. Attempting setup of file: X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\JobOutputGenerator.dll
LOG: Entering run-from-source setup phase.
LOG: Assembly Name is: JobOutputGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
LOG: Binding succeeds. Returns assembly from X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\JobOutputGenerator.dll.
LOG: Assembly is loaded in default load context.

</pre></html>

但是,我确实在fuslogvw中找到了这个小“宝石”,但我不知道该怎么做:

*** Assembly Binder Log Entry  (6/7/2019 @ 9:37:09 AM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\TesterApp.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = RazorLight, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = TesterApp.exe
Calling assembly : JobOutputGenerator, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: X:\Workspaces\SOLUTIONROOTFOLDER\TesterApp\bin\Debug\TesterApp.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/RazorLight.DLL.
LOG: Attempting download of new URL file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/RazorLight/RazorLight.DLL.
LOG: Attempting download of new URL file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/RazorLight.EXE.
LOG: Attempting download of new URL file:///X:/Workspaces/SOLUTIONROOTFOLDER/TesterApp/bin/Debug/RazorLight/RazorLight.EXE.
LOG: All probing URLs attempted and failed.

以下是JobOutputGenerator.csproj的内容:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

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

  <PropertyGroup>
    <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.1" />
    <PackageReference Include="RazorLight_wik8wz" Version="2.0.0" />
  </ItemGroup>

      <Target Name="ILRepack" AfterTargets="Build">

        <PropertyGroup>
            <WorkingDirectory>$(MSBuildThisFileDirectory)bin\$(Configuration)\$(TargetFramework)</WorkingDirectory>
        </PropertyGroup>

        <ILRepack 
            OutputType="$(OutputType)" 
            MainAssembly="$(AssemblyName).dll" 
            OutputAssembly="$(AssemblyName).dll" 
            InputAssemblies="$(WorkingDirectory)\*.dll" 
            WilcardInputAssemblies="true"
            WorkingDirectory="$(WorkingDirectory)" />
    </Target>
</Project>

1 个答案:

答案 0 :(得分:1)

因此,在进行更多搜索之后,我遇到了以下问题和帖子集。遵循这种解决方法似乎目前已经解决了我的问题。

显然,自2017年以来,这一直是一个问题,感谢Microsoft!)。

问题首先在这里报告:

NuGet dependencies of .NET Standard library referenced from classic .NET app cannot be resolved at runtime with FileNotFoundException #1582

如果您仔细阅读了这串消息,您最终会发现 workaround靠近底部,其他人确认了此人的发现。按照这种解决方法,所有DLL文件都将生成/复制到Debug文件夹,现在引用的DLL不再“丢失”,并且我在运行时收到的异常消失了。

我对JobOutputGenerator .csproj文件进行了this修改,但我使用的是; net48而不是; net461。

注意标签:

<TargetFramework>

需要更改为(请注意“ s”)

<TargetFrameworks>

为了使它起作用,否则您的项目将无法在Visual Studio中正确加载。