[英]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.