Net8 自带OpenFeign实现远程接口调用

【c#版本Openfeign】Net8 自带OpenFeign实现远程接口调用
引言
相信巨硬,我们便一直硬。Net版本到现在已经出了7了,8也已经在预览版了,相信在一个半月就会正式发布,其中也有很多拭目以待的新功能了,不仅仅有Apm和Tap的结合,TaskToAscynResult,以及UnsafeAccessor用来获取私有变量,性能比反射,EMIT更高,还有针对AsyncLocal封装的IAsyncContext,IAsyncState,用来存异步上下文的一些数据,当然了,最让我期待的还是自带了一个OpenFeign,在看新增的东西的时候,其他的都觉得一般般,个人觉得哈,当看到这个AutoClient新增的包的时候,好奇心的驱使下,我点进去看了一下,哇,官网终于出这玩意了,使用简单,根据特性,然后使用Sg来生成我们对应的实现从而我们只需要定义一个接口,打上特性,就可以生成一个对应的代理类,调用远程Api接口,太令人心动,为此特地升级了VS,下载了Net8,体验新功能,接下来,我们就看看他的使用案例。附官网链接:https://learn.microsoft.com/zh-cn/dotnet/api/microsoft.extensions.http.autoclient.autoclientattribute?view=dotnet-plat-ext-8.0
AutoClient
在使用自带的OpenFeign的时候,我们还需要下载一个扩展包 Microsoft.Extensions.Http.AutoClient,当然还有 Microsoft.Extensions.Http的扩展包了,接下来我们定义一个接口,IBussiness,打上AutoClient特性,第一个参数是我们在注入Httpclient的时候,给的名字,我这里叫TestApi,这里会根据使用了AutoClient特性自定生成一个BussIness的类,在下图可以看到,自动生成了一个AutoClient.g.cs文件,里面的类就是Bussiness,其中包括了我们的TestPost方法以及路由信息,在上面的代码中,我们使用了Post特性,代表我们这个是Post请求,以及方法参数限制必须有一个CancellationToken,这个Post里面的内容,就是我另外一个项目种的接口地址。
builder.Services.AddHttpClient("TestApi",s=>s.BaseAddress=new Uri(" http://localhost:5062"));
[AutoClient( Task<>
// <auto-generated/>#nullable enable#pragma warning disable CS1591 // Compensate for https://github.com/dotnet/roslyn/issues/54103namespace WebApplication1.Api
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Gen.AutoClient", "8.0.0.0")] public class Bussiness : IBussiness
{ private static class Statics
{ public static readonly global::System.Net.Http.Headers.MediaTypeHeaderValue ApplicationJsonHeader = new("application/json")
{
CharSet = global::System.Text.Encoding.UTF8.WebName
}; public static readonly global::System.Net.Http.Headers.MediaTypeHeaderValue TextPlainHeader = new("text/plain")
{
CharSet = global::System.Text.Encoding.UTF8.WebName
}; public static readonly global::System.Uri UriTestPost = new("/Test/TestPost", global::System.UriKind.Relative); public static readonly global::Microsoft.Extensions.Http.Telemetry.RequestMetadata RequestMetadataTestPost = new()
{
DependencyName = "Bussiness",
RequestName = "TestPost",
RequestRoute = "/Test/TestPost"
};
} private readonly global::System.Net.Http.HttpClient _httpClient; private readonly global::Microsoft.Extensions.Http.AutoClient.AutoClientOptions _autoClientOptions; public Bussiness(global::System.Net.Http.HttpClient httpClient, global::Microsoft.Extensions.Http.AutoClient.AutoClientOptions autoClientOptions)
{
_httpClient = httpClient;
_autoClientOptions = autoClientOptions;
} public async global::System.Threading.Tasks.Task<string> TestPost(global::System.Threading.CancellationToken cancellationToken)
{ var httpRequestMessage = new global::System.Net.Http.HttpRequestMessage()
{
Method = global::System.Net.Http.HttpMethod.Post,
RequestUri = Statics.UriTestPost,
}; try
{ global::Microsoft.Extensions.Telemetry.TelemetryExtensions.SetRequestMetadata(httpRequestMessage, Statics.RequestMetadataTestPost); return await SendRequest<string>("Bussiness", Statics.RequestMetadataTestPost.RequestRoute, httpRequestMessage, cancellationToken)
.ConfigureAwait(false);
} finally
{
httpRequestMessage.Dispose();
}
} private async global::System.Threading.Tasks.Task<TResponse> SendRequest<TResponse>( string dependencyName, string path, global::System.Net.Http.HttpRequestMessage httpRequestMessage, global::System.Threading.CancellationToken cancellationToken) where TResponse : class
{ var response = await _httpClient.SendAsync(httpRequestMessage, cancellationToken).ConfigureAwait(false); if (typeof(TResponse) == typeof(global::System.Net.Http.HttpResponseMessage))
{ return (response as TResponse)!;
} try
{ if (!response.IsSuccessStatusCode)
{ var error = await global::Microsoft.Extensions.Http.AutoClient.AutoClientHttpError.CreateAsync(response, cancellationToken).ConfigureAwait(false); throw new global::Microsoft.Extensions.Http.AutoClient.AutoClientException(global::System.FormattableString.Invariant($"The '{dependencyName}' HTTP client failed with '{response.StatusCode}' status code."), path, error);
} if (typeof(TResponse) == typeof(string))
{#if NET5_0_OR_GREATER var rawContent = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);#else
cancellationToken.ThrowIfCancellationRequested(); var rawContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);#endif
return (rawContent as TResponse)!;
} var mediaType = response.Content.Headers.ContentType?.MediaType; if (mediaType == "application/json")
{ Net8 自带OpenFeign实现远程接口调用
声明:除非特别标注,否则均为本站原创文章,转载时请以链接形式注明文章出处。如若本站内容侵犯了原著者的合法权益,可联系本站删除。




