简体   繁体   English

VS 2017 ASP.NET Core测试项目-缺少Microsoft.AspNetCore.Identity

[英]VS 2017 ASP.NET Core Test Project - Microsoft.AspNetCore.Identity missing

I am trying to write an integration test for an empty .NET Core ASP site using the Microsoft.AspNetCore.TestHost 我正在尝试使用Microsoft.AspNetCore.TestHost为一个空的.NET Core ASP站点编写集成测试

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException :
One or more compilation failures occurred:
/Views/_ViewImports.cshtml(5,28): 

error CS0234: The type or namespace name 'Identity' 
does not exist in the namespace 'Microsoft.AspNetCore' 
(are you missing an assembly reference?) 4uvgaffv.11j(34,11): 

error CS0246: The type or namespace name 'System' could not be found 
(are you missing a using directive or an assembly reference?)

My Test class is identical to the documentation one and looks as follows: 我的Test类与文档一类相同,如下所示:

public class UnitTest1
{
    private readonly TestServer _server;
    private readonly HttpClient _client;
    public UnitTest1()
    {
        // Arrange
        _server = new TestServer(new WebHostBuilder()
            .UseContentRoot(ContentPath)
            .UseStartup<Startup>());

        _client = _server.CreateClient();
    }

    [Fact]
    public async Task ReturnHelloWorld()
    {
        // Act
        var response = await _client.GetAsync("/");

        response.EnsureSuccessStatusCode();

        var body = await response.Content.ReadAsStringAsync();

        // Assert
        Console.WriteLine("Test");
    }

    private static string ContentPath
    {
        get
        {
            var path = PlatformServices.Default.Application.ApplicationBasePath;
            var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(DataTests)}"));
            return contentPath;
        }
    }
}

I have tried adding Microsoft.AspNetCore.Identity 1.1.1 NuGet package to the Test project (same one as MVC Project) but it didn't do anything, although I can see it as missing in the Dependencies dropdown: 我尝试将Microsoft.AspNetCore.Identity 1.1.1 NuGet程序包添加到Test项目(与MVC Project相同),但是它什么也没做,尽管我在Dependencies下拉列表中看到它缺失:

在此处输入图片说明

I have tried reinstalling those packages, dotnet build , dotnet restore , clean rebuild but still no luck. 我试过重新安装这些软件包, dotnet builddotnet restore ,clean rebuild,但是还是没有运气。

Any ideas anyone? 有任何想法吗?

FIX 固定

Final fix for this was (thanks to @Jeffrey 最终的解决方法是(感谢@Jeffrey

WebHostBuilderExtensions.cs WebHostBuilderExtensions.cs

public static class WebHostBuilderExtensions
{
    private static string ContentPath
    {
        get
        {
            var path = PlatformServices.Default.Application.ApplicationBasePath;
            var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(DataTests)}"));
            return contentPath;
        }
    }

    public static IWebHostBuilder ConfigureTestContent(this IWebHostBuilder builder)
    {
        return builder.UseContentRoot(ContentPath);
    }

    public static IWebHostBuilder ConfigureTestServices(this IWebHostBuilder builder)
    {
        return builder.ConfigureServices(services =>
        {
            services.AddMvcCore();
            services.Configure((RazorViewEngineOptions options) =>
            {
                var previous = options.CompilationCallback;
                options.CompilationCallback = (context) =>
                {
                    previous?.Invoke(context);

                    var assembly = typeof(Startup).GetTypeInfo().Assembly;
                    var assemblies = assembly.GetReferencedAssemblies()
                                             .Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
                                             .ToList();
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Private.Corelib")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.ApplicationInsights.AspNetCore")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Html.Abstractions")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor.Runtime")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Mvc")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Dynamic.Runtime")).Location));
                    assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Text.Encodings.Web")).Location));

                    context.Compilation = context.Compilation.AddReferences(assemblies);
                };
            });

            services.AddApplicationInsightsTelemetry();
        });
    }
}

Test.cs Test.cs

public class UnitTest1
{
    private readonly TestServer _server;
    private readonly HttpClient _client;
    private readonly ITestOutputHelper output;

