EzrealClient fork自WebApiClient,现在计划作为一个独立且激进的项目继续发展,如果您希望运用于生产环境,建议移步到上游项目。
WebApiClient是集高性能高可扩展性于一体的声明式http客户端库,特别适用于微服务的restful资源请求,也适用于各种畸形http接口请求。以下对其简称【上游】
增加了PreConfigureApi,即通过一个流式配置的Api,在不侵入接口的前提下,将接口注册为Http接口
示例代码test/ConsoleApp/Program.cs
using EzrealClient;
using EzrealClient.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
namespace ConsoleApp
{
class Program
{
static async Task Main(string[] args)
{
IServiceCollection services = new ServiceCollection();
services.AddLogging(builder => builder.AddConsole());
services
.AddEzrealClient()
.PreConfigureApiMetadata(builder =>
{
builder.TryAddApiFilterAttribute(new LoggingFilterAttribute());
builder.TryAddApiActionAttribute(new HttpHostAttribute("https://www.baidu.com"));
builder.SetCacheAttribute(new CacheAttribute(500));
builder.ConfigureInterface<ITestInterface>(interfaceBuilder =>
{
interfaceBuilder.SetCacheAttribute(new CacheAttribute(1000));
interfaceBuilder.ConfigureMethod(nameof(ITestInterface.Post), methodBuilder =>
{
methodBuilder.SetCacheAttribute(new CacheAttribute(5000));
methodBuilder
.ConfigureParameter("name", parameterBuilder =>
{
parameterBuilder.AliasAs("NewName");
}).ConfigureParameter("testClass", parameterBuilder =>
{
parameterBuilder.AddApiParameterAttribute(new JsonContentAttribute());
});
methodBuilder.TryAddApiActionAttribute(new HttpPostAttribute("api/PostTest"));
});
});
});
services.AddHttpApi<ITestInterface>();
var sp = services.BuildServiceProvider();
var i = sp.GetRequiredService<ITestInterface>();
try
{
await i.Post("testValue", new TestClass { Age = 10, Name = "xiaoming" });
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
Console.ReadKey();
}
}
public class TestClass
{
public int Age { get; set; }
public string Name { get; set; }
}
public interface ITestInterface
{
ITask<string> Post(string name, TestClass testClass);
}
}
暂时也没有什么头绪
可能会移除上游项目中的一些限制
会视情况跟进一些上游项目新的改进
如果不错的,值得称赞的改进引入,将会尽可能的贡献至上游
EzrealClient暂不承诺任何功能的稳定,已有代码在任何时候都有可能被编辑或删除
正在考虑后续的改进是否会发Nuget包
825135345上游项目的qq群,在群里@Ezreal可以找到我
进群时请注明WebApiClient,在咨询问题之前,请先认真阅读以下剩余的文档.