How to call controller method on service start on Topshelf - c#

I am trying to run a method on my controller class when my topshelf service starts and I am now finding how to do it. I search and I couldn't find an answer. Can I call a method of my controller class on service startup directly or I do have to do an HttpClient and call an url on localhost?
Code:
Program.cs:
HostFactory.Run(x =>
{
x.Service<OwinService>(s =>
{
s.ConstructUsing(() => new OwinService());
s.WhenStarted(service => service.Start());
s.WhenStopped(service => service.Stop());
});
x.RunAsLocalSystem();
x.StartAutomatically();
x.SetServiceName("Test Service");
x.SetDisplayName("Test Service");
x.SetDescription("Service that Imports / Exports to DB information");
x.EnableServiceRecovery(recoveryOption =>
{
recoveryOption.RestartService(0);
});
});
OwinService.cs:
public class OwinService
{
private IDisposable _webApp;
public void Start()
{
//_timer.Start();
_webApp = WebApp.Start<ApiConfiguration>("http://+:9000");
}
public void Stop()
{
//_timer.Stop();
_webApp.Dispose();
}
}
OutputController.cs
public class OutputController : ApiController
{
public void DoSomething() {};
}
I want to call the DoSomething() on startup.

Related

WebApplicationFactory is running the Api with the old IConnectionMultiplexer which fails to connect instead of first overriding configuration

I'm working on an integration test for a Web API which communicates through Redis, so I tried to replace the Redis Server with a containerized one and run some tests.
The issue is that it is first running the Api with project's appsettings.Development.json configuration and the old IConnectionMultiplexer instance which obviously won't connect because the hostname is offline. The question is how do I make it run the project with the new IConnectionMultiplexer that uses the containerized Redis Server? Basically the sequence is wrong there. What I did is more like run the old IConnectionMultiplexer and replace it with the new one but it wouldn't connect to the old one, so that exception prevents me from continuing. I commented the line of code where it throws the exception but as I said it's obvious because it's first running the Api with the old configuration instead of first overriding the configuration and then running the Api.
I could have done something like the following but I'm DI'ing other services based on configuration as well, meaning I must override the configuration first and then run the actual API code.
try
{
var redis = ConnectionMultiplexer.Connect(redisConfig.Host);
serviceCollection.AddSingleton<IConnectionMultiplexer>(redis);
}
catch
{
// We discard that service if it's unable to connect
}
Api
public static class RedisConnectionConfiguration
{
public static void AddRedisConnection(this IServiceCollection serviceCollection, IConfiguration config)
{
var redisConfig = config.GetSection("Redis").Get<RedisConfiguration>();
serviceCollection.AddHostedService<RedisSubscription>();
serviceCollection.AddSingleton(redisConfig);
var redis = ConnectionMultiplexer.Connect(redisConfig.Host); // This fails because it didn't override Redis:Host
serviceCollection.AddSingleton<IConnectionMultiplexer>(redis);
}
}
Integration tests
public class OrderManagerApiFactory : WebApplicationFactory<IApiMarker>, IAsyncLifetime
{
private const string Password = "Test1234!";
private readonly TestcontainersContainer _redisContainer;
private readonly int _externalPort = Random.Shared.Next(10_000, 60_000);
public OrderManagerApiFactory()
{
_redisContainer = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("redis:alpine")
.WithEnvironment("REDIS_PASSWORD", Password)
.WithPortBinding(_externalPort, 6379)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(6379))
.Build();
}
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.UseEnvironment("Development");
builder.ConfigureLogging(logging =>
{
logging.ClearProviders();
});
builder.ConfigureAppConfiguration(config =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
{
{ "Redis:Host", $"localhost:{_externalPort},password={Password},allowAdmin=true" },
{ "Redis:Channels:Main", "main:new:order" },
});
});
builder.ConfigureTestServices(services =>
{
services.RemoveAll(typeof(IConnectionMultiplexer));
services.AddSingleton<IConnectionMultiplexer>(_ =>
ConnectionMultiplexer.Connect($"localhost:{_externalPort},password={Password},allowAdmin=true"));
});
}
public async Task InitializeAsync()
{
await _redisContainer.StartAsync();
}
public new async Task DisposeAsync()
{
await _redisContainer.DisposeAsync();
}
}
public class OrderManagerTests : IClassFixture<OrderManagerApiFactory>, IAsyncLifetime
{
private readonly OrderManagerApiFactory _apiFactory;
public OrderManagerTests(OrderManagerApiFactory apiFactory)
{
_apiFactory = apiFactory;
}
[Fact]
public async Task Test()
{
// Arrange
var configuration = _apiFactory.Services.GetRequiredService<IConfiguration>();
var redis = _apiFactory.Services.GetRequiredService<IConnectionMultiplexer>();
var channel = configuration.GetValue<string>("Redis:Channels:Main");
// Act
await redis.GetSubscriber().PublishAsync(channel, "ping");
// Assert
}
public Task InitializeAsync()
{
return Task.CompletedTask;
}
public Task DisposeAsync()
{
return Task.CompletedTask;
}
}
Problem solved.
If you override WebApplicationFactory<T>.CreateHost() and call IHostBuilder.ConfigureHostConfiguration() before calling base.CreateHost() the configuration you add will be visible between WebApplication.CreateBuilder() and builder.Build().
The following two links might help someone:
https://github.com/dotnet/aspnetcore/issues/37680
https://github.com/dotnet/aspnetcore/issues/9275
public sealed class OrderManagerApiFactory : WebApplicationFactory<IApiMarker>, IAsyncLifetime
{
private const string Password = "Test1234!";
private const int ExternalPort = 7777; // Random.Shared.Next(10_000, 60_000);
private readonly TestcontainersContainer _redisContainer;
public OrderManagerApiFactory()
{
_redisContainer = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("redis:alpine")
.WithEnvironment("REDIS_PASSWORD", Password)
.WithPortBinding(ExternalPort, 6379)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(6379))
.Build();
}
public async Task InitializeAsync()
{
await _redisContainer.StartAsync();
}
public new async Task DisposeAsync()
{
await _redisContainer.DisposeAsync();
}
protected override IHost CreateHost(IHostBuilder builder)
{
builder.ConfigureHostConfiguration(config =>
config.AddInMemoryCollection(new[]
{
new KeyValuePair<string, string>("Redis:Host", $"localhost:{ExternalPort},password={Password},allowAdmin=true"),
new KeyValuePair<string, string>("Redis:Channels:Main", "main:new:order")
}));
return base.CreateHost(builder);
}
}

Verify that method is called within Action

I am practicing unit-testing a lot of these days, so bear with me if I fail to understand some basics.
Having these simple abstractions:
public interface ITaskFactory
{
void StartTask(Action action);
}
internal sealed class TaskFactory : ITaskFactory
{
public void StartTask(Action action)
{
Task.Factory.StartNew(action);
}
}
And this class to test (simplified to this case):
internal sealed class TriggerEventDecorator<TEvent> : ITriggerEvent<TEvent> where TEvent : IEvent
{
private readonly ITaskFactory _taskFactory;
private readonly Func<ITriggerEvent<TEvent>> _factory;
public TriggerEventDecorator(ITaskFactory taskFactory, Func<ITriggerEvent<TEvent>> factory)
{
_taskFactory = taskFactory;
_factory = factory;
}
public void Trigger(TEvent evt)
{
_taskFactory.StartTask(() =>
{
_factory().Trigger(evt);
});
}
}
And my test of this class:
public class TriggerEventDecoratorTests
{
[Fact]
public void CanTriggerEventHandler()
{
var evt = new FakeEventWithoutValidation();
Assert.IsAssignableFrom<IEvent>(evt);
var decorated = new Mock<ITriggerEvent<FakeEventWithoutValidation>>(MockBehavior.Strict);
decorated.Setup(x => x.Trigger(evt));
var taskFactory = new Mock<ITaskFactory>(MockBehavior.Strict);
taskFactory.Setup(factory => factory.StartTask(It.IsAny<Action>()));
var decorator = new TriggerEventDecorator<FakeEventWithoutValidation>(taskFactory.Object, () => decorated.Object);
decorator.Trigger(evt);
taskFactory.Verify(x => x.StartTask(It.IsAny<Action>()), Times.Once);
decorated.Verify(x => x.Trigger(evt), Times.Once); // This line is not verified
}
}
The line decorated.Verify(x => x.Trigger(evt), Times.Once); is not verified, it is never invoked.
How do I test that this is trigged in the Action of the _taskFactory?
You didn't invoke the Func method. This is the problem... To do so you'll have to use Callback method.
Change the following sertup:
taskFactory.Setup(factory => factory.StartTask(It.IsAny<Action>()));
To:
taskFactory.Setup(factory => factory.StartTask(It.IsAny<Action>()))
.Callback<Action>((action) => action());

How to pre-load data in an OWIN self-hosted app

I have a OWIN/Katana self-hosted service app.
One of its functions is to service some data over WebAPI.
In this app I have a class called dataManager, which is responsible for retrieving the data, and passing it onto the API controller, which asked for it.
The data is ultimately served to a mobile platform, so it is very important to cache as much as possible for performance.
Is there a way to pre-load my DataManager at the application startup, and have it pre-execute it's linq queries?
The Application class looks like this:
namespace TaskManager
{
using System;
using Microsoft.Owin.Hosting;
public class TaskManagerApplication
{
protected IDisposable WebApplication;
public void Start()
{
WebApplication = WebApp.Start<WebPipeline>("http://*:8080");
}
public void Stop()
{
WebApplication.Dispose();
}
}
}
The Program class looks like this:
namespace TaskManager
{
using Topshelf;
internal class Program
{
private static int Main()
{
var exitCode = HostFactory.Run(host =>
{
host.Service<TaskManagerApplication>(service =>
{
service.ConstructUsing(() => new TaskManagerApplication());
service.WhenStarted(a => a.Start());
service.WhenStopped(a => a.Stop());
});
host.SetDescription("Task Manager");
host.SetDisplayName("Task Manager");
host.SetServiceName("TaskManager");
host.RunAsNetworkService();
});
return (int) exitCode;
}
}
}
And the data retrieval statement contained within DataManager class look like this:
var rawData = from data in new XPQuery<AccountView3.PipelineData>(uow)
where data.Stage.ToLower().Contains("won")
&& data.RevenueStartDate.Value.Year == DateTime.Today.Year
&& data.WeekOfTheYear >= priorWeekCutoff
select data;
What I do is create a public static class in the API library. That's where I modify the HttpConfiguration object. That is also where I define OnStartup() and OnShutdown() methods. I then call these methods in the pipeline class's methods (your WebPipeline class).
For example (in the MyWebApi library, where my controllers and stuff live):
public class Service
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.EnsureInitialized();
}
public static void OnStartup()
{
// add any startup logic here, like caching your data
}
public static void OnShutdown()
{
// add any cleanup logic here
}
}
Then in the pipeline class:
public class WebPipeline
{
public static void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
MyWebApi.Service.Register(config);
MyWebApi.Service.OnStartup();
app.UseWebApi(config);
}
public static void Shutdown()
{
MyWebApi.Service.OnShutdown();
}
}
Now your TaskManagerApplication.Start() will result in the API OnStartup() being called. Then you just have to add a call to WebPipeline.Shutdown() in your TaskManagerApplication.Stop() method.

NServiceBus.IStartableBus is not registered in the container. NServiceBus 4.6.3

I am attempting a very simple application with NServiceBus and Ninject.
I am attempting to use Ninject as the container for NServiceBus, but I am getting the following error - "NServiceBus.IStartableBus is not registered in the container."
I'm sure the answer is quite obvious... just not to me!
My code is as follows
public class StartApp : IWantCustomInitialization, IWantToRunWhenBusStartsAndStops
{
private static IKernel _kernel;
public IBus Bus { get; set; }
public void Init()
{
Configure.Serialization.Json();
}
public void Start()
{
_kernel = new StandardKernel();
Configure.With()
.NinjectBuilder(_kernel)
.CreateBus()
.Start();
Bus.Send(new TestMessage {Id = Guid.NewGuid(), MessageText = "Bloop"});
}
public void Stop()
{
}
}
namespace NServiceBus_Ninject_Simple
{
public class EndpointConfig : IConfigureThisEndpoint, AsA_Server
{ }
}
This google groups discussion is about the same issue.
It seems you are creating the configuration in the wrong place.
It should look like this:
public abstract class DefaultEndpointConfig
: IConfigureThisEndpoint
, IWantCustomInitialization
{
IWantCustomInitialization.Init()
{
Configure
.With()
.NinjectBuilder();
// + any other config;
// Call optional endpoint specific config
Init();
}
public virtual void Init()
{
}
}
See here (Johannes Gustafsson)
It needs to be done in the EndPoint-Configuration (for every endpoint, this is why he suggests using a base class) and it needs to implement IConfigureThisEndpoint.

How to test a command- and event based system with Masstransit

I have a command handler that invokes an operation on a domain object which in turn fires an event when the operation has been executed. I'd like to test that an event handler receives the event when the corresponding command has been sent (see below, some code omitted for brevity). The event handler (MyEventConsumer.Consume) is never invoked even though the event message is published on the bus (loopback bus in this case). Any ideas?
//Test
[TestFixture]
public class TestSendCommandReceiveEvent
{
[Given]
public void installation_of_infrastructure_objects()
{
container.Register(Component.For<MyEventConsumer>().UsingFactoryMethod(() => new MyEventConsumer(_received)));
container.Register(
Component.For<IServiceBus>()
.UsingFactoryMethod(() => ServiceBusFactory.New(x => { x.ReceiveFrom("loopback://localhost/mt_client"); x.Subscribe(conf => conf.LoadFrom(container)); })));
}
[When]
public void sending_a_command()
{
var LocalBus = container.Resolve<IServiceBus>();
LocalBus.Publish(new DoSomething(_aggregateId));
}
[Then]
public void corresponding_event_should_be_received_by_consumer()
{
_received.WaitOne(5000).ShouldBeTrue();
}
}
public class MyEventConsumer : Consumes<SomethingDone>.All
{
private readonly ManualResetEvent _received;
public MyEventConsumer(ManualResetEvent received)
{
_received = received;
}
public void Consume(SomethingDone message)
{
_received.Set();
}
}
//Command handler
public class DoSomethingCommandHandler : Consumes<DoSomething>.All where T:class
{
public void Consume(DoSomething message)
{
var ar = Repository.GetById<SomeAR>(message.ArId);
ar.DoSomething();
Repository.Save(ar, Guid.NewGuid(), null);
}
}
//Domain object
public class SomeDomainObject : AggregateBase
{
public void DoSomething()
{
RaiseEvent(new SomethingDone(Id, 1));
}
}
This passes for me:
// Copyright 2012 Henrik Feldt
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
using System;
using System.Threading;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using Magnum.Extensions;
using Magnum.TestFramework;
using MassTransit;
using NUnit.Framework;
namespace ConsoleApplication11
{
[TestFixture]
public class TestSendCommandReceiveEvent
{
ManualResetEventSlim _received = new ManualResetEventSlim(false);
IWindsorContainer _container;
[Given]
public void installation_of_infrastructure_objects()
{
_container = new WindsorContainer();
_container.Register(
Component.For<IServiceBus>()
.UsingFactoryMethod(() => ServiceBusFactory.New(x =>
{
x.ReceiveFrom("loopback://localhost/mt_client");
x.Subscribe(conf =>
{
conf.Consumer(() => new MyEventConsumer(_received));
conf.Consumer(() => new MyCmdConsumer());
});
})));
when();
}
public void when()
{
var localBus = _container.Resolve<IServiceBus>();
// wait for startup
localBus.Endpoint.InboundTransport.Receive(c1 => c2 => { }, 1.Milliseconds());
localBus.Publish(new DoSomething());
}
[Then]
public void corresponding_event_should_be_received_by_consumer()
{
_received.Wait(5000).ShouldBeTrue();
}
}
[Serializable]
public class DoSomething
{
}
[Serializable]
public class SomethingDone
{
}
public class MyEventConsumer : Consumes<SomethingDone>.All
{
readonly ManualResetEventSlim _received;
public MyEventConsumer(ManualResetEventSlim received)
{
_received = received;
}
public void Consume(SomethingDone message)
{
_received.Set();
}
}
public class MyCmdConsumer : Consumes<DoSomething>.Context
{
public void Consume(IConsumeContext<DoSomething> ctx)
{
Console.WriteLine("consumed cmd");
ctx.Bus.Publish(new SomethingDone());
}
}
}
In my experience, there is a short period of time, right after creation of the bus instance, during which any published messages are lost. Must be some kind of async initialization going on.
Try adding a delay between container.Resolve<IServiceBus>() and LocalBus.Publish(new DoSomething(_aggregateId)).
Thread.Sleep did not work in my case, but a Console.ReadLine() surprisingly did!

Categories