Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>

<Version>0.4.1</Version>
<Version>0.4.2</Version>
</PropertyGroup>
</Project>
1 change: 1 addition & 0 deletions ModEndpoints.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "__SolutionItems", "__Soluti
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
Directory.Build.props = Directory.Build.props
LICENSE.txt = LICENSE.txt
README.md = README.md
EndProjectSection
EndProject
Expand Down
12 changes: 9 additions & 3 deletions samples/Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);

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

if (listResult.IsOk)
{
Expand All @@ -57,7 +60,10 @@ static async Task CallRemoteServicesAsync(IServiceProvider hostProvider)
if (id is not null)
{
//send request over channel to remote ServiceResultEndpoint
var getResult = await channel.SendAsync<GetStoreByIdRequest, GetStoreByIdResponse>(new GetStoreByIdRequest(Id: id.Value), default);
var getResult = await channel.SendAsync<GetStoreByIdRequest, GetStoreByIdResponse>(
new GetStoreByIdRequest(Id: id.Value),
default,
endpointUriPrefix: "v1/storesWithServiceEndpoint/");
if (getResult.IsOk)
{
Console.WriteLine(getResult.Value);
Expand Down
2 changes: 2 additions & 0 deletions src/ModEndpoints.RemoteServices/IServiceChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IServiceChannel
Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
TRequest req,
CancellationToken ct,
string? endpointUriPrefix = null,
MediaTypeHeaderValue? mediaType = null,
JsonSerializerOptions? jsonSerializerOptions = null,
Action<HttpRequestHeaders>? configureRequestHeaders = null,
Expand All @@ -18,6 +19,7 @@ Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
Task<Result> SendAsync<TRequest>(
TRequest req,
CancellationToken ct,
string? endpointUriPrefix = null,
MediaTypeHeaderValue? mediaType = null,
JsonSerializerOptions? jsonSerializerOptions = null,
Action<HttpRequestHeaders>? configureRequestHeaders = null,
Expand Down
22 changes: 20 additions & 2 deletions src/ModEndpoints.RemoteServices/ServiceChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class ServiceChannel(
public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
TRequest req,
CancellationToken ct,
string? endpointUriPrefix = null,
MediaTypeHeaderValue? mediaType = null,
JsonSerializerOptions? jsonSerializerOptions = null,
Action<HttpRequestHeaders>? configureRequestHeaders = null,
Expand All @@ -39,7 +40,9 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
{
return Result<TResponse>.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
}
using (HttpRequestMessage httpReq = new(HttpMethod.Post, requestUriResult.Value))
using (HttpRequestMessage httpReq = new(
HttpMethod.Post,
ServiceChannel.Combine(endpointUriPrefix, requestUriResult.Value)))
{
httpReq.Content = JsonContent.Create(req, mediaType, jsonSerializerOptions);
configureRequestHeaders?.Invoke(httpReq.Headers);
Expand All @@ -60,6 +63,7 @@ public async Task<Result<TResponse>> SendAsync<TRequest, TResponse>(
public async Task<Result> SendAsync<TRequest>(
TRequest req,
CancellationToken ct,
string? endpointUriPrefix = null,
MediaTypeHeaderValue? mediaType = null,
JsonSerializerOptions? jsonSerializerOptions = null,
Action<HttpRequestHeaders>? configureRequestHeaders = null,
Expand All @@ -81,7 +85,9 @@ public async Task<Result> SendAsync<TRequest>(
{
return Result.CriticalError(string.Format(NoChannelRegistrationFound, typeof(TRequest)));
}
using (HttpRequestMessage httpReq = new(HttpMethod.Post, requestUriResult.Value))
using (HttpRequestMessage httpReq = new(
HttpMethod.Post,
ServiceChannel.Combine(endpointUriPrefix, requestUriResult.Value)))
{
httpReq.Content = JsonContent.Create(req, mediaType, jsonSerializerOptions);
configureRequestHeaders?.Invoke(httpReq.Headers);
Expand All @@ -98,4 +104,16 @@ public async Task<Result> SendAsync<TRequest>(
return ex;
}
}

private static string Combine(string? endpointUriPrefix, string endpointUri)
{
if (string.IsNullOrWhiteSpace(endpointUriPrefix))
{
return endpointUri;
}
return string.Format(
"{0}/{1}",
endpointUriPrefix.TrimEnd('/'),
endpointUri.TrimStart('/'));
}
}