Skip to content

Commit 35a9a02

Browse files
authored
Merge pull request #25 from softlgl/dev
合并dev代码优化
2 parents b11a566 + 1ac951e commit 35a9a02

13 files changed

Lines changed: 339 additions & 56 deletions

File tree

demo/Test.Client/Program.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Microsoft.Extensions.Configuration;
1111
using Nacos.V2.DependencyInjection;
12+
using System.Collections.Generic;
1213

1314
namespace Test.Client
1415
{
@@ -127,6 +128,49 @@ static async Task Main(string[] args)
127128
persons = await personService.GetPersons();
128129
Console.WriteLine($"最后获取Persons,persons=[{persons.ToJson()}]");
129130

131+
Stopwatch stopwatch = Stopwatch.StartNew();
132+
for (int i = 0; i < 100; i++)
133+
{
134+
await personService.GetPersons();
135+
}
136+
stopwatch.Stop();
137+
Console.WriteLine($"await:{stopwatch.Elapsed.TotalMilliseconds}");
138+
139+
stopwatch.Reset();
140+
stopwatch.Start();
141+
List<Task> tasks = new List<Task>();
142+
for (int i = 0; i < 100; i++)
143+
{
144+
tasks.Add(personService.GetPersons());
145+
}
146+
await Task.WhenAll(tasks);
147+
stopwatch.Stop();
148+
Console.WriteLine($"tasks await:{stopwatch.Elapsed.TotalMilliseconds}");
149+
150+
IProductService productService = rpcClient.CreateClient<IProductService>(TestServerName);
151+
ProductDto product = new ProductDto
152+
{
153+
Id = 1000,
154+
Name="抗原",
155+
Price = 158.22M
156+
};
157+
int productAddResult = await productService.Add(product);
158+
Console.WriteLine($"添加Product1:{productAddResult==1}");
159+
product = productService.Get(1000);
160+
Console.WriteLine($"获取添加Product1,id=1000,person=[{product.ToJson()}]");
161+
product = new ProductDto
162+
{
163+
Id = 2000,
164+
Name = "N95口罩",
165+
Price = 35.5M
166+
};
167+
productAddResult = await productService.Add(product);
168+
Console.WriteLine($"添加Product2:{productAddResult == 1}");
169+
product = productService.Get(2000);
170+
Console.WriteLine($"获取添加Product2,id=2000,person=[{product.ToJson()}]");
171+
var products = await productService.GetProducts();
172+
Console.WriteLine($"products=[{products.ToJson()}]");
173+
130174
//BenchmarkRunner.Run<RpcClientBenchTest>();
131175

132176
Console.ReadLine();

demo/Test.Client/Test.Client.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@
2323
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
2424
</None>
2525
</ItemGroup>
26+
<ItemGroup>
27+
<Folder Include="Properties\" />
28+
</ItemGroup>
2629
</Project>

demo/Test.DAL/ProductDal.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Test.IDAL;
5+
using Test.Model;
6+
7+
namespace Test.DAL
8+
{
9+
public class ProductDal : IProductDal
10+
{
11+
private List<ProductDto> products = new List<ProductDto>();
12+
13+
public bool Add(ProductDto product)
14+
{
15+
if (products.Any(i => i.Id == product.Id))
16+
{
17+
return false;
18+
}
19+
products.Add(product);
20+
return true;
21+
}
22+
23+
public void Delete(int id)
24+
{
25+
var person = products.FirstOrDefault(i => i.Id == id);
26+
if (person != null)
27+
{
28+
products.Remove(person);
29+
}
30+
}
31+
32+
public ProductDto Get(int id)
33+
{
34+
return products.FirstOrDefault(i => i.Id == id);
35+
}
36+
37+
public List<ProductDto> GetProducts()
38+
{
39+
return products;
40+
}
41+
}
42+
}

demo/Test.IDAL/IProductDal.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Collections.Generic;
2+
using Test.Model;
3+
4+
namespace Test.IDAL
5+
{
6+
public interface IProductDal
7+
{
8+
ProductDto Get(int id);
9+
List<ProductDto> GetProducts();
10+
bool Add(ProductDto product);
11+
void Delete(int id);
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Test.Model;
5+
6+
namespace Test.IService
7+
{
8+
public interface IProductService
9+
{
10+
ProductDto Get(int id);
11+
Task<List<ProductDto>> GetProducts();
12+
ValueTask<int> Add(ProductDto person);
13+
void Delete(int id);
14+
Task Edit(int id);
15+
}
16+
}

demo/Test.Model/ProductDto.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
namespace Test.Model
3+
{
4+
public class ProductDto
5+
{
6+
public long Id { get; set; }
7+
public string Name { get; set; }
8+
public decimal Price { get; set; }
9+
}
10+
}

demo/Test.Server/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public void ConfigureServices(IServiceCollection services)
3131
{
3232
services.AddSingleton<IPersonDal, PersonDal>()
3333
.AddSingleton<IPersonService,PersonService>()
34+
.AddSingleton<IProductDal, ProductDal>()
35+
.AddSingleton<IProductService, ProductService>()
3436
.AddSingleton(new RedisConfig { Address="127.0.0.1:6379",db=10 })
3537
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
3638
.AddDotNetCoreRpcServer(options => {

demo/Test.Server6/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
builder.Services.AddSingleton<IPersonDal, PersonDal>()
1212
.AddSingleton<IPersonService, PersonService>()
13+
.AddSingleton<IProductDal, ProductDal>()
14+
.AddSingleton<IProductService, ProductService>()
1315
.AddSingleton(new RedisConfig { Address = "127.0.0.1:6379", db = 10 })
1416
.AddSingleton(new ElasticSearchConfig { Address = "127.0.0.1:9200" })
1517
.AddDotNetCoreRpcServer(options => {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Test.IDAL;
5+
using Test.IService;
6+
using Test.Model;
7+
using Test.Service.Filters;
8+
9+
namespace Test.Service
10+
{
11+
public class ProductService:IProductService
12+
{
13+
private readonly IProductDal _productDal;
14+
public ProductService(IProductDal productDal)
15+
{
16+
_productDal = productDal;
17+
}
18+
19+
[LoggerFilter]
20+
public ValueTask<int> Add(ProductDto person)
21+
{
22+
bool result =_productDal.Add(person);
23+
return new ValueTask<int>(result?1:0);
24+
}
25+
26+
public void Delete(int id)
27+
{
28+
_productDal.Delete(id);
29+
}
30+
31+
public Task Edit(int id)
32+
{
33+
return Task.CompletedTask;
34+
}
35+
36+
[LoggerFilter]
37+
public ProductDto Get(int id)
38+
{
39+
return _productDal.Get(id);
40+
}
41+
42+
public async Task<List<ProductDto>> GetProducts()
43+
{
44+
return await Task.FromResult(_productDal.GetProducts());
45+
}
46+
}
47+
}

src/DotNetCoreRpc.Client/HttpRequestInterceptor.cs

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,46 @@ namespace DotNetCoreRpc.Client
1212
{
1313
public class HttpRequestInterceptor : IInterceptor
1414
{
15-
private readonly HttpClient _httpClient;
15+
private readonly RequestHandler _requestHandler;
1616
public HttpRequestInterceptor(HttpClient httpClient)
1717
{
18-
_httpClient = httpClient;
18+
_requestHandler = new RequestHandler(httpClient);
1919
}
2020

2121
public void Intercept(IInvocation invocation)
2222
{
23-
HandleRequest(invocation).GetAwaiter().GetResult();
24-
}
23+
var methodReturnType = invocation.Method.ReturnType.GetTypeInfo();
2524

26-
private async Task HandleRequest(IInvocation invocation)
27-
{
28-
var methodInfo = invocation.Method;
29-
var requestModel = new RequestModel
30-
{
31-
TypeFullName = methodInfo.DeclaringType.FullName,
32-
MethodName = methodInfo.Name,
33-
Paramters = invocation.Arguments
34-
};
35-
HttpContent httpContent = new StringContent(requestModel.ToJson());
36-
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
37-
var responseMessage = await _httpClient.PostAsync("/DotNetCoreRpc/ServerRequest", httpContent);
38-
if (responseMessage.StatusCode == HttpStatusCode.OK)
25+
if (!methodReturnType.IsAsync())
3926
{
40-
var methodReturnType = methodInfo.ReturnType.GetTypeInfo();
41-
byte[] result = await responseMessage.Content.ReadAsByteArrayAsync();
42-
if (result != null && result.Length != 0)
27+
var result = _requestHandler.SyncResultHandle(invocation.Method, invocation.Arguments);
28+
if (result == null)
4329
{
44-
ResponseModel responseModel = result.FromJson<ResponseModel>();
45-
if (responseModel.Code != (int)HttpStatusCode.OK)
46-
{
47-
throw new Exception($"请求出错,返回内容:{Encoding.UTF8.GetString(result)}");
48-
}
49-
if (responseModel.Data != null)
50-
{
51-
if (!methodReturnType.IsAsync())
52-
{
53-
invocation.ReturnValue = responseModel.Data.ToJson().FromJson(methodInfo.ReturnType);
54-
return;
55-
}
30+
return;
31+
}
5632

57-
var returnValue = responseModel.Data.ToJson().FromJson(methodReturnType.GetGenericArguments()[0]);
58-
var resultType = invocation.Method.ReturnType.GetGenericArguments()[0];
59-
if (methodReturnType.IsTaskWithResult())
60-
{
61-
invocation.ReturnValue = TaskUtils.TaskResultFunc(resultType).Invoke(returnValue);
62-
return;
63-
}
33+
invocation.ReturnValue = result;
34+
return;
35+
}
6436

65-
if (methodReturnType.IsValueTaskWithResult())
66-
{
67-
invocation.ReturnValue = TaskUtils.ValueTaskResultFunc(resultType).Invoke(returnValue);
68-
return;
69-
}
70-
}
37+
if (methodReturnType.IsTask() || methodReturnType.IsValueTask())
38+
{
39+
invocation.ReturnValue = Task.CompletedTask;
40+
return;
41+
}
7142

72-
if (methodReturnType.IsTask() || methodReturnType.IsValueTask())
73-
{
74-
invocation.ReturnValue = Task.CompletedTask;
75-
return;
76-
}
77-
}
43+
if (methodReturnType.IsTaskWithResult())
44+
{
45+
invocation.ReturnValue = _requestHandler.GetTaskResultHandleFunc(methodReturnType).Invoke(invocation.Method, invocation.Arguments);
7846
return;
7947
}
80-
throw new Exception($"请求异常,StatusCode:{responseMessage.StatusCode}");
81-
}
48+
49+
if (methodReturnType.IsValueTaskWithResult())
50+
{
51+
invocation.ReturnValue = _requestHandler.GetValueResultHandleFunc(methodReturnType).Invoke(invocation.Method, invocation.Arguments);
52+
return;
53+
}
54+
}
55+
8256
}
8357
}

0 commit comments

Comments
 (0)