Closed vinh84 closed 7 years ago
@vinhhrv Glad you find it useful.
I'm not sure but I'd say it's probably something to do with the IApplicationLifetime
, where are you getting that instance from? Maybe you can mock that?
If you're using the Startup
class from the client application, you could probably create a TestStartup
and use that with a mocked IApplicationLifetime
OK I found the problem, when running in a unit test, when is the ApplicationLifetime.ApplicationStopping
token expected to be cancelled? In my test the token is never cancelled so the reporter continues to run.
For your tests are you able to just use a test startup to run your TestServer
? e..g https://github.com/alhardy/AppMetrics/blob/dev/test/App.Metrics.Extensions.Middleware.Integration.Facts/Startup/DefaultTestStartup.cs Alternatively, have the cancellation token on the IApplicationLifetime
cancelled after your test run.
@alhardy
When i use UseMetricsReporting
IApplicationLifetime
appLifetime.ApplicationStarted.Register(() => { });
Not Work, issue is criticle, please fix
@vinhhrv can you provide some more details please? If possible a sample or unit test, otherwise what exactly isn't working and what are you trying to achieve.
Unit Test For Can't Start with Test Server
When run in real env, application can start, but ApplicationLifetime problem
appLifetime.ApplicationStarted.Register(() => { // not fire });
@vinhhrv The UseMetricsReporting
extension method is registering a delegate on the IApplicationLIfetime
and therefore will override the delegate you are registering. UseMetricsReporting
is simply a helper method to simplify running a reporter in a web context.
If you require more than just registering a reporter you can implement the same thing yourself, e.g. change your code sample's Startup_with_reporting
Configure
method to the following:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime)
{
var reportFactory = app.ApplicationServices.GetRequiredService<IReportFactory>();
var metrics = app.ApplicationServices.GetRequiredService<IMetrics>();
var reporter = reportFactory.CreateReporter();
appLifetime.ApplicationStarted.Register(
() =>
{
IsStarted = true;
reporter.RunReports(metrics, appLifetime.ApplicationStopping);
});
app.UseMetrics();
}
appLifetime.ApplicationStarted.Register(
() =>
{
IsStarted = true;
reporter.RunReports(metrics, appLifetime.ApplicationStopping);
Console.Write("event one never run");
});
appLifetime.ApplicationStarted.Register(
() =>
{
Console.Write("event two never run tow");
});
this is problem, the code run blocked here. reporter.RunReports(metrics, appLifetime.ApplicationStopping); this function shoult start your scheduler service and return.
if u remove this line, two event is run
FluentScheduler, u can try this for task scheduler
@vinhhrv I see, for now you can just run reporter.RunReports
in a new Task
. Task.Run(() => ... )
I'm familiar with FluentScheduler, I don't want to take any other dependencies when they can be avoided, plus that's a bit overkill for my needs.
I use your sample Api.InfluxDB.Sample Add XUnitTest project Try unit test above can't work
Try comment this //app.UseMetricsReporting(lifetime); => Work
Thank for you lib, it very usefull