I'm trying make a telegram bot with reminder. I'm using Telegram.Bot 14.10.0, Quartz 3.0.7, .net core 2.0. The first version should : get message "reminder" from telegram, create job (using Quartz) and send meaasage back in 5 seconds.
My console app with DI looks like:
Program.cs
static IBot _botClient;
public static void Main(string[] args)
{
// it doesn't matter
var servicesProvider = BuildDi(connecionString, section);
_botClient = servicesProvider.GetRequiredService<IBot>();
_botClient.Start(appModel.BotConfiguration.BotToken, httpProxy);
var reminderJob = servicesProvider.GetRequiredService<IReminderJob>();
reminderJob.Bot = _botClient;
Console.ReadLine();
_botClient.Stop();
// it doesn't matter
}
private static ServiceProvider BuildDi(string connectionString, IConfigurationSection section)
{
var rJob = new ReminderJob();
var sCollection = new ServiceCollection()
.AddSingleton<IBot, Bot>()
.AddSingleton<ReminderJob>(rJob)
.AddSingleton<ISchedulerBot>(s =>
{
var schedBor = new SchedulerBot();
schedBor.StartScheduler();
return schedBor;
});
return sCollection.BuildServiceProvider();
}
Bot.cs
public class Bot : IBot
{
static TelegramBotClient _botClient;
public void Start(string botToken, WebProxy httpProxy)
{
_botClient = new TelegramBotClient(botToken, httpProxy);
_botClient.OnReceiveError += BotOnReceiveError;
_botClient.OnMessage += Bot_OnMessage;
_botClient.StartReceiving();
}
private static async void Bot_OnMessage(object sender, MessageEventArgs e)
{
var me = wait _botClient.GetMeAsync();
if (e.Message.Text == "reminder")
{
var map= new Dictionary<string, object> { { ReminderJobConst.ChatId, e.Message.Chat.Id.ToString() }, { ReminderJobConst.HomeWordId, 1} };
var job = JobBuilder.Create<ReminderJob>().WithIdentity($"{prefix}{rnd.Next()}").UsingJobData(new JobDataMap(map)).Build();
var trigger = TriggerBuilder.Create().WithIdentity($"{prefix}{rnd.Next()}").StartAt(DateTime.Now.AddSeconds(5).ToUniversalTime())
.Build();
await bot.Scheduler.ScheduleJob(job, trigger);
}
}
}
Quartz.net not allow use constructor with DI. That's why I'm trying to create property with DI.
ReminderJob.cs
public class ReminderJob : IJob
{
static IBot _bot;
public IBot Bot { get; set; }
public async Task Execute(IJobExecutionContext context)
{
var parameters = context.JobDetail.JobDataMap;
var userId = parameters.GetLongValue(ReminderJobConst.ChatId);
var homeWorkId = parameters.GetLongValue(ReminderJobConst.HomeWordId);
await System.Console.Out.WriteLineAsync("HelloJob is executing.");
}
}
How can I pass _botClient to reminderJob in Program.cs?
If somebody looks for answer, I have one:
Program.cs (in Main)
var schedBor = servicesProvider.GetRequiredService<ISchedulerBot>();
var logger = servicesProvider.GetRequiredService<ILogger<DIJobFactory>>();
schedBor.StartScheduler();
schedBor.Scheduler.JobFactory = new DIJobFactory(logger, servicesProvider);
DIJobFactory.cs
public class DIJobFactory : IJobFactory
{
static ILogger<DIJobFactory> _logger;
static IServiceProvider _serviceProvider;
public DIJobFactory(ILogger<DIJobFactory> logger, IServiceProvider sp)
{
_logger = logger;
_serviceProvider = sp;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
IJobDetail jobDetail = bundle.JobDetail;
Type jobType = jobDetail.JobType;
try
{
_logger.LogDebug($"Producing instance of Job '{jobDetail.Key}', class={jobType.FullName}");
if (jobType == null)
{
throw new ArgumentNullException(nameof(jobType), "Cannot instantiate null");
}
return (IJob)_serviceProvider.GetRequiredService(jobType);
}
catch (Exception e)
{
SchedulerException se = new SchedulerException($"Problem instantiating class '{jobDetail.JobType.FullName}'", e);
throw se;
}
}
// get from https://github.com/quartznet/quartznet/blob/139aafa23728892b0a5ebf845ce28c3bfdb0bfe8/src/Quartz/Simpl/SimpleJobFactory.cs
public void ReturnJob(IJob job)
{
var disposable = job as IDisposable;
disposable?.Dispose();
}
}
ReminderJob.cs
public interface IReminderJob : IJob
{
}
public class ReminderJob : IReminderJob
{
ILogger<ReminderJob> _logger;
IBot _bot;
public ReminderJob(ILogger<ReminderJob> logger, IBot bot)
{
_logger = logger;
_bot = bot;
}
public async Task Execute(IJobExecutionContext context)
{
var parameters = context.JobDetail.JobDataMap;
var userId = parameters.GetLongValue(ReminderJobConst.ChatId);
var homeWorkId = parameters.GetLongValue(ReminderJobConst.HomeWordId);
await _bot.Send(userId.ToString(), "test");
}
}
Related
I am using .net core project .
I implemented DI like :
builder.Services.AddSingleton<IMessageSender>(x => new
MessageSender(Environment.GetEnvironmentVariable("ServiceBusConnection"),"queueName"));
var serviceprovider = builder.Services.BuildServiceProvider();
ServiceBusUtils.Configure(serviceprovider.GetService<IMessageSender>());
Also, I have a Utility class :
public static class ServiceBusUtils
{
private static IMessageSender _messageSender;
public static void Configure(IMessageSender messageSender)
{
_messageSender = messageSender;
}
public static async Task<bool> SendMessage(ExecuteMessage<Message> message ,string queueName,string Id)
{
var message = new Message(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(Message)))
{
SessionId = Id,
};
await _messageSender.SendAsync(message);
return true;
}
}
But I cannot set the queuename with the parameter.
I want to use implementation like
ServiceBusUtils.SendMessage(message,"quename");
You have to register named clients for using with multiple queue names. The following code will give you an idea.
public void ConfigureServices(IServiceCollection services)
{
var queueNames = new string[] { "q1", "q2" };
var queueFactory = new Dictionary<string, IMessageSender>();
foreach (var queueName in queueNames)
{
queueFactory.Add(queueName, new MessageSender(Environment.GetEnvironmentVariable("ServiceBusConnection"),queueName));
}
services.AddSingleton(queueFactory);
services.AddSingleton<IServiceBus,ServiceBus>();
}
public interface IServiceBus
{
public Task SendMessage(string message, string queue);
}
public class ServiceBus : IServiceBus
{
private readonly Dictionary<string, IMessageSender> _queueFactory;
public ServiceBus(Dictionary<string, IMessageSender> queueFactory)
{
_queueFactory = queueFactory;
}
public async Task SendMessage(string message, string queue)
{
var messageSender = _queueFactory[queue];
await messageSender.SendAysnc(message);
}
}
You can pass the queue name to your send message method. This will resolve your MessageSender instance for the named queue.
_serviceBus.SendMessage("message","q1");
I have an odata query builder class that I am using to build my odata string that is desterilising the result based on the object that called it.
public class UosOdataQueryBuilder<T>
{
private readonly Dictionary<string, string> _queryOptions;
private readonly IHttpClientFactory _clientFactory;
private readonly ILogger _logger;
public UosOdataQueryBuilder([FromServices] IHttpClientFactory clientFactory, [FromServices] ILogger logger)
{
_queryOptions = new Dictionary<string, string>();
_clientFactory = clientFactory;
_logger = logger;
}
public UosOdataQueryBuilder<T> WithFilter(string filter)
{
_queryOptions.Add("$filter", filter);
return this;
}
public UosOdataQueryBuilder<T> Skip(int skip)
{
_queryOptions.Add("$skip", skip.ToString());
return this;
}
public UosOdataQueryBuilder<T> Top(int top)
{
_queryOptions.Add("$top", top.ToString());
return this;
}
public UosOdataQueryBuilder<T> WithNoInlineCount()
{
_queryOptions.Add("$inlinecount", "none");
return this;
}
public UosOdataQueryBuilder<T> OrderBy(string orderBy)
{
_queryOptions.Add("$orderby", orderBy);
return this;
}
public async Task<UosOdataReponse<T>> ExecuteQueryAsync(string elementName = "")
{
var result = new UosOdataReponse<T>();
try
{
var authToken = AppSettings.PlatformBearerToken;
var queryParameters = new List<string>();
foreach (var option in _queryOptions)
queryParameters.Add($"{option.Key}={option.Value}");
var queryParametersCombined = string.Join("&", queryParameters);
var oDataElementName = (elementName == "") ? typeof(T).Name : elementName;
var baseUrl = AppSettings.PlatformBaseUri;
var client = _clientFactory.CreateClient("UOS");
var request = new HttpRequestMessage(
HttpMethod.Get,
new Uri(baseUrl + $"/uos/v4/odata/{oDataElementName}" + queryParametersCombined));
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<UosOdataReponse<T>>(data);
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message);
}
return result;
}
}
I have setup the client in startup
services.AddHttpClient("UOS", c =>
{
c.BaseAddress = new Uri(Configuration.GetValue<string>("PlatformBaseUri") + "uos/v4/");
c.DefaultRequestHeaders.Add("Accept", "application/json");
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Configuration.GetValue<string>("PlatformBearerToken"));
//c.Timeout = new TimeSpan(0, 0, 30);
});
When I create a new instance of this from another method it is requiring that I pass in the clientFactory and logger.
protected async Task<int> GetUosOdataCount(string filter)
{
var result = new List<T>();
try
{
var countCheck = await new UosOdataQueryBuilder<T>()
.WithFilter(filter)
.Top(1)
.ExecuteQueryAsync();
return countCheck.Count;
}
catch (Exception ex)
{
//CustomLogger.LogError(GetType().FullName, "GetUosOdata", ex.Message);
}
}
In .NET Framework I would remove the parameters from the constructor of the UosOdataQueryBuilder and resolve the dependencies within it. For Example:
_uosUserAttributeRepository = GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IUosUserAttributeRepository)) as IUosUserAttributeRepository;
But I am not sure how to achieve in .NET Core. Any suggestions?
You can create an interface for UosOdataQueryBuilder<T> and register it into DI generically. some thing like this:
public interface IUosOdataQueryBuilder<T>
{
Task<T> SomeMethod();
}
public class UosOdataQueryBuilder<T> : IUosOdataQueryBuilder<T>
{
private readonly Dictionary<string, string> _queryOptions;
private readonly IHttpClientFactory _clientFactory;
private readonly ILogger<UosOdataQueryBuilder<T>> _logger;
public UosOdataQueryBuilder(IHttpClientFactory clientFactory, ILogger<UosOdataQueryBuilder<T>> logger)
{
_queryOptions = new Dictionary<string, string>();
_clientFactory = clientFactory;
_logger = logger;
}
public Task<T> SomeMethod()
{
return default;
}
}
And in ConfigureServices in startup write this:
services.AddScoped(typeof(IUosOdataQueryBuilder<>), typeof(UosOdataQueryBuilder<>));
And in your controller inject the IUosOdataQueryBuilder:
private readonly IUosOdataQueryBuilder<YourClass> _uosOdataQueryBuilder;
public YourController( IUosOdataQueryBuilder<YourClass> uosOdataQueryBuilder)
{
_uosOdataQueryBuilder = uosOdataQueryBuilder;
}
you can register IUosOdataQueryBuilder as Singleton but for prevent memory leak you should inject IServiceScopeFactory in concrete class to get registered service in your methods not in constructor.
I'd agree with comment to stick with injection but you can retrieve the dependencies within the class using IServiceProvider
e.g
MyMethod(IServiceProvider serviceProvider)
{
MyService svc = (MyService)serviceProvider.GetService(typeof(MyService));
...
Goal:
Fundamentally I am trying to add a background job, that has dependencies injected, to a console application.
Problem:
Although the jobs are queued, they are never executed.
Program.cs
var appSettings = ConfigHelper.GetConfig();
Console.WriteLine("Initialising Hangfire Server...");
GlobalConfiguration.Configuration.UseSqlServerStorage(appSettings.ConnectionString);
using (var server = new BackgroundJobServer())
{
Console.WriteLine("Hangfire Server started.");
var t = serviceProvider.GetService<ITestService>();
t.Test();
Console.ReadKey();
}
ServiceProviderFactory.cs
public static void Setup()
{
IServiceCollection services = new ServiceCollection();
...
services.AddDbContext<Db>(x => x.UseSqlServer(appSettings.ConnectionString));
services.AddTransient<IInsertLogJob, InsertLogJob>();
services.AddTransient<ITestService, TestService>();
_serviceProvider = services.BuildServiceProvider();
}
TestService.cs
public interface ITestService
{
void Test();
}
public class TestService : ITestService
{
private readonly ILogger<TestService> _logger;
public TestService(ILogger<TestService> logger)
{
_logger = logger;
}
public void Test()
{
logger.LogInformation("Test");
_logger.LogError("Error");
}
}
Logger.cs
public class Logger : ILogger
{
...
Log log = new Log()
{
Message = message,
EventId = eventId.Id,
ObjectId = eventId.Name,
LogLevel = logLevel.ToString(),
CreatedTime = DateTime.Now
};
BackgroundJob.Enqueue<IInsertLogJob>(j => j.Insert(log));
}
InsertLogJob.cs
public interface IInsertLogJob
{
void Insert(Log log);
}
public class InsertLogJob : IInsertLogJob
{
private Db _dataContext;
public InsertLogJob(Db context)
{
_dataContext = context;
}
public void Insert(Log log)//<-- this never happens
{
_dataContext.Logs.Add(log);
_dataContext.SaveChanges();
}
}
DB Record
So all the code up to the point where the data has to be inserted into the database runs, the Hangfire job gets inserted as per the picture above, but the code is never executed.
I have been reading a lot for DI and Quartz.net using ninject and I can't make it run. I'm trying to follow this link. This is my code:
This is the job:
public class ClientsImportJob : IJob
{
private readonly IUserClientImportService _userClientImportService;
public ClientsImportJob(IUserClientImportService userClientImportService)
{
_userClientImportService = userClientImportService;
}
public void Execute(IJobExecutionContext context)
{
_userClientImportService.ProcessFiles();
}
}
This is the Factory:
public class NInjectJobFactory : SimpleJobFactory
{
readonly IKernel _kernel;
public NInjectJobFactory(IKernel kernel)
{
this._kernel = kernel;
}
public override IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
try
{
return (IJob) this._kernel.Get(bundle.JobDetail.JobType); // will inject dependencies that the job requires
}
catch (Exception e)
{
throw new SchedulerException(string.Format("Problem while instantiating job '{0}' from the NinjectJobFactory.", bundle.JobDetail.Key), e);
}
}
}
And this is where I call the scheduller:
public class QuartzService
{
public static void ScheduleImportTask()
{
var kernel = InitializeNinjectKernel();
var scheduler = kernel.Get<IScheduler>();
scheduler.ScheduleJob(
JobBuilder.Create<ClientsImportJob>().Build(),
TriggerBuilder.Create().WithSimpleSchedule(s => s.WithIntervalInSeconds(10).RepeatForever()).Build());
scheduler.Start();
}
private static IKernel InitializeNinjectKernel()
{
var kernel = new StandardKernel();
kernel.Bind<IScheduler>().ToMethod(x =>
{
var sched = new StdSchedulerFactory().GetScheduler();
sched.JobFactory = new NInjectJobFactory(kernel);
return sched;
});
kernel.Bind<IUserClientImportService>().To<UserClientImportService>();
return kernel;
}
}
What am I doing wrong?
Currently I am writing a service using Quartz.NET to schedule the running of it.
I was wondering if anyone has any experience of using constructor injection with Quartz.NET and Simple Injector.
Below is essentially what I wish to achieve
public class JobImplementation: IJob
{
private readonly IInjectedClass injectedClass;
public JobImplementation(IInjectedClass _injectedClass)
{
injectedClass = _injectedClass
}
public void Execute(IJobExecutionContext _context)
{
//Job code
}
According to this blog post, you would need to implement a custom IJobFactory, like this:
public class SimpleInjectorJobFactory : IJobFactory
{
private readonly Container container;
private readonly Dictionary<Type, InstanceProducer> jobProducers;
public SimpleInjectorJobFactory(
Container container, params Assembly[] assemblies)
{
this.container = container;
// By creating producers, jobs can be decorated.
var transient = Lifestyle.Transient;
this.jobProducers =
container.GetTypesToRegister(typeof(IJob), assemblies).ToDictionary(
type => type,
type => transient.CreateProducer(typeof(IJob), type, container));
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler _)
{
var jobProducer = this.jobProducers[bundle.JobDetail.JobType];
return new AsyncScopedJobDecorator(
this.container, () => (IJob)jobProducer.GetInstance());
}
public void ReturnJob(IJob job)
{
// This will be handled automatically by Simple Injector
}
private sealed class AsyncScopedJobDecorator : IJob
{
private readonly Container container;
private readonly Func<IJob> decorateeFactory;
public AsyncScopedJobDecorator(
Container container, Func<IJob> decorateeFactory)
{
this.container = container;
this.decorateeFactory = decorateeFactory;
}
public async Task Execute(IJobExecutionContext context)
{
using (AsyncScopedLifestyle.BeginScope(this.container))
{
var job = this.decorateeFactory();
await job.Execute(context);
}
}
}
}
Furthermore, you'll need the following registrations:
var container = new Container();
container.Options.ScopedLifestyle = new AsyncScopedLifestyle();
var factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
scheduler.JobFactory = new SimpleInjectorJobFactory(
container,
Assembly.GetExecutingAssembly()); // assemblies that contain jobs
// Optional: register some decorators
container.RegisterDecorator(typeof(IJob), typeof(LoggingJobDecorator));
container.Verify();
Late to the party, but https://github.com/hbiarge/Quartz.Unity works well for combining Quartz.NET and Unity.
IUnityContainer container = new UnityContainer();
container.AddNewExtension<Quartz.Unity.QuartzUnityExtension>();
// do your other Unity registrations
IScheduler scheduler = container.Resolve<IScheduler>();
scheduler.ScheduleJob(
new JobDetailImpl(myCommandName, typeof(MyCommand)),
TriggerBuilder.Create()
.WithCronSchedule(myCronSchedule)
.StartAt(startTime)
.Build()
);
scheduler.Start();
There are few steps to use Quartz.net with dependency injection engine from asp.net core.
Add nuget package to your project:
Microsoft.Extensions.DependencyInjection
Create custom JobFactory:
public class JobFactory : IJobFactory
{
protected readonly IServiceProvider _serviceProvider;
public JobFactory(IServiceProvider serviceProvider)
=> _serviceProvider = serviceProvider;
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
=> _serviceProvider.GetService(bundle.JobDetail.JobType) as IJob;
public void ReturnJob(IJob job)
=> (job as IDisposable)?.Dispose();
}
Specify JobFactory when configuring scheduler:
var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
scheduler.JobFactory = new JobFactory(_serviceProvider);
For someone can be usefull example of win service with Quartz.net and DI (from asp.net core) on the board:
public class WinService : ServiceBase
{
private Scheduler _scheduleManager;
private readonly Startup _startup;
public WinService()
{
ServiceName = "SomeWinService";
_startup = new Startup();
}
static void Main(string[] args)
{
var service = new WinService();
// Working as Windows-service
if (Console.IsInputRedirected && Console.IsOutputRedirected)
{
ServiceBase.Run(service);
}
// Working as console app
else
{
service.OnStart(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey();
service.OnStop();
}
}
protected override void OnStart(string[] args)
{
_startup.RegisterServices();
_scheduleManager = new Scheduler(_startup.ServiceProvider);
_scheduleManager.StartTracking().Wait();
}
protected override void OnPause()
=> _scheduleManager.PauseTracking().Wait();
protected override void OnContinue()
=> _scheduleManager.ResumeTracking().Wait();
protected override void OnStop()
{
_scheduleManager.StopTracking().Wait();
_startup.DisposeServices();
}
}
public class Startup
{
private IServiceProvider _serviceProvider;
public IServiceProvider ServiceProvider => _serviceProvider;
public void RegisterServices()
{
_serviceProvider = new ServiceCollection()
//.AddTransient(...)
//.AddScoped(...)
//.AddSingleton(...)
.BuildServiceProvider();
}
public void DisposeServices()
{
if (_serviceProvider == null)
return;
if (_serviceProvider is IDisposable)
{
((IDisposable)_serviceProvider).Dispose();
}
}
}
public class Scheduler
{
private readonly IServiceProvider _serviceProvider;
private IScheduler _scheduler;
public Scheduler(IServiceProvider serviceProvider)
=> _serviceProvider = serviceProvider;
public async Task StartTracking()
{
_scheduler = await StdSchedulerFactory.GetDefaultScheduler();
_scheduler.JobFactory = new JobFactory(_serviceProvider);
await _scheduler.Start();
// Schedule your jobs here
}
public async Task PauseTracking() => await _scheduler?.PauseAll();
public async Task ResumeTracking() => await _scheduler?.ResumeAll();
public async Task StopTracking() => await _scheduler?.Shutdown();
}