    public UnitTest1(ITestOutputHelper output)
    {
        this.output = output;

        // Arrange
        _server = new TestServer(new WebHostBuilder()
            .ConfigureTestContent()
            .ConfigureLogging(l => l.AddConsole())
            .UseStartup<Startup>()
            .ConfigureTestServices());

        _client = _server.CreateClient();
    }

    [Fact]
    public async Task ReturnHelloWorld()
    {
        // Act
        var response = await _client.GetAsync("/");

        response.EnsureSuccessStatusCode();

        var body = await response.Content.ReadAsStringAsync();

        // Assert
        output.WriteLine(body);
    }
}

Had the same issue.. After some digging found a working solution.. 有同样的问题..经过一番挖掘发现了一个可行的解决方案..

The roslyn compiler used in Razor doesn't include the referenced assemblies of the main assembly.. So I added these by looking them up Razor中使用的roslyn编译器不包含主程序集的引用程序集。因此,我通过查找它们来添加它们

In the test class add the following code.. Works on my machine™ 在测试类中,添加以下代码。

private static string ContentPath
{
    get
    {
        var path = PlatformServices.Default.Application.ApplicationBasePath;
        var contentPath = Path.GetFullPath(Path.Combine(path, $@"..\..\..\..\{nameof(src)}"));
        return contentPath;
    }
}

.

var builder = new WebHostBuilder()
    .UseContentRoot(ContentPath)
    .ConfigureLogging(factory =>
    {
        factory.AddConsole();
    })
    .UseStartup<Startup>()
    .ConfigureServices(services =>
     {
         services.Configure((RazorViewEngineOptions options) =>
         {
             var previous = options.CompilationCallback;
             options.CompilationCallback = (context) =>
             {
                 previous?.Invoke(context);

                 var assembly = typeof(Startup).GetTypeInfo().Assembly;
                 var assemblies = assembly.GetReferencedAssemblies().Select(x => MetadataReference.CreateFromFile(Assembly.Load(x).Location))
                 .ToList();
                 assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location));
                 assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Private.Corelib")).Location));
                 assemblies.Add(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("Microsoft.AspNetCore.Razor")).Location));

                 context.Compilation = context.Compilation.AddReferences(assemblies);
             };
         });
     });

    _server = new TestServer(builder);

Same issue on GitHub repo https://github.com/aspnet/Hosting/issues/954 GitHub回购上的同一问题https://github.com/aspnet/Hosting/issues/954

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .Net 标准项目中使用 Microsoft.AspNetCore.Identity? - Use Microsoft.AspNetCore.Identity in .Net Standard Project? Microsoft.Aspnet.identity.Core与Microsoft.AspNetCore.Identity之间有什么区别 - What is the difference between Microsoft.Aspnet.identity.Core vs Microsoft.AspNetCore.Identity 将现有的Microsoft.AspNet.Identity DB(EF 6)迁移到Microsoft.AspNetCore.Identity(EF Core) - Migrate existing Microsoft.AspNet.Identity DB (EF 6) to Microsoft.AspNetCore.Identity (EF Core) Blazor 和 Microsoft.AspNetCore.Identity 需要电子邮件 - Blazor and Microsoft.AspNetCore.Identity Requiring Email 在同一个 Web 项目中使用 Microsoft Identity Web App 和 Microsoft.AspNetCore.Identity 的问题 - Problems using Microsoft Identity Web App and Microsoft.AspNetCore.Identity in the same Web Project Asp.Net Core没有类型为Microsoft.AspNetCore.Identity.RoleManager的服务 - Asp.Net Core No Service for Type Microsoft.AspNetCore.Identity.RoleManager ASP.Net Core 2.1:没有针对Microsoft.AspNetCore.Identity.UserManager类型的服务 - ASP.Net Core 2.1: No service for type Microsoft.AspNetCore.Identity.UserManager ASP.NET Core 项目中的 Microsoft Identity Platform - Microsoft Identity Platform in an ASP.NET Core project 在 Microsoft.AspNetCore.Identity Identity 中使用非 ASCII 字符 - Using non-ASCII characters in Microsoft.AspNetCore.Identity Identity 可以在没有Microsoft.AspNetCore.All包的情况下创建ASP.NET Core 2.0项目吗? - Can an ASP.NET Core 2.0 project be created without a Microsoft.AspNetCore.All package?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM