简体   繁体   English

在不使用Microsoft.AspNetCore.TestHost中包含的TestServer的情况下运行Kestrel进行测试

[英]Running Kestrel for testing without using the TestServer included in Microsoft.AspNetCore.TestHost

I'm using SoapCore for creating a WCF-ish kind of application using asp.net core 2. 我正在使用SoapCore使用asp.net core 2创建WCF类的应用程序。

This works fine for me so for but I've somewhat hit a brickwall when it comes to integration testing my endpoints. 这样做对我来说很好,但是在集成测试端点时遇到了一些障碍。

Since SoapCore is a middleware and has no relation to any api controllers I'm not able to use an HttpClient for testing the endpoints, hence the TestServer is of no use to me. 由于SoapCore是中间件,并且与任何api控制器都没有关系,所以我无法使用HttpClient来测试端点,因此TestServer对我没有用。

My question is how do I run kestrel side by side with my integration test without using the TestServer, or is there a way to utilize the TestServer in this case? 我的问题是在不使用TestServer的情况下如何与我的集成测试并行运行kestrel,或者在这种情况下是否可以利用TestServer?

I don't think any code here might be of any use but what I got so far is as follows. 我认为这里的任何代码都没有用,但是到目前为止,我得到的是以下内容。

Startup.cs 启动文件

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<IPaymentService>(service => new Services.PaymentService());
        services.AddMvc();
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseSoapEndpoint<IPaymentService>("/PaymentService.svc", new BasicHttpBinding());
        app.UseMvc();
    }
}

PaymentService PaymentService

[ServiceContract]
public interface IPaymentService
{
    [OperationContract]
    string ReadPaymentFiles(string caller);
}

 public class PaymentService : IPaymentService
{
    public string ReadPaymentFiles(string caller)
    {
        return caller;
    }
}

One of my tests: 我的测试之一:

public void Should_Get_Soap_Response_From_PaymentService()
    {
        var testServerFixture = new TestServerFixture();
        var binding = new BasicHttpBinding();
        var endpoint = new EndpointAddress(new Uri("http://localhost:5000/PaymentService.svc"));
        var channelFactory = new ChannelFactory<IPaymentService>(binding, endpoint);

        var serviceClient = channelFactory.CreateChannel();
        var response = serviceClient.ReadPaymentFiles("Ping");
        channelFactory.Close();
    }

The test does'nt do anything right now since it is not calling any live endpoint, which is my problem... 该测试现在不执行任何操作,因为它没有调用任何活动的端点,这是我的问题...

You can use self hosting like Microsoft.AspNetCore.Hosting package. 您可以使用Microsoft.AspNetCore.Hosting包之类的自托管。 Before test execution, you can run your webHost , and then you execute your text over this host. 在执行测试之前,您可以运行webHost ,然后在该主机上执行文本。

public MyTestStartup()
{
    _webhost = WebHost.CreateDefaultBuilder(null)
                      .UseStartup<Startup>()
                      .UseKestrel()
                      .UseUrls(BASE_URL)
                      .Build();
    _webhost.Start();
}

暂无
暂无

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

相关问题 如何使用 (Microsoft.AspNetCore.TestHost)TestServer 和客户端证书 - How to use (Microsoft.AspNetCore.TestHost)TestServer with Client Certificate 使用 Microsoft.AspNetCore.TestHost 热调试 playwright-do.net 测试 - Hot to debug playwright-dotnet tests with Microsoft.AspNetCore.TestHost 从Microsoft.AspNetCore.TestHost.TestServer获取连接字符串值 - Get connection string value from Microsoft.AspNetCore.TestHost.TestServer 用TestHost测试AspNetCore,找不到WebApplicationTestFixture类 - AspNetCore testing with TestHost, WebApplicationTestFixture class not found Kestrel 使用 Microsoft.AspNetCore.Authentication.Negotiate 与多个用户 - Kestrel using Microsoft.AspNetCore.Authentication.Negotiate with multiple users 测试 .net 内核 web api Z594C103F2C6E04C3D8AB059F031E0C 使用 XUnitCoreTestHost 文件上传。 - Testing a .net core web api controller file upload using XUnit and AspNetCore.TestHost Microsoft.AspNetCore.Server.Kestrel[0] 无法启动 Kestrel - Microsoft.AspNetCore.Server.Kestrel[0] Unable to start Kestrel 找不到Microsoft.Owin.Testing.TestServer 404 - Microsoft.Owin.Testing.TestServer 404 not found 集成测试 - 如何使用 Microsoft.Owin.Testing.TestServer 命中端点? - Integration Testing - How to hit endpoints using Microsoft.Owin.Testing.TestServer? Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException - Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2ConnectionErrorException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM