Skip to content

Add attributes to SignalR source generator #38025

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

Closed
wants to merge 6 commits into from
Closed
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
23 changes: 22 additions & 1 deletion AspNetCore.sln
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Logging.W3C.Sample", "src\M
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Client.SourceGenerator", "Client.SourceGenerator", "{4FC50620-4C8B-495F-859C-BFACAD158033}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNetCore.SignalR.Client.SourceGenerator", "src\SignalR\clients\csharp\Client.SourceGenerator\src\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj", "{E090F82D-8345-477E-92E8-F724F08ADC56}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.Client.SourceGenerator", "src\SignalR\clients\csharp\Client.SourceGenerator\src\gen\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj", "{E090F82D-8345-477E-92E8-F724F08ADC56}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Razor.Internal.SourceGenerator.Transport", "src\Razor\Microsoft.AspNetCore.Razor.Internal.SourceGenerator.Transport\Microsoft.AspNetCore.Razor.Internal.SourceGenerator.Transport.csproj", "{247E7B6F-FBA2-41A9-BA03-C7C4DF28091C}"
EndProject
Expand Down Expand Up @@ -1714,6 +1714,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DelegationSite", "src\Serve
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Server.HttpSys.NonHelixTests", "src\Servers\HttpSys\test\NonHelixTests\Microsoft.AspNetCore.Server.HttpSys.NonHelixTests.csproj", "{8EB0B983-8851-4565-B92F-366F1B126E61}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.SignalR.Client.SourceGenerator.HubProxyAttributes", "src\SignalR\clients\csharp\Client.SourceGenerator\src\HubProxyAttributes\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.HubProxyAttributes.csproj", "{FF358174-C540-4FDD-BA8A-8B1589FDEE94}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -9845,13 +9847,15 @@ Global
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|arm64.ActiveCfg = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|arm64.Build.0 = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|x64.ActiveCfg = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|x64.Build.0 = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|x86.ActiveCfg = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Debug|x86.Build.0 = Debug|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|Any CPU.Build.0 = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|arm64.ActiveCfg = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|arm64.Build.0 = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|x64.ActiveCfg = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|x64.Build.0 = Release|Any CPU
{E090F82D-8345-477E-92E8-F724F08ADC56}.Release|x86.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -10332,6 +10336,22 @@ Global
{8EB0B983-8851-4565-B92F-366F1B126E61}.Release|x64.Build.0 = Release|Any CPU
{8EB0B983-8851-4565-B92F-366F1B126E61}.Release|x86.ActiveCfg = Release|Any CPU
{8EB0B983-8851-4565-B92F-366F1B126E61}.Release|x86.Build.0 = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|arm64.ActiveCfg = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|arm64.Build.0 = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|x64.ActiveCfg = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|x64.Build.0 = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|x86.ActiveCfg = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Debug|x86.Build.0 = Debug|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|Any CPU.Build.0 = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|arm64.ActiveCfg = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|arm64.Build.0 = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|x64.ActiveCfg = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|x64.Build.0 = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|x86.ActiveCfg = Release|Any CPU
{FF358174-C540-4FDD-BA8A-8B1589FDEE94}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -11181,6 +11201,7 @@ Global
{56DFE643-7F0F-40C8-9F7F-8EA5357781EF} = {3CBC4802-E9B8-48B7-BC8C-B0AFB9EEC643}
{E0BE6B86-F8DB-405D-AC05-78C8C9D3857D} = {C3722C5D-E159-4AB3-AF60-769185B31B47}
{8EB0B983-8851-4565-B92F-366F1B126E61} = {C3722C5D-E159-4AB3-AF60-769185B31B47}
{FF358174-C540-4FDD-BA8A-8B1589FDEE94} = {4FC50620-4C8B-495F-859C-BFACAD158033}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3E8720B3-DBDD-498C-B383-2CC32A054E8F}
Expand Down
1 change: 1 addition & 0 deletions eng/Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@
$(RepoRoot)src\Mvc\**\src\*.csproj;
$(RepoRoot)src\Azure\**\src\*.csproj;
$(RepoRoot)src\SignalR\**\src\*.csproj;
$(RepoRoot)src\SignalR\**\Client.SourceGenerator\**\*.csproj;
$(RepoRoot)src\Components\**\src\*.csproj;
$(RepoRoot)src\FileProviders\**\src\*.csproj;
$(RepoRoot)src\Configuration.KeyPerFile\**\src\*.csproj;
Expand Down
3 changes: 2 additions & 1 deletion eng/ProjectReferences.props
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.AzureAppServices.HostingStartup" ProjectPath="$(RepoRoot)src\Azure\AzureAppServices.HostingStartup\src\Microsoft.AspNetCore.AzureAppServices.HostingStartup.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.AzureAppServicesIntegration" ProjectPath="$(RepoRoot)src\Azure\AzureAppServicesIntegration\src\Microsoft.AspNetCore.AzureAppServicesIntegration.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Client.Core" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Client.Core\src\Microsoft.AspNetCore.SignalR.Client.Core.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Client.SourceGenerator" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Client.SourceGenerator\src\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Client" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Client\src\Microsoft.AspNetCore.SignalR.Client.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Http.Connections.Client" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Http.Connections.Client\src\Microsoft.AspNetCore.Http.Connections.Client.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Http.Connections.Common" ProjectPath="$(RepoRoot)src\SignalR\common\Http.Connections.Common\src\Microsoft.AspNetCore.Http.Connections.Common.csproj" />
Expand All @@ -140,6 +139,8 @@
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR" ProjectPath="$(RepoRoot)src\SignalR\server\SignalR\src\Microsoft.AspNetCore.SignalR.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Specification.Tests" ProjectPath="$(RepoRoot)src\SignalR\server\Specification.Tests\src\Microsoft.AspNetCore.SignalR.Specification.Tests.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" ProjectPath="$(RepoRoot)src\SignalR\server\StackExchangeRedis\src\Microsoft.AspNetCore.SignalR.StackExchangeRedis.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Client.SourceGenerator" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Client.SourceGenerator\src\gen\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.SignalR.Client.SourceGenerator.HubProxyAttributes" ProjectPath="$(RepoRoot)src\SignalR\clients\csharp\Client.SourceGenerator\src\HubProxyAttributes\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.HubProxyAttributes.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Components.Authorization" ProjectPath="$(RepoRoot)src\Components\Authorization\src\Microsoft.AspNetCore.Components.Authorization.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Components" ProjectPath="$(RepoRoot)src\Components\Components\src\Microsoft.AspNetCore.Components.csproj" />
<ProjectReferenceProvider Include="Microsoft.AspNetCore.Components.Forms" ProjectPath="$(RepoRoot)src\Components\Forms\src\Microsoft.AspNetCore.Components.Forms.csproj" />
Expand Down
7 changes: 4 additions & 3 deletions src/SignalR/SignalR.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"src\\DataProtection\\Abstractions\\src\\Microsoft.AspNetCore.DataProtection.Abstractions.csproj",
"src\\DataProtection\\Cryptography.Internal\\src\\Microsoft.AspNetCore.Cryptography.Internal.csproj",
"src\\DataProtection\\DataProtection\\src\\Microsoft.AspNetCore.DataProtection.csproj",
"src\\Extensions\\Features\\src\\Microsoft.Extensions.Features.csproj",
"src\\FileProviders\\Embedded\\src\\Microsoft.Extensions.FileProviders.Embedded.csproj",
"src\\Hosting\\Abstractions\\src\\Microsoft.AspNetCore.Hosting.Abstractions.csproj",
"src\\Hosting\\Hosting\\src\\Microsoft.AspNetCore.Hosting.csproj",
Expand All @@ -13,7 +14,6 @@
"src\\Hosting\\TestHost\\src\\Microsoft.AspNetCore.TestHost.csproj",
"src\\Http\\Authentication.Abstractions\\src\\Microsoft.AspNetCore.Authentication.Abstractions.csproj",
"src\\Http\\Authentication.Core\\src\\Microsoft.AspNetCore.Authentication.Core.csproj",
"src\\Extensions\\Features\\src\\Microsoft.Extensions.Features.csproj",
"src\\Http\\Headers\\src\\Microsoft.Net.Http.Headers.csproj",
"src\\Http\\Http.Abstractions\\src\\Microsoft.AspNetCore.Http.Abstractions.csproj",
"src\\Http\\Http.Extensions\\src\\Microsoft.AspNetCore.Http.Extensions.csproj",
Expand All @@ -40,8 +40,9 @@
"src\\Servers\\Kestrel\\Core\\src\\Microsoft.AspNetCore.Server.Kestrel.Core.csproj",
"src\\Servers\\Kestrel\\Kestrel\\src\\Microsoft.AspNetCore.Server.Kestrel.csproj",
"src\\Servers\\Kestrel\\Transport.Sockets\\src\\Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.csproj",
"src\\SignalR\\clients\\csharp\\Client.SourceGenerator\\src\\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj",
"src\\SignalR\\clients\\csharp\\Client.Core\\src\\Microsoft.AspNetCore.SignalR.Client.Core.csproj",
"src\\SignalR\\clients\\csharp\\Client.SourceGenerator\\src\\HubProxyAttributes\\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.HubProxyAttributes.csproj",
"src\\SignalR\\clients\\csharp\\Client.SourceGenerator\\src\\gen\\Microsoft.AspNetCore.SignalR.Client.SourceGenerator.csproj",
"src\\SignalR\\clients\\csharp\\Client\\src\\Microsoft.AspNetCore.SignalR.Client.csproj",
"src\\SignalR\\clients\\csharp\\Client\\test\\FunctionalTests\\Microsoft.AspNetCore.SignalR.Client.FunctionalTests.csproj",
"src\\SignalR\\clients\\csharp\\Client\\test\\UnitTests\\Microsoft.AspNetCore.SignalR.Client.Tests.csproj",
Expand Down Expand Up @@ -73,4 +74,4 @@
"src\\WebEncoders\\src\\Microsoft.Extensions.WebEncoders.csproj"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.AspNetCore.SignalR.Client;

