All Projects → xljiulang → WebApiClient.Extensions

xljiulang / WebApiClient.Extensions

Licence: MIT license
WebApiClient项目的第三方扩展:Autofac、DependencyInjection、HttpClientFactory、SteeltoeOSS.Discovery、MessagePack、Protobuf、Json-Rpc

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to WebApiClient.Extensions

Rq
Record Query - A tool for doing record analysis and transformation
Stars: ✭ 1,808 (+2376.71%)
Mutual labels:  protobuf, messagepack
di speed
Speed comparison of Dependency Injection Container
Stars: ✭ 18 (-75.34%)
Mutual labels:  dependency-injection, autofac
KickStart
Application initialization helper
Stars: ✭ 42 (-42.47%)
Mutual labels:  dependency-injection, autofac
Autofac
An addictive .NET IoC container
Stars: ✭ 3,713 (+4986.3%)
Mutual labels:  dependency-injection, autofac
Gosercomp
⚡️ Golang Serializer Benchmark Comparison
Stars: ✭ 300 (+310.96%)
Mutual labels:  protobuf, messagepack
Hyperf
🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
Stars: ✭ 4,206 (+5661.64%)
Mutual labels:  dependency-injection, json-rpc
Blog.core
💖 ASP.NET Core 6.0 全家桶教程,前后端分离后端接口,vue教程姊妹篇,官方文档:
Stars: ✭ 3,542 (+4752.05%)
Mutual labels:  dependency-injection, autofac
Getty
a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Stars: ✭ 532 (+628.77%)
Mutual labels:  protobuf, json-rpc
Autofac.Extras.NLog
An Autofac module to integrate Autofac and NLog, it supports both constructor and property injection.
Stars: ✭ 48 (-34.25%)
Mutual labels:  dependency-injection, autofac
Autofac.Configuration
Configuration support for Autofac IoC
Stars: ✭ 35 (-52.05%)
Mutual labels:  autofac
gocontainer
Simple Dependency Injection Container
Stars: ✭ 18 (-75.34%)
Mutual labels:  dependency-injection
protokit
A starter kit for building protoc plugins. Rather than write your own, you can just use an existing one.
Stars: ✭ 69 (-5.48%)
Mutual labels:  protobuf
rules proto grpc
Bazel rules for building Protobuf and gRPC code and libraries from proto_library targets
Stars: ✭ 201 (+175.34%)
Mutual labels:  protobuf
transmission
Go wrapper for the transmission API
Stars: ✭ 34 (-53.42%)
Mutual labels:  json-rpc
Meshtastic-protobufs
Protobuf definitions for the Meshtastic project
Stars: ✭ 32 (-56.16%)
Mutual labels:  protobuf
stellarstation-api
The API definition for StellarStation.
Stars: ✭ 22 (-69.86%)
Mutual labels:  protobuf
molch
An implementation of the axolotl ratchet based on libsodium.
Stars: ✭ 24 (-67.12%)
Mutual labels:  protobuf
node-mole-rpc
Transport agnostic spec compliant JSON RPC client and server
Stars: ✭ 54 (-26.03%)
Mutual labels:  json-rpc
metastore
A protobuf schema registry on steroids. It will keep track of the contracts throughout your organization, making sure no contract is broken.
Stars: ✭ 43 (-41.1%)
Mutual labels:  protobuf
j2cl-protobuf
Protocol Buffers implementation for J2CL
Stars: ✭ 23 (-68.49%)
Mutual labels:  protobuf

WebApiClient.Extensions

WebApiClient项目的第三方扩展:AutofacDependencyInjectionHttpClientFactorySteeltoeOSS.DiscoveryMessagePackProtobufJson-Rpc

0 Autofac扩展

0.1 Nuget

PM> install-package WebApiClient.Extensions.Autofac
支持 netstandard1.3

0.2 使用方法

声明远程http服务的的WebApiClient调用接口

[HttpHost("https:/localhost:5000")]
public interface IValuesApi : IHttpApi
{
    [HttpGet("api/values")]
    ITask<string[]> GetAsync();

    [HttpGet("api/values/{id}")]
    ITask<string> GetAsync(int id);
}

注册和配置接口

var builder = new ContainerBuilder();
builder.RegisterHttpApi<IValuesApi>().ConfigureHttpApiConfig(c =>
{
    c.HttpHost = new Uri("http://localhost:9999/");
    c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
});

1 DependencyInjection扩展

1.1 Nuget

PM> install-package WebApiClient.Extensions.DependencyInjection
支持 netstandard2.0

1.2 使用方法

声明远程http服务的的WebApiClient调用接口

[HttpHost("https:/localhost:5000")]
public interface IValuesApi : IHttpApi
{
    [HttpGet("api/values")]
    ITask<string[]> GetAsync();

    [HttpGet("api/values/{id}")]
    ITask<string> GetAsync(int id);
}

Startup相关配置

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpApi<IValuesApi>().ConfigureHttpApiConfig(c =>
    {
        c.HttpHost = new Uri("http://localhost:9999/");
        c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    });
    ...
}

Controller

public class HomeController : Controller
{
    public async Task<string> Index([FromServices]IValuesApi api, int id = 0)
    {
        var values = await api.GetValuesAsync();
        var value = await api.GetValuesAsync(id);
        return "ok";
    }
}

2 HttpClientFactory扩展

2.1 Nuget

PM> install-package WebApiClient.Extensions.HttpClientFactory
支持 netstandard2.0

2.2 使用方法

声明远程http服务的的WebApiClient调用接口

[HttpHost("https:/localhost:5000")]
public interface IValuesApi : IHttpApi
{
    [HttpGet("api/values")]
    ITask<string[]> GetAsync();

    [HttpGet("api/values/{id}")]
    ITask<string> GetAsync(int id);
}

Startup相关配置

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{   
    services.AddHttpApiTypedClient<IValuesApi>(c =>
    {
        c.HttpHost = new Uri("http://localhost:9999/");
        c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    });
    ...
}

Controller

public class HomeController : Controller
{
    public async Task<string> Index([FromServices]IValuesApi api, int id = 0)
    {
        var values = await api.GetAsync();
        var value = await api.GetAsync(id);
        return "ok";
    }
}

3 DiscoveryClient扩展

3.1 Nuget

PM> install-package WebApiClient.Extensions.DiscoveryClient
支持 netstandard2.0

3.2 使用方法

声明微服务的WebApiClient调用接口

[HttpHost("http://VALUES")]
public interface IValuesApi : IHttpApi
{
    [HttpGet("api/values")]
    ITask<string[]> GetAsync();

    [HttpGet("api/values/{id}")]
    ITask<string> GetAsync(int id);
}

Startup相关配置

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddDiscoveryClient(Configuration);
    services.AddDiscoveryTypedClient<IValuesApi>(c =>
    {        
        c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
    });
    ...
}


// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    app.UseDiscoveryClient();
}

Controller

public class HomeController : Controller
{
    public async Task<string> Index([FromServices]IValuesApi api, int id = 0)
    {
        var values = await api.GetAsync();
        var value = await api.GetAsync(id);
        return "ok";
    }
}

4 MessagePack扩展

4.1 Nuget

PM> install-package WebApiClient.Extensions.MessagePack
支持 netstandard1.6 / net4.5

4.2 使用方法

声明远程http服务的的WebApiClient调用接口

[MessagePackReturn]
[HttpHost("https:/localhost:5000")]
public interface IUsersApi : IHttpApi
{
    [HttpGet("api/users/{id}")]
    ITask<UserInfo> GetAsync(int id);
    
    [HttpPut("api/users")]
    ITask<bool> PutAsync([MessagePackContent] UserInfo value);
}

asp.net core服务端MessagePack相关配置

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddMvcOptions(o =>
    {
        o.OutputFormatters.Add(new MessagePackOutputFormatter(ContractlessStandardResolver.Instance));
        o.InputFormatters.Add(new MessagePackInputFormatter(ContractlessStandardResolver.Instance));
    });
}



class MessagePackInputFormatter : InputFormatter
{
    private readonly IFormatterResolver resolver;

    private static readonly StringSegment mediaType = new StringSegment("application/x-msgpack");

    public MessagePackInputFormatter(IFormatterResolver resolver)
    {
        this.resolver = resolver ?? MessagePackSerializer.DefaultResolver;
        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));
    }

    public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        var body = context.HttpContext.Request.Body;
        var result = MessagePackSerializer.NonGeneric.Deserialize(context.ModelType, body, resolver);
        return InputFormatterResult.SuccessAsync(result);
    }
}

class MessagePackOutputFormatter : OutputFormatter
{
    private readonly IFormatterResolver resolver;

    private static readonly StringSegment mediaType = new StringSegment("application/x-msgpack");

    public MessagePackOutputFormatter(IFormatterResolver resolver)
    {
        this.resolver = resolver ?? MessagePackSerializer.DefaultResolver;
        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));
    }

    public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
    {
        if (context.ObjectType != typeof(object))
        {
            MessagePackSerializer.NonGeneric.Serialize(context.ObjectType, context.HttpContext.Response.Body, context.Object, resolver);
        }
        else if (context.Object == null)
        {
            context.HttpContext.Response.Body.WriteByte(MessagePackCode.Nil);
        }
        else
        {
            MessagePackSerializer.NonGeneric.Serialize(context.Object.GetType(), context.HttpContext.Response.Body, context.Object, resolver);
        }

        context.ContentType = mediaType;
        return Task.CompletedTask;
    }
}

5 Protobuf扩展

4.1 Nuget

PM> install-package WebApiClient.Extensions.Protobuf
支持 netstandard1.3 / net4.5

4.2 使用方法

声明远程http服务的的WebApiClient调用接口

[ProtobufReturn]
[HttpHost("https:/localhost:5000")]
public interface IUsersApi : IHttpApi
{
    [HttpGet("api/users/{id}")]
    ITask<UserInfo> GetAsync(int id);
    
    [HttpPut("api/users")]
    ITask<bool> PutAsync([ProtobufContent] UserInfo value);
}

asp.net core服务端MessagePack相关配置

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddMvcOptions(o =>
    {
        o.OutputFormatters.Add(new ProtobufOutputFormatter());
        o.InputFormatters.Add(new ProtobufInputFormatter());
    });
}


class ProtobufInputFormatter : InputFormatter
{
    private static readonly StringSegment mediaType = new StringSegment("application/x-protobuf");

    public ProtobufInputFormatter()
    {
        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));
    }

    public override Task<InputFormatterResult> ReadRequestBodyAsync(InputFormatterContext context)
    {
        var body = context.HttpContext.Request.Body;
        var model = Serializer.NonGeneric.Deserialize(context.ModelType, body);
        return InputFormatterResult.SuccessAsync(model);
    }
}

class ProtobufOutputFormatter : OutputFormatter
{
    private static readonly StringSegment mediaType = new StringSegment("application/x-protobuf");

    public ProtobufOutputFormatter()
    {
        this.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue(mediaType));
    }

    public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
    {
        var body = context.HttpContext.Response.Body;
        Serializer.NonGeneric.Serialize(body, context.Object);
        context.ContentType = mediaType;
        return Task.CompletedTask;
    }
}

6 Json-Rpc扩展

6.1 Nuget

PM> install-package WebApiClient.Extensions.JsonRpc
支持 netstandard1.3 / net4.5

4.2 使用方法

声明远程Rpc服务的的WebApiClient调用接口

[HttpHost("http://localhost:6800/jsonrpc")]
public interface Aria2 : IHttpApi
{
    [JsonRpcMethod("aria2.addUri")]
    ITask<JsonRpcResult<string>> AddUriAsync([RpcParam] params string[] uri);
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].