SignalR Source Generator always throw exception

80 Views Asked by At

Here are the classes I am providing

[AttributeUsage(AttributeTargets.Method)]
internal class HubClientProxyAttribute : Attribute { }

[AttributeUsage(AttributeTargets.Method)]
internal class HubServerProxyAttribute : Attribute { }

public static partial class HubConnectionExtensions
{
    [HubClientProxy]
    public static partial IDisposable Subscribe<T>(this HubConnection connection, T provider);

    [HubServerProxy]
    public static partial T HubInvoker<T>(this HubConnection connection);
}

It is generating these files

HubClientProxy.g.cs

    public static partial class HubConnectionExtensions
    {
        public static partial System.IDisposable Subscribe<T>(this HubConnection connection, T provider)
        {
            if (provider is null)
            {
                throw new System.ArgumentNullException("provider");
            }

            throw new System.ArgumentException(nameof(T)); // this line seems wrong, we should have code here.
        }

        private sealed class CallbackProviderRegistration : System.IDisposable
        {
            private System.IDisposable[]? registrations;
            public CallbackProviderRegistration(params System.IDisposable[] registrations)
            {
                this.registrations = registrations;
            }

            public void Dispose()
            {
                if (this.registrations is null)
                {
                    return;
                }

                System.Collections.Generic.List<System.Exception>? exceptions = null;
                foreach(var registration in this.registrations)
                {
                    try
                    {
                        registration.Dispose();
                    }
                    catch (System.Exception exc)
                    {
                        if (exceptions is null)
                        {
                            exceptions = new ();
                        }

                        exceptions.Add(exc);
                    }
                }
                this.registrations = null;
                if (exceptions is not null)
                {
                    throw new System.AggregateException(exceptions);
                }
            }
        }
    }

HubServerProxy.g.cs

    public static partial class HubConnectionExtensions
    {
        public static partial T HubInvoker<T>(this HubConnection connection)
        {

            throw new System.ArgumentException(nameof(T)); // this line seems wrong, we should have code here.
        }
    }

I am using it like this, where AppService inherits the same interface as the hub, and it throws the exception

    public AppService(IHttpClientFactory clientFactory, ILogger<AppService> logger)
    {
        _logger = logger;
        _client = clientFactory.CreateClient("clientname");
        _hub = new HubConnectionBuilder().WithUrl($"{_client.BaseAddress!.GetLeftPart(UriPartial.Authority)}/hubpath").Build();
        _subscriber = _hub.Subscribe(this); // exception

        _dataTaskSource = InitializeDataTaskSource();
    }

I tried giving the extension methods different names, maybe there is a specific name I have to use? The source generator seems to accept anything for the method name.

1

There are 1 best solutions below

2
mattygs On
 public AppService(IHttpClientFactory clientFactory, ILogger<AppService> logger)
    {
        _logger = logger;
        _client = clientFactory.CreateClient("clientname");
        _hub = new HubConnectionBuilder().WithUrl($"{_client.BaseAddress!.GetLeftPart(UriPartial.Authority)}/hubpath").Build();
        _subscriber = _hub.Subscribe<ICompleteNotifier>(this); // exception

        _dataTaskSource = InitializeDataTaskSource();
    }

I had to explicitly put the interface in the generic extension method call. It only works when it is an interface.