This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Caller): Fix IRequestMessage life cycle (#68)
* fix(Caller): Fix IRequestMessage life cycle * docs(Caller): Improve Caller documentation * chore: Adjust AddCaller * test(Caller): Add RequestMessage unit test
- Loading branch information
1 parent
5f300fa
commit fed40e5
Showing
16 changed files
with
580 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[中](README.zh-CN.md) | EN | ||
|
||
## Masa.Utils.Caller.Core | ||
|
||
Masa.Utils.Caller.Core is the basic class library of Caller, which provides the abstraction of the following capabilities | ||
|
||
* `ICallerFactory`: Factory for creating `CallerProvider` (Singleton) | ||
* `ICallerProvider`: Provides `Post`, `Delete`, `Patch`, `Put`, `Get`, `Send` capabilities (Scoped) | ||
* `IRequestMessage`: Provides the ability to process request data (default implementation [`JsonRequestMessage`](./JsonRequestMessage.cs)) (Singleton) | ||
* `IResponseMessage`: Provides the ability to handle response data (default implementation [`DefaultResponseMessage`](./DefaultResponseMessage.cs)) (Singleton) | ||
* `ITypeConvertProvider`: Provides the ability to convert types, support for `Get` requests of `ICallerProvider` (Singleton) | ||
|
||
## Summarize | ||
|
||
`Masa.Utils.Caller.Core` is the basic class library of Caller, but it cannot be used alone. Currently, Caller supports two implementations: | ||
|
||
* Implementation based on HttpClient: [Masa.Utils.Caller.HttpClient](../Masa.Utils.Caller.HttpClient/README.md) | ||
* Implementation based on DaprClient: [Masa.Utils.Caller.DaprClient](../Masa.Utils.Caller.DaprClient/README.md) | ||
|
||
> Q: What should I do if the callee uses xml instead of json? | ||
> | ||
> A: Rewrite IRequestMessage and add the custom RequestMessage to the IServiceCollection before calling AddCaller | ||
```` C# | ||
services.AddSingleton<IRequestMessage, XmlRequestMessage>(); | ||
services.AddCaller(); | ||
```` | ||
|
||
> Q: If you want to handle custom StatusCode and throw exception information | ||
> | ||
> A: Rewrite IResponseMessage, add custom ResponseMessage to IServiceCollection before calling AddCaller | ||
|
||
```` C# | ||
services.AddSingleton<IResponseMessage, CustomizeResponseMessage>(); | ||
services.AddCaller(); | ||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
中 | [EN](README.md) | ||
|
||
## Masa.Utils.Caller.Core | ||
|
||
Masa.Utils.Caller.Core是Caller的基础类库,提供了以下能力的抽象 | ||
|
||
* `ICallerFactory`: 工厂,用于创建`CallerProvider` (Singleton) | ||
* `ICallerProvider`: 提供`Post`、`Delete`、`Patch`、`Put`、`Get`、`Send`的能力 (Scoped) | ||
* `IRequestMessage`: 提供对请求数据处理的能力 (默认实现[`JsonRequestMessage`](./JsonRequestMessage.cs)) (Singleton) | ||
* `IResponseMessage`: 提供对响应数据处理的能力 (默认实现[`DefaultResponseMessage`](./DefaultResponseMessage.cs)) (Singleton) | ||
* `ITypeConvertProvider`: 提供类型转换的能力,为`ICallerProvider`的`Get`请求支撑 (Singleton) | ||
|
||
## 总结 | ||
|
||
`Masa.Utils.Caller.Core`是Caller的基础类库,但不能单独使用,目前Caller支持了两种实现方式: | ||
|
||
* 基于HttpClient的实现: [Masa.Utils.Caller.HttpClient](../Masa.Utils.Caller.HttpClient/README.zh-CN.md) | ||
* 基于DaprClient的实现: [Masa.Utils.Caller.DaprClient](../Masa.Utils.Caller.DaprClient/README.zh-CN.md) | ||
|
||
> Q: 如果被调用方使用的是数据格式为xml,而不是json,我应该怎么做? | ||
> | ||
> A: 重写IRequestMessage,在调用AddCaller之前先将自定义的RequestMessage添加到IServiceCollection中 | ||
``` C# | ||
services.AddSingleton<IRequestMessage, XmlRequestMessage>(); | ||
services.AddCaller(); | ||
``` | ||
|
||
> Q: 如果希望处理自定义的StatusCode,并抛出异常信息 | ||
> | ||
> A: 重写IResponseMessage,在调用AddCaller之前先将自定义的ResponseMessage添加到IServiceCollection中 | ||
|
||
``` C# | ||
services.AddSingleton<IResponseMessage, CustomizeResponseMessage>(); | ||
services.AddCaller(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
[中](README.zh-CN.md) | EN | ||
|
||
## Masa.Utils.Caller.DaprClient | ||
|
||
## Example: | ||
|
||
````c# | ||
Install-Package Masa.Utils.Caller.DaprClient | ||
```` | ||
|
||
### Basic usage: | ||
|
||
1. Modify `Program.cs` | ||
|
||
```` C# | ||
builder.Services.AddCaller(options => | ||
{ | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "UserCaller";// The alias of the current Caller, when there is only one Provider, you can not assign a value to Name | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//AppID of the callee dapr | ||
}); | ||
}); | ||
```` | ||
|
||
2. How to use: | ||
|
||
```` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) | ||
=> userCallerProvider.GetAsync<string>($"/Hello", new { Name = name })); | ||
```` | ||
|
||
> The interface address of the complete request is: http://localhost:3500/v1.0/invoke/<Replace-You-Dapr-AppID>/method/Hello?Name={name} | ||
3. When there are multiple DaprClients, modify `Program.cs` | ||
|
||
```` C# | ||
builder.Services.AddCaller(options => | ||
{ | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "UserCaller"; | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//AppID of the callee User service Dapr | ||
}); | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "OrderCaller"; | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//AppID of the callee Order service Dapr | ||
}); | ||
}); | ||
```` | ||
|
||
4. How to use UserCaller or OrderCaller | ||
|
||
```` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) | ||
=> userCallerProvider.GetAsync<string>($"/Hello", new { Name = name })); | ||
|
||
|
||
app.MapGet("/Test/Order/Hello", ([FromServices] ICallerFactory callerFactory, string name) => | ||
{ | ||
var callerProvider = callerFactory.CreateClient("OrderCaller"); | ||
return callerProvider.GetAsync<string>($"/Hello", new { Name = name }); | ||
}); | ||
```` | ||
|
||
> When multiple Callers are added, how to get the specified Caller? | ||
>> Get the CallerProvider of the specified alias through the `CreateClient` method of `CallerFactory` | ||
> | ||
> Why doesn't `userCallerProvider` get the corresponding Caller through the `CreateClient` method of `CallerFactory`? | ||
>> If no default ICallerProvider is specified, the default CallerProvider is the first one added in the `AddCaller` method | ||
|
||
#### Recommended usage | ||
|
||
1. Modify `Program.cs` | ||
|
||
```` C# | ||
builder.Services.AddCaller(); | ||
```` | ||
|
||
2. Add a new class `UserCaller` | ||
|
||
```` C# | ||
public class UserCaller: DaprCallerBase | ||
{ | ||
protected override string AppId { get; set; } = "<Replace-You-UserService-Dapr-AppID>"; | ||
|
||
public HttpCaller(IServiceProvider serviceProvider) : base(serviceProvider) | ||
{ | ||
} | ||
|
||
public Task<string> HelloAsync(string name) => CallerProvider.GetStringAsync($"/Hello", new { Name = name }); | ||
} | ||
```` | ||
|
||
3. How to use UserCaller | ||
|
||
```` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] UserCaller caller, string name) | ||
=> caller.HelloAsync(name)); | ||
```` |
101 changes: 101 additions & 0 deletions
101
src/Caller/Masa.Utils.Caller.DaprClient/README.zh-CN.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
中 | [EN](README.md) | ||
|
||
## Masa.Utils.Caller.DaprClient | ||
|
||
## 用例: | ||
|
||
```c# | ||
Install-Package Masa.Utils.Caller.DaprClient | ||
``` | ||
|
||
### 基本用法: | ||
|
||
1. 修改`Program.cs` | ||
|
||
``` C# | ||
builder.Services.AddCaller(options => | ||
{ | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "UserCaller";// 当前Caller的别名,仅存在一个Provider时,可以不对Name赋值 | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//被调用方dapr的AppID | ||
}); | ||
}); | ||
``` | ||
|
||
2. 如何使用: | ||
|
||
``` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) | ||
=> userCallerProvider.GetAsync<string>($"/Hello", new { Name = name })); | ||
``` | ||
|
||
> 完整请求的接口地址是:http://localhost:3500/v1.0/invoke/<Replace-You-Dapr-AppID>/method/Hello?Name={name} | ||
3. 当存在多个DaprClient时,则修改`Program.cs`为 | ||
|
||
``` C# | ||
builder.Services.AddCaller(options => | ||
{ | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "UserCaller"; | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//被调用方User服务Dapr的AppID | ||
}); | ||
options.UseDapr(clientBuilder => | ||
{ | ||
clientBuilder.Name = "OrderCaller"; | ||
clientBuilder.AppId = "<Replace-You-Dapr-AppID>" ;//被调用方Order服务Dapr的AppID | ||
}); | ||
}); | ||
``` | ||
|
||
4. 如何使用UserCaller或OrderCaller | ||
|
||
``` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] ICallerProvider userCallerProvider, string name) | ||
=> userCallerProvider.GetAsync<string>($"/Hello", new { Name = name })); | ||
|
||
|
||
app.MapGet("/Test/Order/Hello", ([FromServices] ICallerFactory callerFactory, string name) => | ||
{ | ||
var callerProvider = callerFactory.CreateClient("OrderCaller"); | ||
return callerProvider.GetAsync<string>($"/Hello", new { Name = name }); | ||
}); | ||
``` | ||
|
||
> 当多个Caller被添加时,如何获取指定的Caller? | ||
>> 通过`CallerFactory`的`CreateClient`方法得到指定别名的CallerProvider | ||
> | ||
> 为什么`userCallerProvider`没有通过`CallerFactory`的`CreateClient`方法得到对应的Caller? | ||
>> 如果未指定默认的ICallerProvider,则在`AddCaller`方法中第一个被添加的就是默认的CallerProvider | ||
|
||
#### 推荐用法 | ||
|
||
1. 修改`Program.cs` | ||
|
||
``` C# | ||
builder.Services.AddCaller(); | ||
``` | ||
|
||
2. 新增加类`UserCaller` | ||
|
||
``` C# | ||
public class UserCaller: DaprCallerBase | ||
{ | ||
protected override string AppId { get; set; } = "<Replace-You-UserService-Dapr-AppID>"; | ||
|
||
public HttpCaller(IServiceProvider serviceProvider) : base(serviceProvider) | ||
{ | ||
} | ||
|
||
public Task<string> HelloAsync(string name) => CallerProvider.GetStringAsync($"/Hello", new { Name = name }); | ||
} | ||
``` | ||
|
||
3. 如何使用UserCaller | ||
|
||
``` C# | ||
app.MapGet("/Test/User/Hello", ([FromServices] UserCaller caller, string name) | ||
=> caller.HelloAsync(name)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.