Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.NETCore3.0 Api集成测试出现问题_C#_Asp.net_.net_Asp.net Mvc_Asp.net Core - Fatal编程技术网

C# Asp.NETCore3.0 Api集成测试出现问题

C# Asp.NETCore3.0 Api集成测试出现问题,c#,asp.net,.net,asp.net-mvc,asp.net-core,C#,Asp.net,.net,Asp.net Mvc,Asp.net Core,我为Asp.NETCore3.0配置了AutoFac,但现在我在ControllerTestBase类上遇到了问题。我知道我不应该使用WebHostBuilder编写其他代码,但我尝试了很多次。我只有新的错误 ControllerTestBase.cs namespace Passenger.Tests.EndToEnd.Controllers { public abstract class ControllerTestsBase { protected read

我为Asp.NETCore3.0配置了AutoFac,但现在我在ControllerTestBase类上遇到了问题。我知道我不应该使用WebHostBuilder编写其他代码,但我尝试了很多次。我只有新的错误

ControllerTestBase.cs

namespace Passenger.Tests.EndToEnd.Controllers
{
    public abstract class ControllerTestsBase
    {
        protected readonly TestServer Server;
        protected readonly HttpClient Client;

        protected ControllerTestsBase()
        {
            var hostBuilder = new HostBuilder()
              .ConfigureWebHost(webHost =>
              {
                // Add TestServer
                webHost.UseTestServer();
                webHost.UseStartup<Startup>();
              });

            var host = hostBuilder.Start();
            Client = host.GetTestClient();

        }

        protected static StringContent GetPayload(object data)
        {
            var json = JsonConvert.SerializeObject(data);

            //Content-Type: "application/json"
            return new StringContent(json, Encoding.UTF8, "application/json");
        }


    }
}



错误消息:

您不需要使用Autofac,因为.net core 3.0提供内置依赖项注入

阅读更多关于

重构代码并使用依赖项注入进行对象初始化

    namespace Passenger.Tests.EndToEnd.Controllers
  {
     public class ControllerTestsBase
    {
    private readonly ITestServer _testServer;
    private readonly HttpClient Client;

    protected ControllerTestsBase(ITestServer testServer,HttpClient client)
    {
        _testServer = testServer;
         client.BaseAddress = new Uri("https://www.stackoverflow.com");

         client.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");

         client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample");

         Client = client;
    }

    protected static StringContent GetPayload(object data)
    {
        var json = JsonConvert.SerializeObject(data);

        //Content-Type: "application/json"
        return new StringContent(json, Encoding.UTF8, "application/json");
    }


}

}

您遇到了什么错误?@MattU请看下面我的错误。您好,欢迎来到StackOverflow。正如Matt提到的,在问题中包含任何错误(包括它们的堆栈跟踪)是很重要的。我需要在课程中使用Autofac。
    namespace Passenger.Tests.EndToEnd.Controllers
  {
     public class ControllerTestsBase
    {
    private readonly ITestServer _testServer;
    private readonly HttpClient Client;

    protected ControllerTestsBase(ITestServer testServer,HttpClient client)
    {
        _testServer = testServer;
         client.BaseAddress = new Uri("https://www.stackoverflow.com");

         client.DefaultRequestHeaders.Add("Accept","application/vnd.github.v3+json");

         client.DefaultRequestHeaders.Add("User-Agent","HttpClientFactory-Sample");

         Client = client;
    }

    protected static StringContent GetPayload(object data)
    {
        var json = JsonConvert.SerializeObject(data);

        //Content-Type: "application/json"
        return new StringContent(json, Encoding.UTF8, "application/json");
    }


}