1414#### Client端配置使用
1515首先新建任意形式的.net core宿主,为了简单我使用的是Console程序,引入DotNetCoreRpc.Client包和DependencyInjection相关包
1616```
17- <PackageReference Include="DotNetCoreRpc.Client" Version="1.1.1 " />
17+ <PackageReference Include="DotNetCoreRpc.Client" Version="1.1.2 " />
1818```
1919引入自己的服务接口包我这里是Test.IService,只需要引入interface层即可,写入如下测试代码,具体代码可参阅demo,由于DotNetCoreRpc通信是基于HttpClientFactory的,所以需要注册HttpClientFactory
2020``` cs
@@ -64,15 +64,38 @@ class Program
6464 Console .WriteLine ($" 删除Person,id=1完成" );
6565 persons = await personService .GetPersons ();
6666 Console .WriteLine ($" 最后获取Persons,persons=[{persons .ToJson ()}]" );
67- Console .ReadLine ();
67+
68+ IProductService productService = rpcClient .CreateClient <IProductService >(TestServerName );
69+ ProductDto product = new ProductDto
70+ {
71+ Id = 1000 ,
72+ Name = " 抗原" ,
73+ Price = 158 . 22 M
74+ };
75+ int productAddResult = await productService .Add (product );
76+ Console .WriteLine ($" 添加Product1:{productAddResult == 1 }" );
77+ product = productService .Get (1000 );
78+ Console .WriteLine ($" 获取添加Product1,id=1000,person=[{product .ToJson ()}]" );
79+ product = new ProductDto
80+ {
81+ Id = 2000 ,
82+ Name = " N95口罩" ,
83+ Price = 35 . 5 M
84+ };
85+ productAddResult = await productService .Add (product );
86+ Console .WriteLine ($" 添加Product2:{productAddResult == 1 }" );
87+ product = productService .Get (2000 );
88+ Console .WriteLine ($" 获取添加Product2,id=2000,person=[{product .ToJson ()}]" );
89+ var products = await productService .GetProducts ();
90+ Console .WriteLine ($" products=[{products .ToJson ()}]" );
6891 }
6992}
7093```
7194#### Server端配置使用
7295
7396新建一个最简单的Asp.net Core项目,我这里的Demo是新建的Asp.net Core的空项目,引入DotNetCoreRpc.Server包
7497```
75- <PackageReference Include="DotNetCoreRpc.Server" Version="1.1.1 " />
98+ <PackageReference Include="DotNetCoreRpc.Server" Version="1.1.2 " />
7699```
77100然后添加注入和相关中间件
78101``` cs
@@ -123,6 +146,8 @@ var builder = WebApplication.CreateBuilder(args);
123146
124147builder .Services .AddSingleton <IPersonDal , PersonDal >()
125148 .AddSingleton <IPersonService , PersonService >()
149+ .AddSingleton <IProductDal , ProductDal >()
150+ .AddSingleton <IProductService , ProductService >()
126151 .AddSingleton (new RedisConfig { Address = " 127.0.0.1:6379" , db = 10 })
127152 .AddSingleton (new ElasticSearchConfig { Address = " 127.0.0.1:9200" })
128153 .AddDotNetCoreRpcServer (options => {
@@ -133,8 +158,6 @@ builder.Services.AddSingleton<IPersonDal, PersonDal>()
133158 options .AddFilter <CacheFilter >();
134159 });
135160
136- var app = builder .Build ();
137-
138161app .UseDotNetCoreRpc ();
139162app .MapGet (" /" , () => " Hello World!" );
140163
@@ -146,19 +169,21 @@ public class CacheFilter : RpcFilterAttribute
146169{
147170 private readonly ElasticSearchConfig _elasticSearchConfig ;
148171
149- // 支持属性注入public private都可以
150172 [FromServices ]
151173 private RedisConfig RedisConfig { get ; set ; }
152174
175+ [FromServices ]
176+ private ILogger <CacheFilter > Logger { get ; set ; }
177+
153178 public CacheFilter (ElasticSearchConfig elasticSearchConfig )
154179 {
155180 _elasticSearchConfig = elasticSearchConfig ;
156181 }
157182 public override async Task InvokeAsync (RpcContext context , RpcRequestDelegate next )
158183 {
159- Debug . WriteLine ($" CacheFilter begin,Parameters={context .Parameters }" );
184+ Logger . LogInformation ($" CacheFilter begin,Parameters={context .Parameters }" );
160185 await next (context );
161- Debug . WriteLine ($" CacheFilter end,ReturnValue={context .ReturnValue .ToJson ()}" );
186+ Logger . LogInformation ($" CacheFilter end,ReturnValue={context .ReturnValue .ToJson ()}" );
162187 }
163- }
188+ }
164189```
0 commit comments