nenonaninu / typedsignalr.client Goto Github PK
View Code? Open in Web Editor NEWC# Source Generator to Create Strongly Typed SignalR Clients.
License: MIT License
C# Source Generator to Create Strongly Typed SignalR Clients.
License: MIT License
From a Blazor Razor page, when registering the client interface:
var subscription = hubConnection.Register<IDatafeedClient>(this);
This exception is thrown:
System.InvalidOperationException: Failed to register a receiver. TypedSignalR.Client did not generate source code to register a receiver, which type is Trading.Abstractions.Datafeed.IDatafeedClient.
at TypedSignalR.Client.HubConnectionExtensions.Register[TReceiver](HubConnection connection, TReceiver receiver) in \TypedSignalR.Client\TypedSignalR.Client.SourceGenerator\TypedSignalR.Client.HubConnectionExtensions.Generated.cs:line 38
at Trading.IntlWeb.Pages.SignalRClientTest.OnInitializedAsync() in \Trading.IntlWeb\Pages\SignalRClientTest.razor:line 30
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle, ComponentState owningComponentState)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.g__ProcessAsynchronousWork|48_0()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.WaitForQuiescence()
at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32 componentId, ParameterView initialParameters)
at Microsoft.AspNetCore.Components.Rendering.HtmlRenderer.RenderComponentAsync(Type componentType, ParameterView initialParameters)
at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c__11`1.<b__11_0>d.MoveNext()
Why would this occur?
It seems that internal
is more appropriate than public
for the accessibility of the generated codes.
Want to use this nice library, but do receive the following error TypedSignalR.Client did not generate source code to create a hub proxy, which type is MyNamespace.ITestHub
.
I tried to follow the instructions, my simple usage:
var url = new Uri(_settings.BackendUri, hubRoute);
var hubConnection = new HubConnectionBuilder()
.WithUrl(url, options =>
{
options.AccessTokenProvider = async () =>
{
var tokenProvider = serviceProvider.GetRequiredService<IAccessTokenProvider>();
var tokenResult = await tokenProvider.RequestAccessToken();
if (tokenResult.TryGetToken(out var token))
{
return token.Value;
}
throw new InvalidOperationException("Access token not available");
};
})
.WithAutomaticReconnect()
.Build();
await hubConnection.StartAsync();
var hub = hubConnection.Connection.CreateHubProxy<ITestHub>(); // <-- Exception here
var subscription = hubConnection.Connection.Register<ITestHubClient>(this);
The interface is totally basic:
public interface ITestHub
{
public Task SendMessage(TestMessage message);
}
Would appreciate some pointers on what I'm doing wrong here.
Thanks for fixing nullables as return value.
But I also have nullables as parameters, and now with 3.5.0 I get a compile error instead:
CS8639 The typeof operator cannot be used on a nullable reference type
So the warnings before were better than the errors now :(
As the documentation says
In ASP.NET Core SignalR, CancellationToken is passed for each invoke.
On the other hand, in TypedSignalR.Client, CancellationToken is passed only once when creating a hub proxy. The passed CancelationToken will be used for each invoke internally.
Why is it? In my application I store a long living client and use it for independent call chains.
It would be great if one could supply a CancellationToken for a specific request. It would be great if every method had a CancellationToken? cancellationToken = null parameter. If it is null, the request can use the global cancellation token (or combine the 2)
Is there a specific reason there is no per request cancellation token?
ps: This is such a great library, thank you for maintaining it!
I think adding ClientFactory functionality would be really helpful. Similar to how IHttpClientFactory can be used. The client could then be injected into the Program.cs and accessed via DI throughout the app. I have been trying to find how to manually inject the HubConnection on the client side. I would be happy to collaborate on something like this. I believe that SignalR is held back by it's client api.
Similar to this:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-8.0
And how Refit does for HttpClients.
SignalR can use streaming since ASP.NET Core 5. Currently, however, TypedSignalR.Client does not support streaming. I would like to support streaming.
Great job there! Thanks for your effort.
One remark though: I have spent quite a lot of time to figure out why the generated code is full with strange errors, while I have followed the instructions you gave. It turned out that the generator could not handle my method parameter with the name @event
. The generated code stripped the monkey, which resulted in quite a mess.
.NET Blog described the following statement.
Previously, when using SignalR, the server could invoke a method on a client but didn’t have the ability to wait for a response. This scenario is now supported with .NET 7 Preview 4.
I would like to support this new feature when it enters the official release of .NET 7.
We are trying to use this in a Unity project. We are importing the DLL like this:
https://docs.unity3d.com/Manual/roslyn-analyzers.html
However, Unity complains that it cannot find the namespace and types contained in the TypedSignalR.Client.dll.
Do you have any suggestions on how to use it with Unity?
We also tried to manually add the DLL to the C# compiler (csc.rsp) but also no luck there.
First of all, thanks for your awesome work.
The following usecase does not work:
interface IHub : IAnyInterface {}
interface IAnyInterface
{
Task<Guid> HubMethod2(string message, int value);
}
var hub = hubConnection.CreateHubProxy<IHub>();
build output:
Error CS0535 'Extensions.HubInvokerForXyServicesIHub' does not implement interface member 'IAnyInterface.HubMethod2(string, int)' <path>\TypedSignalR.Client\TypedSignalR.Client.SourceGenerator.ExtensionsSourceGenerator\TypedSignalR.Client.Extensions.Internal.Generated.cs
When using a nullable argument on a client method, the generated types are missing the nullable specifier.
public interface IChatHub
{
Task SendMessage(string user, string? message);
}
public interface IChatHubClient
{
Task ReceiveMessage(string user, string? message);
}
public class ChatHub : Hub<IChatHubClient>, IChatHub
{
public async Task SendMessage(string user, string? message)
{
await Clients.All.ReceiveMessage(user, message);
}
}
public class Program
{
public async Task Start()
{
HubConnection connection = new HubConnectionBuilder()
.WithUrl("/hello/world")
.WithAutomaticReconnect()
.Build();
IChatHubClient hubProxy = connection.CreateHubProxy<IChatHubClient>();
await connection.StartAsync();
}
}
This generates the following ReceiveMessage
type
I would expect the message
argument to be nullable here.
Are nullable arguments not supported by TypedSignalR? Or is this a bug?
It spews "error TSRC001: TInterface is not an interface. The type argument must be an interface."
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.