.NETStandard 1.1库中的接口在.NET 4.61中没有实现

编程入门 行业动态 更新时间:2024-10-23 15:26:32
本文介绍了.NETStandard 1.1库中的接口在.NET 4.61中没有实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在.NETStandard库中定义一个接口,该接口是 HttpClient 实例的工厂:

I'm defining an interface in a .NETStandard library that is a factory for a HttpClient instances:

namespace Ofl.Net.Http { public interface IHttpClientFactory { Task<HttpClient> CreateAsync(HttpMessageHandler handler, bool disposeHandler, CancellationToken cancellationToken); } }

< FrameworkTarget> .csproj文件中的元素设置为 netstandard1.1 ( HttpClient 要求1.1)

The <FrameworkTarget> element in the .csproj file is set to netstandard1.1 (HttpClient requires 1.1).

.NETStandard.Library 1.6.1是默认引用的(尽管未在.csproj文件中明确指定)。

.NETStandard.Library 1.6.1 is referenced by default (although not explicitly specified in the .csproj file).

在同一解决方案中,我从一个.NET Standard项目开始,并将< FrameworkTargets> 元素设置为 netcoreapp1.1; net461 。

In the same solution, I start with a .NET Standard project and set the <FrameworkTargets> element to netcoreapp1.1;net461.

我有一些测试可以测试只是创建接口的实现(我通常不会对此进行测试,但这可以将其减少为

I have some tests to test just creating an implementation of the interface (I wouldn't normally test this, but this reduces it to the smallest self contained reproducible example).

为此,我有一个 IHttpClientFactory 的私有实现:

To aid in this, I have a private implementation of IHttpClientFactory:

private class HttpClientFactory : IHttpClientFactory { #region Implementation of IHttpClientFactory public Task<HttpClient> CreateAsync(HttpMessageHandler handler, bool disposeHandler, CancellationToken cancellationToken) { // Return a new client. return Task.FromResult(new HttpClient()); } #endregion }

然后测试:

[Fact] public async Task Test_CreateAsync_Interface_Async() { // Cancellation token. CancellationToken cancellationToken = CancellationToken.None; // The client factory. IHttpClientFactory factory = new HttpClientFactory(); // Not null. Assert.NotNull(factory); // Create client. HttpClient client = await factory.CreateAsync(null, true, cancellationToken).ConfigureAwait(false); // Not null. Assert.NotNull(client); } [Fact] public async Task Test_CreateAsync_Concrete_Async() { // Cancellation token. CancellationToken cancellationToken = CancellationToken.None; // The client factory. var factory = new HttpClientFactory(); // Not null. Assert.NotNull(factory); // Create client. HttpClient client = await factory.CreateAsync(null, true, cancellationToken).ConfigureAwait(false); // Not null. Assert.NotNull(client); }

基本上,通过具有接口实现类型的变量进行调用,

Basically, making a call through a variable with a type of the interface implementation, and a variable with a type of the implementing class (that doesn't matter though).

运行测试时(通过Resharper或 dotnet)和带有实现类类型的变量(尽管没关系)。 .NETCoreApp 1.1框架中的测试)可以通过所有测试。但是,当在.NET Framework 4.6.1上运行时,我得到一个 System.TypeLoadException 指出没有实现:

When running the tests (through Resharper, or dotnet test) in .NETCoreApp 1.1 framework has all tests passing. However, when running on the .NET Framework 4.6.1, I get a System.TypeLoadException stating that there is no implementation:

System.TypeLoadException : Method 'CreateAsync' in type 'HttpClientFactory' from assembly 'Ofl.Net.Http.Abstractions.Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation. at Ofl.Net.Http.Abstractions.Tests.HttpClientFactoryTests.<Test_CreateAsync_Interface_Async>d__1.MoveNext() at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) at Ofl.Net.Http.Abstractions.Tests.HttpClientFactoryTests.Test_CreateAsync_Interface_Async()

为什么不在.NET Framework 4.6.1上运行时,测试通过了吗?

Why doesn't the test pass when running on the .NET Framework 4.6.1? There's most certainly an implementation.

我认为可能是因为未引用 System.Net.Http 测试项目(在接口定义中不是必需的,因为引用了.NETStandard 1.6.1,它引用了 System.Net.Http ),所以我确保添加了该项目,但是在.NET Framework 4.6.1下测试仍然失败。

I thought it could be because System.Net.Http wasn't referenced in the test project (not necessary in the interface definition because .NETStandard 1.6.1 is referenced, which references System.Net.Http) so I made sure to add that, but the test still fails under the .NET Framework 4.6.1.

完整的设置可以在这里找到:

The full setup can be found here:

github/OneFrameLink/Ofl.Net.Http.Abstractions / tree / c2bc5499b86e29c2e0a91282ca7dabcc1acc1176

信息

VS.NET 2017 .NET CLI:1.1.0 .NET桌面框架:4.61 .NET Core:1.1

VS.NET 2017 .NET CLI: 1.1.0 .NET Desktop Framework: 4.61 .NET Core: 1.1

推荐答案

这是因为没有为您的程序集和测试生成具有正确绑定重定向的 .dll.config 文件SDK并未设置必需的属性来自动执行此操作。

This is because no .dll.config file with the correct binding redirects is generated for your assembly and the Test SDK doesn't set the required properties to do this automatically.

您可以通过将其添加到测试的 .csproj 文件:

You can work around this by adding this to your test's .csproj file:

<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' "> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>

您会看到这会导致 bin\Debug\net461 generatedOfl.Net.Http.Abstractions.Tests.dll.config 文件将生成,其中包括 System.Net.Http 和您的测试应该可以正常运行。

You'll see that this causes a bin\Debug\net461\Ofl.Net.Http.Abstractions.Tests.dll.config file to be generated that includes binding redirects for System.Net.Http and your tests should run properly.

即将推出的 core / cli 2.0工具和test sdk工具将进行一些更改,可以解决此问题,但这可能被认为是当前工具中的错误。

The upcoming tooling for core / cli 2.0 and the test sdk will include a few changes that should fix this but this may be considered a bug in the current tooling.

更多推荐

.NETStandard 1.1库中的接口在.NET 4.61中没有实现

本文发布于:2023-11-16 18:45:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:库中   接口   NETStandard   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!