如何在我的石英配置中动态添加字段? [英] How to add a field in my quartz configuration dynamically?

查看:45
本文介绍了如何在我的石英配置中动态添加字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 web.config 中有一个quartz 配置部分,我想向其中添加一个键值字段.(我知道我可以去 web.config 并手动添加它,但这违背了目的)

I have a quartz configuration section in my web.config, and I want to add a key value field to it. (I know I can just go to the web.config and add it manually but that defeats the purpose)

我试过用这种方式

var config = (NameValueCollection)WebConfigurationManager.GetSection("quartz");
config.Add("quartz.dataSource.quartzDS.connectionString", "data source =..");

但它失败了,因为集合是只读的,不能修改.有关如何执行此操作的任何提示?

but it failed because collection is read only and can't be modified. Any tip to how to do this?

我最终将配置复制到 nameValueCollection,然后将其复制到另一个(对于只读属性)添加我想要的键值并将其传递给我需要的函数.

I ended up copying the config to a nameValueCollection and then copying it to another one (for the readonly properties) add the key values I want and passing it to the function I needed.

var oldConfig = (NameValueCollection)WebConfigurationManager.GetSection("quartz");
var config = Test(oldConfig);
var connectionString = unitOfWork.GetConnectionStringByTenantIdentifier(tenantId);
config.Add("quartz.dataSource.quartzDS.connectionString", connectionString);
await unitOfWork.GetService<SchedulerService>().StartScheduler(config, tenantId);

这样我就会按照我想要的方式为每个租户定制配置.对不起,如果我的问题不清楚.

this way I will have the custom configuration for every tenant the way I want it. Sorry if my question wasn't clear.

推荐答案

实际上您可以通过两种方式做到这一点.一种方法是在标准的 AppSettings 部分设置您的动态连接字符串,然后使用一组新的 XML 属性创建一个新的 Quartz 调度程序(Quartz.NET 发行版中提供了一个示例,因此我将缩短这个)

You can actually do this in two ways. One way is to set your dynamic connection string in the standard AppSettings section and then create a new Quartz Scheduler with a new set of XML properties (an example is provided in Quartz.NET distribution, so I will cut this short)

var properties = new NameValueCollection
    {
        ["quartz.scheduler.instanceName"] = "XmlConfiguredInstance",
        ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz", 
        ... etc.
    };
ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = await sf.GetScheduler();

然后您可以在 AppSettings 中保存您的非常量字符串并在那里获取它.

Then you can save your non-const string in the AppSettings and get it form there.

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    config.AppSettings.Settings.Add("quartz.dataSource.quartzDS.connectionString", connstring);
    config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

或者您可以将整个设置文件作为 XML 读取,如前所述,但是您必须确保在初始化默认 Quartz Scheduler 之前完成任何编辑,因为它的属性变为只读,并更改它们无论如何都必须创建一个新的 ISchedulerFactory,这有点超出了目的.

Or you can read your whole settings file as XML, as previously answered, BUT you have to make sure that any edits are done before you initialize the default Quartz Scheduler, as it's properties become read-only, and to change them you will have to create a new ISchedulerFactory anyway, which kinda beats the purpose.

var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
xmlDoc.SelectSingleNode("//quartz/add[@key='quartz.dataSource.quartzDS.connectionString']").Attributes["value"].Value = "...";
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("quartz");

但我建议您根本不要在运行时编辑主配置文件,而是使用 ISchedulerFactory XmlConfiguredInstance,同时以您喜欢的任何格式获取并保存连接字符串到与 UAC 兼容的位置(防止在运行时修改 app.config 引发异常)

But I advise you not to edit your main config file at runtime at all, and instead use an ISchedulerFactory XmlConfiguredInstance while getting and saving the connstring into a UAC-compatible location in any format you like (to prevent Modifying app.config at runtime throws exception from happening)

如果你想使用配置文件,你可以使用 本教程来自曾奕,供进一步阅读

Still if you want to use the config file you can use this tutorial from Yi Zeng for further reading

这篇关于如何在我的石英配置中动态添加字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