-
Notifications
You must be signed in to change notification settings - Fork 108
Add client invocation for RestHublifetimeManager #2206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 8 commits
ab1dbcc
b0f2622
4ae742a
13d6eda
6e91cd5
ccd74e2
894fa29
93b3e76
8094a28
cc6e1ac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace Microsoft.Azure.SignalR.Management.ClientInvocation; | ||
| #nullable enable | ||
| sealed class InvocationResponse<T> | ||
| { | ||
| [JsonPropertyName("result")] | ||
| public T? Result { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,19 @@ | |
| using System.Linq; | ||
| using System.Net; | ||
| using System.Net.Http; | ||
| #if NET7_0_OR_GREATER | ||
| using System.IO; | ||
| #endif | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Azure; | ||
|
|
||
| using Azure.Core.Serialization; | ||
| using Microsoft.AspNetCore.SignalR; | ||
| #if NET7_0_OR_GREATER | ||
| using Microsoft.AspNetCore.SignalR.Protocol; | ||
| using Microsoft.Azure.SignalR.Management.ClientInvocation; | ||
| #endif | ||
| using Microsoft.Azure.SignalR.Protocol; | ||
| using Microsoft.Extensions.Primitives; | ||
|
|
||
|
|
@@ -29,13 +36,15 @@ internal class RestHubLifetimeManager<THub> : HubLifetimeManager<THub>, IService | |
| private readonly RestApiProvider _restApiProvider; | ||
| private readonly string _hubName; | ||
| private readonly string _appName; | ||
| private readonly ObjectSerializer _objectSerializer; | ||
|
|
||
| public RestHubLifetimeManager(string hubName, ServiceEndpoint endpoint, string appName, RestClient restClient) | ||
| public RestHubLifetimeManager(string hubName, ServiceEndpoint endpoint, string appName, RestClient restClient, ObjectSerializer objectSerializer) | ||
| { | ||
| _restApiProvider = new RestApiProvider(endpoint); | ||
| _appName = appName; | ||
| _hubName = hubName; | ||
| _restClient = restClient; | ||
| _objectSerializer = objectSerializer; | ||
| } | ||
|
|
||
| public override async Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default) | ||
|
|
@@ -351,6 +360,70 @@ public async Task SendStreamCompletionAsync(string connectionId, string streamId | |
| await _restClient.SendWithRetryAsync(api, HttpMethod.Post, cancellationToken: cancellationToken); | ||
| } | ||
|
|
||
| #if NET7_0_OR_GREATER | ||
| #nullable enable | ||
| public override async Task<T> InvokeConnectionAsync<T>(string connectionId, string methodName, object?[] args, CancellationToken cancellationToken = default) | ||
| { | ||
| // Validate input parameters | ||
| if (string.IsNullOrEmpty(methodName)) | ||
| { | ||
| throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(methodName)); | ||
| } | ||
| if (string.IsNullOrEmpty(connectionId)) | ||
| { | ||
| throw new ArgumentException(NullOrEmptyStringErrorMessage, nameof(connectionId)); | ||
| } | ||
|
|
||
| // Get API endpoint and prepare for the request | ||
| var api = _restApiProvider.SendClientInvocation(_appName, _hubName, connectionId); | ||
| string? responseContent = null; | ||
| var isSuccessStatusCode = false; | ||
| // Send request and capture the response | ||
| await _restClient.SendMessageWithRetryAsync( | ||
| api, | ||
| HttpMethod.Post, | ||
| methodName, | ||
| args, | ||
| async response => | ||
| { | ||
| responseContent = await response.Content.ReadAsStringAsync(cancellationToken); | ||
|
||
| isSuccessStatusCode = response.IsSuccessStatusCode; | ||
| return response.IsSuccessStatusCode || response.StatusCode == HttpStatusCode.BadRequest; | ||
| }, | ||
| cancellationToken: cancellationToken); | ||
|
|
||
| // Ensure we have a response | ||
| if (responseContent is null) | ||
| { | ||
| throw new HubException("Response content is null or empty"); | ||
| } | ||
|
|
||
| if (!isSuccessStatusCode) | ||
| { | ||
| throw new HubException(responseContent); | ||
| } | ||
|
|
||
| using var contentStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(responseContent)); | ||
|
|
||
| var wrapperObj = await _objectSerializer.DeserializeAsync( | ||
| contentStream, | ||
| typeof(InvocationResponse<T>), | ||
| cancellationToken); | ||
|
|
||
| var wrapper = wrapperObj as InvocationResponse<T> | ||
| ?? throw new HubException("Failed to deserialize response"); | ||
|
|
||
| return wrapper.Result ?? throw new HubException("Result not found in response"); | ||
| } | ||
|
|
||
| public override Task SetConnectionResultAsync(string connectionId, CompletionMessage result) | ||
| { | ||
| // This method won't get trigger because in transient we will wait for the returned completion message. | ||
| // this is to honor the interface | ||
| throw new NotImplementedException(); | ||
| } | ||
| #endif | ||
|
|
||
| private static bool FilterExpectedResponse(HttpResponseMessage response, string expectedErrorCode) => | ||
| response.IsSuccessStatusCode | ||
| || (response.StatusCode == HttpStatusCode.NotFound && response.Headers.TryGetValues(Headers.MicrosoftErrorCode, out var errorCodes) && errorCodes.First().Equals(expectedErrorCode, StringComparison.OrdinalIgnoreCase)); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may not work if the object serializer is backed by
Newtonsoft.Json.