{"id":19991906,"url":"https://github.com/xljiulang/WebApiClient.Extensions","last_synced_at":"2025-05-04T11:30:27.884Z","repository":{"id":60773809,"uuid":"149910104","full_name":"xljiulang/WebApiClient.Extensions","owner":"xljiulang","description":"WebApiClient项目的第三方扩展：Autofac、DependencyInjection、HttpClientFactory、SteeltoeOSS.Discovery、MessagePack、Protobuf、Json-Rpc","archived":false,"fork":false,"pushed_at":"2020-05-25T08:17:17.000Z","size":45,"stargazers_count":71,"open_issues_count":6,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-30T09:12:24.482Z","etag":null,"topics":["autofac","dependency-injection","discoveryclient","httpclientfactory","json-rpc","messagepack","protobuf","webapiclient"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xljiulang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-09-22T19:28:08.000Z","updated_at":"2024-02-05T03:14:11.000Z","dependencies_parsed_at":"2022-10-04T15:28:40.512Z","dependency_job_id":null,"html_url":"https://github.com/xljiulang/WebApiClient.Extensions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xljiulang%2FWebApiClient.Extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xljiulang%2FWebApiClient.Extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xljiulang%2FWebApiClient.Extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xljiulang%2FWebApiClient.Extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xljiulang","download_url":"https://codeload.github.com/xljiulang/WebApiClient.Extensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252323478,"owners_count":21729568,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["autofac","dependency-injection","discoveryclient","httpclientfactory","json-rpc","messagepack","protobuf","webapiclient"],"created_at":"2024-11-13T04:51:57.761Z","updated_at":"2025-05-04T11:30:27.477Z","avatar_url":"https://github.com/xljiulang.png","language":"C#","funding_links":[],"categories":["C\\#"],"sub_categories":[],"readme":"# WebApiClient.Extensions\n[WebApiClient](https://github.com/dotnetcore/WebApiClient)项目的第三方扩展：[Autofac](https://github.com/autofac/Autofac)、[DependencyInjection](https://github.com/aspnet/DependencyInjection)、[HttpClientFactory](https://github.com/aspnet/HttpClientFactory)、[SteeltoeOSS.Discovery](https://github.com/SteeltoeOSS/Discovery)、[MessagePack](https://github.com/neuecc/MessagePack-CSharp)、[Protobuf](https://github.com/mgravell/protobuf-net)、[Json-Rpc](https://www.jsonrpc.org/specification)\n\n\n\n### 0 Autofac扩展\n\n#### 0.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.Autofac`\n\u003cbr/\u003e支持 netstandard1.3\n\n#### 0.2 使用方法\n\u003e 声明远程http服务的的WebApiClient调用接口\n\n```c#\n[HttpHost(\"https:/localhost:5000\")]\npublic interface IValuesApi : IHttpApi\n{\n    [HttpGet(\"api/values\")]\n    ITask\u003cstring[]\u003e GetAsync();\n\n    [HttpGet(\"api/values/{id}\")]\n    ITask\u003cstring\u003e GetAsync(int id);\n}\n```\n\u003e 注册和配置接口\n```c#\nvar builder = new ContainerBuilder();\nbuilder.RegisterHttpApi\u003cIValuesApi\u003e().ConfigureHttpApiConfig(c =\u003e\n{\n    c.HttpHost = new Uri(\"http://localhost:9999/\");\n    c.FormatOptions.DateTimeFormat = \"yyyy-MM-dd HH:mm:ss.fff\";\n});\n```\n\n### 1 DependencyInjection扩展\n\n#### 1.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.DependencyInjection`\n\u003cbr/\u003e支持 netstandard2.0 \n\n#### 1.2 使用方法\n\u003e 声明远程http服务的的WebApiClient调用接口\n\n```c#\n[HttpHost(\"https:/localhost:5000\")]\npublic interface IValuesApi : IHttpApi\n{\n    [HttpGet(\"api/values\")]\n    ITask\u003cstring[]\u003e GetAsync();\n\n    [HttpGet(\"api/values/{id}\")]\n    ITask\u003cstring\u003e GetAsync(int id);\n}\n```\n\n\u003e Startup相关配置\n\n```c#\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddHttpApi\u003cIValuesApi\u003e().ConfigureHttpApiConfig(c =\u003e\n    {\n        c.HttpHost = new Uri(\"http://localhost:9999/\");\n        c.FormatOptions.DateTimeFormat = \"yyyy-MM-dd HH:mm:ss.fff\";\n    });\n    ...\n}\n```\n\n\u003e Controller\n\n```c#\npublic class HomeController : Controller\n{\n    public async Task\u003cstring\u003e Index([FromServices]IValuesApi api, int id = 0)\n    {\n        var values = await api.GetValuesAsync();\n        var value = await api.GetValuesAsync(id);\n        return \"ok\";\n    }\n}\n```\n\n### 2 HttpClientFactory扩展\n\n#### 2.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.HttpClientFactory`\n\u003cbr/\u003e支持 netstandard2.0 \n\n#### 2.2 使用方法\n\u003e 声明远程http服务的的WebApiClient调用接口\n\n```c#\n[HttpHost(\"https:/localhost:5000\")]\npublic interface IValuesApi : IHttpApi\n{\n    [HttpGet(\"api/values\")]\n    ITask\u003cstring[]\u003e GetAsync();\n\n    [HttpGet(\"api/values/{id}\")]\n    ITask\u003cstring\u003e GetAsync(int id);\n}\n```\n\n\u003e Startup相关配置\n\n```c#\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{   \n    services.AddHttpApiTypedClient\u003cIValuesApi\u003e(c =\u003e\n    {\n        c.HttpHost = new Uri(\"http://localhost:9999/\");\n        c.FormatOptions.DateTimeFormat = \"yyyy-MM-dd HH:mm:ss.fff\";\n    });\n    ...\n}\n```\n\n\u003e Controller\n\n```c#\npublic class HomeController : Controller\n{\n    public async Task\u003cstring\u003e Index([FromServices]IValuesApi api, int id = 0)\n    {\n        var values = await api.GetAsync();\n        var value = await api.GetAsync(id);\n        return \"ok\";\n    }\n}\n```\n### 3 DiscoveryClient扩展\n\n#### 3.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.DiscoveryClient`\n\u003cbr/\u003e支持 netstandard2.0 \n\n#### 3.2 使用方法\n\u003e 声明微服务的WebApiClient调用接口\n\n```c#\n[HttpHost(\"http://VALUES\")]\npublic interface IValuesApi : IHttpApi\n{\n    [HttpGet(\"api/values\")]\n    ITask\u003cstring[]\u003e GetAsync();\n\n    [HttpGet(\"api/values/{id}\")]\n    ITask\u003cstring\u003e GetAsync(int id);\n}\n```\n\n\u003e Startup相关配置\n\n```c#\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    services.AddDiscoveryClient(Configuration);\n    services.AddDiscoveryTypedClient\u003cIValuesApi\u003e(c =\u003e\n    {        \n        c.FormatOptions.DateTimeFormat = \"yyyy-MM-dd HH:mm:ss.fff\";\n    });\n    ...\n}\n\n\n// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.\npublic void Configure(IApplicationBuilder app, IHostingEnvironment env)\n{\n    ...\n    app.UseDiscoveryClient();\n}\n```\n\n\u003e Controller\n\n```c#\npublic class HomeController : Controller\n{\n    public async Task\u003cstring\u003e Index([FromServices]IValuesApi api, int id = 0)\n    {\n        var values = await api.GetAsync();\n        var value = await api.GetAsync(id);\n        return \"ok\";\n    }\n}\n```\n \n### 4 MessagePack扩展\n\n#### 4.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.MessagePack `\n\u003cbr/\u003e支持 netstandard1.6 / net4.5 \n\n#### 4.2 使用方法\n\u003e 声明远程http服务的的WebApiClient调用接口\n\n```c#\n[MessagePackReturn]\n[HttpHost(\"https:/localhost:5000\")]\npublic interface IUsersApi : IHttpApi\n{\n    [HttpGet(\"api/users/{id}\")]\n    ITask\u003cUserInfo\u003e GetAsync(int id);\n    \n    [HttpPut(\"api/users\")]\n    ITask\u003cbool\u003e PutAsync([MessagePackContent] UserInfo value);\n}\n```\n\n\u003e `asp.net core`服务端MessagePack相关配置\n\n```c#\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    ...\n    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddMvcOptions(o =\u003e\n    {\n        o.OutputFormatters.Add(new MessagePackOutputFormatter(ContractlessStandardResolver.Instance));\n        o.InputFormatters.Add(new MessagePackInputFormatter(ContractlessStandardResolver.Instance));\n    });\n}\n\n\n\nclass MessagePackInputFormatter : InputFormatter\n{\n    private readonly IFormatterResolver resolver;\n\n    private static readonly StringSegment mediaType = new StringSegment(\"application/x-msgpack\");\n\n    public MessagePackInputFormatter(IFormatterResolver resolver)\n    {\n        this.resolver = resolver ?? MessagePackSerializer.DefaultResolver;\n        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));\n    }\n\n    public override Task\u003cInputFormatterResult\u003e ReadRequestBodyAsync(InputFormatterContext context)\n    {\n        var body = context.HttpContext.Request.Body;\n        var result = MessagePackSerializer.NonGeneric.Deserialize(context.ModelType, body, resolver);\n        return InputFormatterResult.SuccessAsync(result);\n    }\n}\n\nclass MessagePackOutputFormatter : OutputFormatter\n{\n    private readonly IFormatterResolver resolver;\n\n    private static readonly StringSegment mediaType = new StringSegment(\"application/x-msgpack\");\n\n    public MessagePackOutputFormatter(IFormatterResolver resolver)\n    {\n        this.resolver = resolver ?? MessagePackSerializer.DefaultResolver;\n        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));\n    }\n\n    public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)\n    {\n        if (context.ObjectType != typeof(object))\n        {\n            MessagePackSerializer.NonGeneric.Serialize(context.ObjectType, context.HttpContext.Response.Body, context.Object, resolver);\n        }\n        else if (context.Object == null)\n        {\n            context.HttpContext.Response.Body.WriteByte(MessagePackCode.Nil);\n        }\n        else\n        {\n            MessagePackSerializer.NonGeneric.Serialize(context.Object.GetType(), context.HttpContext.Response.Body, context.Object, resolver);\n        }\n\n        context.ContentType = mediaType;\n        return Task.CompletedTask;\n    }\n}\n```\n \n\n### 5 Protobuf扩展\n\n#### 4.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.Protobuf`\n\u003cbr/\u003e支持 netstandard1.3 / net4.5 \n\n#### 4.2 使用方法\n\u003e 声明远程http服务的的WebApiClient调用接口\n\n```c#\n[ProtobufReturn]\n[HttpHost(\"https:/localhost:5000\")]\npublic interface IUsersApi : IHttpApi\n{\n    [HttpGet(\"api/users/{id}\")]\n    ITask\u003cUserInfo\u003e GetAsync(int id);\n    \n    [HttpPut(\"api/users\")]\n    ITask\u003cbool\u003e PutAsync([ProtobufContent] UserInfo value);\n}\n```\n\n\u003e `asp.net core`服务端MessagePack相关配置\n\n```c#\n// This method gets called by the runtime. Use this method to add services to the container.\npublic void ConfigureServices(IServiceCollection services)\n{\n    ...\n    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddMvcOptions(o =\u003e\n    {\n        o.OutputFormatters.Add(new ProtobufOutputFormatter());\n        o.InputFormatters.Add(new ProtobufInputFormatter());\n    });\n}\n\n\nclass ProtobufInputFormatter : InputFormatter\n{\n    private static readonly StringSegment mediaType = new StringSegment(\"application/x-protobuf\");\n\n    public ProtobufInputFormatter()\n    {\n        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));\n    }\n\n    public override Task\u003cInputFormatterResult\u003e ReadRequestBodyAsync(InputFormatterContext context)\n    {\n        var body = context.HttpContext.Request.Body;\n        var model = Serializer.NonGeneric.Deserialize(context.ModelType, body);\n        return InputFormatterResult.SuccessAsync(model);\n    }\n}\n\nclass ProtobufOutputFormatter : OutputFormatter\n{\n    private static readonly StringSegment mediaType = new StringSegment(\"application/x-protobuf\");\n\n    public ProtobufOutputFormatter()\n    {\n        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));\n    }\n\n    public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)\n    {\n        var body = context.HttpContext.Response.Body;\n        Serializer.NonGeneric.Serialize(body, context.Object);\n        context.ContentType = mediaType;\n        return Task.CompletedTask;\n    }\n}\n```\n \n### 6 Json-Rpc扩展\n#### 6.1 Nuget\nPM\u003e `install-package WebApiClient.Extensions.JsonRpc`\n\u003cbr/\u003e支持 netstandard1.3 / net4.5 \n\n#### 4.2 使用方法\n\u003e 声明远程Rpc服务的的WebApiClient调用接口\n\n```c#\n[HttpHost(\"http://localhost:6800/jsonrpc\")]\npublic interface Aria2 : IHttpApi\n{\n    [JsonRpcMethod(\"aria2.addUri\")]\n    ITask\u003cJsonRpcResult\u003cstring\u003e\u003e AddUriAsync([RpcParam] params string[] uri);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxljiulang%2FWebApiClient.Extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxljiulang%2FWebApiClient.Extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxljiulang%2FWebApiClient.Extensions/lists"}