/// <summary>
/// Provides information for generating strongly typed SignalR client callbacks.
/// Place this attribute on a method with the following syntax:
/// <code>
/// public static partial IDisposable RegisterCallbacks&lt;T&gt;(this HubConnection connection, T proxy);
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ClientHubAttribute : Attribute
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Attributes to be included in the source generator output</Description>
<TargetFramework>netstandard2.0</TargetFramework>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsShipping>false</IsShipping>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Microsoft.AspNetCore.SignalR.Client.ClientHubAttribute
Microsoft.AspNetCore.SignalR.Client.ClientHubAttribute.ClientHubAttribute() -> void
Microsoft.AspNetCore.SignalR.Client.ServerHubProxyAttribute
Microsoft.AspNetCore.SignalR.Client.ServerHubProxyAttribute.ServerHubProxyAttribute() -> void
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Microsoft.AspNetCore.SignalR.Client;

/// <summary>
/// Provides information for generating strongly typed SignalR server invocations from the client.
/// Place this attribute on a method with the following syntax:
/// <code>
/// public static partial T GetProxy&lt;T&gt;(this HubConnection connection);
/// </code>
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ServerHubProxyAttribute : Attribute
{
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;

namespace Microsoft.AspNetCore.SignalR.Client.SourceGenerator;

internal partial class HubClientProxyGenerator
internal partial class ClientHubGenerator
{
public class Emitter
{
Expand Down Expand Up @@ -49,25 +48,21 @@ private void EmitExtensions()
// Generate body of RegisterCallbackProvider<T>
foreach (var typeSpec in _spec.Types)
{
var indentation = " ";
var methodName = $"Register{typeSpec.FullyQualifiedTypeName.Replace(".", string.Empty)}";
var fqtn = typeSpec.FullyQualifiedTypeName;
registerProviderBody.AppendLine($@"
if (typeof({_spec.SetterTypeParameterName}) == typeof({fqtn}))
{{
return (System.IDisposable) new CallbackProviderRegistration({methodName}({_spec.SetterHubConnectionParameterName}, ({fqtn}) provider));
}}");
{indentation}if(typeof({_spec.SetterTypeParameterName}) == typeof({fqtn}))
{indentation}{{
{indentation} return (System.IDisposable) new CallbackProviderRegistration({methodName}({_spec.SetterHubConnectionParameterName}, ({fqtn}) {_spec.SetterProviderParameterName}));
{indentation}}}");
}

// Generate RegisterCallbackProvider<T> extension method and CallbackProviderRegistration class
// RegisterCallbackProvider<T> is used by end-user to register their callback provider types
// CallbackProviderRegistration is a private implementation of IDisposable which simply holds
// an array of IDisposables acquired from registration of each callback method from HubConnection
var extensions = $@"// <auto-generated>
// Generated by Microsoft.AspNetCore.Client.SourceGenerator
// </auto-generated>

#nullable enable

var extensions = GeneratorHelpers.SourceFilePrefix() + $@"
using Microsoft.AspNetCore.SignalR.Client;

namespace {_spec.SetterNamespace}
Expand Down Expand Up @@ -134,12 +129,7 @@ private void EmitRegistrationMethod(TypeSpec typeSpec)
// The actual registration method goes thru each method that the callback provider type has and then
// registers the method with HubConnection and stashes the returned IDisposable into an array for
// later consumption by CallbackProviderRegistration's constructor
var registrationMethodBody = new StringBuilder($@"// <auto-generated>
// Generated by Microsoft.AspNetCore.Client.SourceGenerator
// </auto-generated>

#nullable enable

var registrationMethodBody = new StringBuilder(GeneratorHelpers.SourceFilePrefix() + $@"
using Microsoft.AspNetCore.SignalR.Client;

namespace {_spec.SetterNamespace}
Expand Down
Loading