-
Notifications
You must be signed in to change notification settings - Fork 110
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 all 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")] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It may not work if the object serializer is backed by |
||
| public T? Result { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,12 +7,18 @@ | |
| 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 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; | ||
|
|
||
|
|
@@ -351,6 +357,73 @@ 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); | ||
| InvocationResponse<T>? wrapper = null; | ||
| string? errorContent = null; | ||
| bool isSuccess = false; | ||
| // Send request and capture the response | ||
| await _restClient.SendMessageWithRetryAsync( | ||
| api, | ||
| HttpMethod.Post, | ||
| methodName, | ||
| args, | ||
| async response => | ||
| { | ||
| isSuccess = response.IsSuccessStatusCode; | ||
|
|
||
| if (isSuccess) | ||
| { | ||
| await using var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken); | ||
|
|
||
| var deserialized = await _restClient.ObjectSerializer.DeserializeAsync( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
| contentStream, | ||
| typeof(InvocationResponse<T>), | ||
| cancellationToken); | ||
|
|
||
| wrapper = deserialized as InvocationResponse<T> | ||
| ?? throw new HubException("Failed to deserialize response"); | ||
| } | ||
| else | ||
| { | ||
| errorContent = await response.Content.ReadAsStringAsync(cancellationToken); | ||
| } | ||
|
|
||
| return isSuccess || response.StatusCode == HttpStatusCode.BadRequest; | ||
| }, | ||
| cancellationToken); | ||
|
|
||
| // Ensure we have a response | ||
| if (!isSuccess) | ||
| { | ||
| throw new HubException(errorContent ?? "Unknown error in response"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's throw an |
||
| } | ||
|
|
||
| return wrapper != null && wrapper.Result != null ? wrapper.Result : throw new HubException("Result not found in response"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a null invocation result invalid? |
||
| } | ||
|
|
||
| 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.
This doesn't work if the
PayloadContentBuilderis not aJsonPayloadContentBuider. I'd suggest injectingIOptionsMonior<ServiceManagerOptions>intoRestClient, using theServiceManagerOptions.ObjectSerializeror providing a default one whenServiceManagerOptions.ObjectSerializer=null