运行 Kestrel 用于测试而不使用 Microsoft.AspNetCore.TestHost 中包含的 TestServer
Running Kestrel for testing without using the TestServer included in Microsoft.AspNetCore.TestHost
我正在使用 SoapCore 使用 asp.net 核心 2 创建类似 WCF 的应用程序。
这对我来说很好用,但在集成测试我的端点时我有点碰壁。
由于 SoapCore 是一个中间件并且与任何 api 控制器无关,我无法使用 HttpClient 来测试端点,因此 TestServer 对我没有用。
我的问题是如何在不使用 TestServer 的情况下 运行 kestrel 与我的集成测试并行,或者在这种情况下有没有办法利用 TestServer?
我认为这里的任何代码都没有用,但到目前为止我得到的如下。
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();
}
}
支付服务
[ServiceContract]
public interface IPaymentService
{
[OperationContract]
string ReadPaymentFiles(string caller);
}
public class PaymentService : IPaymentService
{
public string ReadPaymentFiles(string caller)
{
return caller;
}
}
我的一个测试:
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();
}
测试现在没有做任何事情,因为它没有调用任何实时端点,这是我的问题...
您可以像 Microsoft.AspNetCore.Hosting
包一样使用自托管。在执行测试之前,您可以 运行 您的 webHost
,然后在此主机上执行您的文本。
public MyTestStartup()
{
_webhost = WebHost.CreateDefaultBuilder(null)
.UseStartup<Startup>()
.UseKestrel()
.UseUrls(BASE_URL)
.Build();
_webhost.Start();
}
我正在使用 SoapCore 使用 asp.net 核心 2 创建类似 WCF 的应用程序。
这对我来说很好用,但在集成测试我的端点时我有点碰壁。
由于 SoapCore 是一个中间件并且与任何 api 控制器无关,我无法使用 HttpClient 来测试端点,因此 TestServer 对我没有用。
我的问题是如何在不使用 TestServer 的情况下 运行 kestrel 与我的集成测试并行,或者在这种情况下有没有办法利用 TestServer?
我认为这里的任何代码都没有用,但到目前为止我得到的如下。
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();
}
}
支付服务
[ServiceContract]
public interface IPaymentService
{
[OperationContract]
string ReadPaymentFiles(string caller);
}
public class PaymentService : IPaymentService
{
public string ReadPaymentFiles(string caller)
{
return caller;
}
}
我的一个测试:
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();
}
测试现在没有做任何事情,因为它没有调用任何实时端点,这是我的问题...
您可以像 Microsoft.AspNetCore.Hosting
包一样使用自托管。在执行测试之前,您可以 运行 您的 webHost
,然后在此主机上执行您的文本。
public MyTestStartup()
{
_webhost = WebHost.CreateDefaultBuilder(null)
.UseStartup<Startup>()
.UseKestrel()
.UseUrls(BASE_URL)
.Build();
_webhost.Start();
}