Skip to content

Commit b8991af

Browse files
author
liguoliang
committed
添加自定义请求path
1 parent 45b52c0 commit b8991af

9 files changed

Lines changed: 27 additions & 18 deletions

File tree

demo/Test.Client/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ static async Task Main(string[] args)
2727
IServiceCollection services = new ServiceCollection();
2828
services.AddLogging().AddDotNetCoreRpcClient()
2929
//单机版Httpclient配置
30-
.AddHttpClient(TestServerName, client => { client.BaseAddress = new Uri("http://localhost:34047/"); });
30+
.AddHttpClient(TestServerName, client => { client.BaseAddress = new Uri("http://localhost:34047/Test.Server6"); });
3131
//基于Nacos注册中心
3232
//.AddNacosV2Naming(configuration)
3333
//.AddScoped<NacosDiscoveryDelegatingHandler>()
34-
//.AddHttpClient(TestServerName, client => {
35-
// client.BaseAddress = new Uri($"http://{TestServerName}");
34+
//.AddHttpClient(TestServerName, client =>
35+
//{
36+
// client.BaseAddress = new Uri($"http://{TestServerName}/Test.Server6");
3637
//}).AddHttpMessageHandler<NacosDiscoveryDelegatingHandler>();
3738

3839
IServiceProvider serviceProvider = services.BuildServiceProvider();

demo/Test.Client/appsettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
}
88
},
99
"nacos": {
10-
"ServerAddresses": [ "http://192.168.219.1:8848" ],
10+
"ServerAddresses": [ "http://localhost:8848" ],
1111
"ServiceName": "apigateway",
1212
"DefaultTimeOut": 15000,
1313
//自定义Namespace的Id
14-
"Namespace": "2ae308e2-7e8a-4602-9d1c-56508a3e263c",
14+
"Namespace": "01761089-727d-49cb-9abe-fbe647f83927",
1515
"GroupName": "DEFAULT_GROUP",
1616
"ClusterName": "DEFAULT",
1717
"ListenInterval": 1000,

demo/Test.Server/Startup.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void ConfigureServices(IServiceCollection services)
4242
options.AddNameSpace("Test.IService");
4343
options.AddFilter<CacheFilter>();
4444
});
45-
//services.AddNacosAspNet(Configuration);
45+
services.AddNacosAspNet(Configuration);
4646
}
4747

4848
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
@@ -53,7 +53,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
5353
app.UseEndpoints(endpoint => {
5454
endpoint.Map("/", async context=>await context.Response.WriteAsync("server start!"));
5555
//通过endpoint的方式引入
56-
endpoint.MapDotNetCoreRpc();
56+
endpoint.MapDotNetCoreRpc("/Test.Server6");
5757
});
5858
}
5959
}

demo/Test.Server/appsettings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
},
99
"AllowedHosts": "*",
1010
"nacos": {
11-
"ServerAddresses": [ "http://192.168.219.1:8848" ],
11+
"ServerAddresses": [ "http://localhost:8848" ],
1212
"ServiceName": "testserver",
1313
"DefaultTimeOut": 15000,
14-
//×Ô¶¨ÒåNamespaceµÄId
15-
"Namespace": "2ae308e2-7e8a-4602-9d1c-56508a3e263c",
14+
//自定义Namespace的Id
15+
"Namespace": "01761089-727d-49cb-9abe-fbe647f83927",
1616
"GroupName": "DEFAULT_GROUP",
1717
"ClusterName": "DEFAULT",
1818
"ListenInterval": 1000,

demo/Test.Server6/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
var app = builder.Build();
2626

27-
app.UseDotNetCoreRpc();
27+
app.UseDotNetCoreRpc("/Test.Server6");
2828
app.MapGet("/", () => "Hello World!");
2929

3030
app.Run();

src/DotNetCoreRpc.Client/RequestHandler.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ private async Task<byte[]> SendRequest(MethodInfo methodInfo, params object[] ar
102102

103103
HttpContent httpContent = new StringContent(requestModel.ToJson());
104104
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
105-
var responseMessage = await _httpClient.PostAsync("/DotNetCoreRpc/ServerRequest", httpContent);
105+
106+
string path = string.IsNullOrWhiteSpace(_httpClient.BaseAddress.PathAndQuery) || _httpClient.BaseAddress.PathAndQuery == "/"
107+
? "/DotNetCoreRpc/ServerRequest" : "";
108+
109+
var responseMessage = await _httpClient.PostAsync(path, httpContent);
106110
byte[] result = await responseMessage.Content.ReadAsByteArrayAsync();
107111
//判断http请求状态
108112
responseMessage.EnsureSuccessStatusCode();

src/DotNetCoreRpc.Server/DncRpcEndpointRouteBuilderExtensions.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ namespace DotNetCoreRpc.Server
1212
/// </summary>
1313
public static class DncRpcEndpointRouteBuilderExtensions
1414
{
15-
public static IEndpointConventionBuilder MapDotNetCoreRpc(this IEndpointRouteBuilder endpoints)
15+
public static IEndpointConventionBuilder MapDotNetCoreRpc(this IEndpointRouteBuilder endpoints, string path = default)
1616
{
1717
if (endpoints == null)
1818
{
1919
throw new ArgumentNullException(nameof(endpoints));
2020
}
21-
RequestDelegate requestDelegate = endpoints.CreateApplicationBuilder().UseDotNetCoreRpc().Build();
22-
return endpoints.MapPost("/DotNetCoreRpc/ServerRequest", requestDelegate);
21+
22+
path = string.IsNullOrWhiteSpace(path) ? "/DotNetCoreRpc/ServerRequest" : path;
23+
RequestDelegate requestDelegate = endpoints.CreateApplicationBuilder().UseDotNetCoreRpc(path).Build();
24+
return endpoints.MapPost(path, requestDelegate);
2325
}
2426
}
2527
}

src/DotNetCoreRpc.Server/DncRpcIApplicationBuilderExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
2+
using System.Linq;
23
using Microsoft.AspNetCore.Builder;
34

45
namespace DotNetCoreRpc.Server
56
{
67
public static class DncRpcIApplicationBuilderExtensions
78
{
8-
public static IApplicationBuilder UseDotNetCoreRpc(this IApplicationBuilder applicationBuilder)
9+
public static IApplicationBuilder UseDotNetCoreRpc(this IApplicationBuilder applicationBuilder, string path = default)
910
{
10-
return applicationBuilder.UseWhen(context => context.Request.Path.Value.Contains("/DotNetCoreRpc/ServerRequest")
11+
path = string.IsNullOrWhiteSpace(path) ? "/DotNetCoreRpc/ServerRequest" : path;
12+
return applicationBuilder.UseWhen(context => context.Request.Path.Value.Contains(path)
1113
&& context.Request.Headers.ContainsKey("req-source")
1214
&& context.Request.Headers["req-source"] == "dncrpc"
1315
&& string.Equals(context.Request.Method, "post", StringComparison.OrdinalIgnoreCase),

src/DotNetCoreRpc.Server/ServerServiceCollectionExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace DotNetCoreRpc.Server
66
{
77
public static class ServiceCollectionExtensions
88
{
9-
public static IServiceCollection AddDotNetCoreRpcServer(this IServiceCollection services,Action<RpcServerOptions> options)
9+
public static IServiceCollection AddDotNetCoreRpcServer(this IServiceCollection services, Action<RpcServerOptions> options)
1010
{
1111
RpcServerOptions rpcServerOptions = new RpcServerOptions(services);
1212
options.Invoke(rpcServerOptions);

0 commit comments

Comments
 (0)