public async Task <IImmutableSet <string> > DetectFeaturesAsync(string projectFullPath, CancellationToken cancellationToken = default)
        {
            if (projectFullPath == null)
            {
                throw new ArgumentNullException(nameof(projectFullPath));
            }

            // If the workspace is uninitialized, we need to do the first access on the UI thread.
            //
            // This is very unlikely to occur, but doing it here for completeness.
            if (!_workspace.IsValueCreated)
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

                GC.KeepAlive(_workspace.Value);
                await TaskScheduler.Default;
            }

            var workspace = _workspace.Value;
            var solution  = workspace.CurrentSolution;

            var project = GetProject(solution, projectFullPath);

            if (project == null)
            {
                // Cannot find matching project.
                return(ImmutableHashSet <string> .Empty);
            }

            var compilation = await project.GetCompilationAsync(cancellationToken).ConfigureAwait(false);

            return(await CompilationFeatureDetector.DetectFeaturesAsync(compilation, cancellationToken));
        }
コード例 #2
0
    public async Task DetectFeaturesAsync_FindsNoFeatures()
    {
        // Arrange
        var source      = @"
using Microsoft.AspNetCore.Builder;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.CompilationFeatureDetectorTest
{
    public class StartupWithNoFeatures
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapFallbackToFile(""index.html"");
            });
        }
    }
}";
        var compilation = TestCompilation.Create(source);
        var symbols     = new StartupSymbols(compilation);

        var type = (INamedTypeSymbol)compilation.GetSymbolsWithName("StartupWithNoFeatures").Single();

        Assert.True(StartupFacts.IsStartupClass(symbols, type));

        // Act
        var features = await CompilationFeatureDetector.DetectFeaturesAsync(compilation);

        // Assert
        Assert.Empty(features);
    }
コード例 #3
0
    public async Task DetectFeatureAsync_StartupWithMapHub_FindsSignalR()
    {
        var source      = @"
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.SignalR;

namespace Microsoft.AspNetCore.Analyzers.TestFiles.CompilationFeatureDetectorTest
{
    public class StartupWithMapHub
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<MyHub>("" / test"");
            });
        }
    }

    public class MyHub : Hub
    {
    }
}
";
        var compilation = TestCompilation.Create(source);
        var symbols     = new StartupSymbols(compilation);

        var type = (INamedTypeSymbol)compilation.GetSymbolsWithName("StartupWithMapHub").Single();

        Assert.True(StartupFacts.IsStartupClass(symbols, type));

        // Act
        var features = await CompilationFeatureDetector.DetectFeaturesAsync(compilation);

        // Assert
        Assert.Collection(features, f => Assert.Equal(WellKnownFeatures.SignalR, f));
    }