Skip to content

Commit 04e7ccf

Browse files
authored
Service endpoint uri (#5)
* - service channel send methods: add support for calling service endpoints with endpoint uri prefixes * - add existing license file to solution items - bump version
1 parent c9fe323 commit 04e7ccf

File tree

5 files changed

+33
-6
lines changed

5 files changed

+33
-6
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<PackageReadmeFile>README.md</PackageReadmeFile>
1818
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1919

20-
<Version>0.4.1</Version>
20+
<Version>0.4.2</Version>
2121
</PropertyGroup>
2222
</Project>

ModEndpoints.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__SolutionItems", "__Soluti
77
ProjectSection(SolutionItems) = preProject
88
.editorconfig = .editorconfig
99
Directory.Build.props = Directory.Build.props
10+
LICENSE.txt = LICENSE.txt
1011
README.md = README.md
1112
EndProjectSection
1213
EndProject

samples/Client/Program.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
1111

12-
var baseAddress = "https://localhost:7012/api/v1/storesWithServiceEndpoint/";
12+
var baseAddress = "https://localhost:7012/api/";
1313
var clientName = "ShowcaseApi.Client";
1414
//builder.Services.AddRemoteServiceWithNewClient<ListStoresRequest>(clientName,
1515
// (sp, client) =>
@@ -48,7 +48,10 @@ static async Task CallRemoteServicesAsync(IServiceProvider hostProvider)
4848
//resolve service channel from DI
4949
var channel = provider.GetRequiredService<IServiceChannel>();
5050
//send request over channel to remote ServiceResultEndpoint
51-
var listResult = await channel.SendAsync<ListStoresRequest, ListStoresResponse>(new ListStoresRequest(), default);
51+
var listResult = await channel.SendAsync<ListStoresRequest, ListStoresResponse>(
52+
new ListStoresRequest(),
53+
default,
54+
endpointUriPrefix: "v1/storesWithServiceEndpoint/");
5255

5356
if (listResult.IsOk)
5457
{
@@ -57,7 +60,10 @@ static async Task CallRemoteServicesAsync(IServiceProvider hostProvider)
5760
if (id is not null)
5861
{
5962
//send request over channel to remote ServiceResultEndpoint
60-
var getResult = await channel.SendAsync<GetStoreByIdRequest, GetStoreByIdResponse>(new GetStoreByIdRequest(Id: id.Value), default);
63+
var getResult = await channel.SendAsync<GetStoreByIdRequest, GetStoreByIdResponse>(
64+
new GetStoreByIdRequest(Id: id.Value),
65+
default,
66+
endpointUriPrefix: "v1/storesWithServiceEndpoint/");
6167
if (getResult.IsOk)
6268
{
6369
Console.WriteLine(getResult.Value);

src/ModEndpoints.RemoteServices/IServiceChannel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IServiceChannel
99
Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
1010
TRequest req,
1111
CancellationToken ct,
12+
string? endpointUriPrefix = null,
1213
MediaTypeHeaderValue? mediaType = null,
1314
JsonSerializerOptions? jsonSerializerOptions = null,
1415
Action<HttpRequestHeaders>? configureRequestHeaders = null,
@@ -18,6 +19,7 @@ Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
1819
Task<Result> SendAsync<TRequest>(
1920
TRequest req,
2021
CancellationToken ct,
22+
string? endpointUriPrefix = null,
2123
MediaTypeHeaderValue? mediaType = null,
2224
JsonSerializerOptions? jsonSerializerOptions = null,
2325
Action<HttpRequestHeaders>? configureRequestHeaders = null,

src/ModEndpoints.RemoteServices/ServiceChannel.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ServiceChannel(
1717
public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
1818
TRequest req,
1919
CancellationToken ct,
20+
string? endpointUriPrefix = null,
2021
MediaTypeHeaderValue? mediaType = null,
2122
JsonSerializerOptions? jsonSerializerOptions = null,
2223
Action<HttpRequestHeaders>? configureRequestHeaders = null,
@@ -39,7 +40,9 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
3940
{
4041
return Result<TResponse>.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
4142
}
42-
using (HttpRequestMessage httpReq = new(HttpMethod.Post, requestUriResult.Value))
43+
using (HttpRequestMessage httpReq = new(
44+
HttpMethod.Post,
45+
ServiceChannel.Combine(endpointUriPrefix, requestUriResult.Value)))
4346
{
4447
httpReq.Content = JsonContent.Create(req, mediaType, jsonSerializerOptions);
4548
configureRequestHeaders?.Invoke(httpReq.Headers);
@@ -60,6 +63,7 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
6063
public async Task<Result> SendAsync<TRequest>(
6164
TRequest req,
6265
CancellationToken ct,
66+
string? endpointUriPrefix = null,
6367
MediaTypeHeaderValue? mediaType = null,
6468
JsonSerializerOptions? jsonSerializerOptions = null,
6569
Action<HttpRequestHeaders>? configureRequestHeaders = null,
@@ -81,7 +85,9 @@ public async Task<Result> SendAsync<TRequest>(
8185
{
8286
return Result.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
8387
}
84-
using (HttpRequestMessage httpReq = new(HttpMethod.Post, requestUriResult.Value))
88+
using (HttpRequestMessage httpReq = new(
89+
HttpMethod.Post,
90+
ServiceChannel.Combine(endpointUriPrefix, requestUriResult.Value)))
8591
{
8692
httpReq.Content = JsonContent.Create(req, mediaType, jsonSerializerOptions);
8793
configureRequestHeaders?.Invoke(httpReq.Headers);
@@ -98,4 +104,16 @@ public async Task<Result> SendAsync<TRequest>(
98104
return ex;
99105
}
100106
}
107+
108+
private static string Combine(string? endpointUriPrefix, string endpointUri)
109+
{
110+
if (string.IsNullOrWhiteSpace(endpointUriPrefix))
111+
{
112+
return endpointUri;
113+
}
114+
return string.Format(
115+
"{0}/{1}",
116+
endpointUriPrefix.TrimEnd('/'),
117+
endpointUri.TrimStart('/'));
118+
}
101119
}

0 commit comments

Comments
 (0)