有没有更新的方法来完成本文的建议?

时间:2014-11-13 16:06:17

标签: c# .net xml c#-5.0

我喜欢这个代理服务的想法,它允许我在不影响现有代码的情况下添加作业,但文章很老。自2005年以来有什么东西可以用来完成同样的事情吗?

我仅限于.Net,但我可以使用任何版本的框架。

http://visualstudiomagazine.com/articles/2005/05/01/create-a-net-agent.aspx

编辑:以下是我正在尝试做的总结

拥有一个.Net服务,可以读取配置文件以动态加载DLL来执行任务。

该服务有一个动态对象加载器,它将XML映射到DLL中的类以执行任务。

下面是xml文件及其映射的类的示例。基本上,服务可以从xml读取作业,实例化CustomerAttendeeCreateNotificationWorker的实例并调用Run()方法。作为开发人员,我可以在Run()方法中创建任意数量的这些类并实现我想要的任何内容。另请注意,“CustomerAttendanceNotificationTemplateName”条目将分配给实例化对象上具有相同名称的属性。

<?xml version="1.0" encoding="utf-8"?>
<Manager type="Task.RemotingManager, TaskClasses">
    <Jobs type="Task.TaskJob, TaskBase" Name="CustomerAttendeeCreateNotificationWorker Worker">
        <Worker type="TaskWorkers.CustomerAttendeeCreateNotificationWorker, TaskWorkers"
            Description="Inserts records into NotificationQueue for CustomerAttendees"
                CustomerAttendanceNotificationTemplateName ="CustomerAttendanceNotificationTemplateName"
            />
        <Schedulers type="Task.FixedIntervalScheduler, TaskClasses"
            DaysOfWeek="Everyday"
            Frequency="00:05:00"
            StartTime="00:00:00"
            EndTime="23:55:00"
            />
    </Jobs>
    <Jobs type="Task.TaskJob, TaskBase" Name="SendNotificationWorker Worker">
        <Worker type="TaskWorkers.SendNotificationWorker, TaskWorkers"
            Description="Sends messages from NotificationQueue"
            />
        <Schedulers type="Task.FixedIntervalScheduler, TaskClasses"
            DaysOfWeek="Everyday"
            Frequency="00:05:00"
            StartTime="00:00:00"
            EndTime="23:55:00"
            />
    </Jobs>/>
</Manager>
/// <summary>
///     The customer attendee create notification worker.
/// </summary>
[Serializable]
public class CustomerAttendeeCreateNotificationWorker : TaskWorker
{
    #region Constants

    /// <summary>
    ///     The stat e_ critical.
    /// </summary>
    private const int StateCritical = 1;

    /// <summary>
    ///     The stat e_ ok.
    /// </summary>
    private const int StateOk = 0;

    #endregion

    #region Fields

    /// <summary>
    ///     The customer notification setting name.
    /// </summary>
    private string customerAttendanceNotificationTemplateName;

    #endregion

    #region Public Properties

    /// <summary>
    ///     The customer notification setting name.
    /// </summary>
    public string CustomerAttendanceNotificationTemplateName
    {
        get
        {
            return this.customerAttendanceNotificationTemplateName;
        }

        set
        {
            this.customerAttendanceNotificationTemplateName = value;
        }
    }

    /// <summary>
    /// Gets or sets the logger.
    /// </summary>
    public ILogger Logger { get; set; }

    #endregion

    #region Public Methods and Operators

    /// <summary>
    ///     The run.
    /// </summary>
    /// <returns>
    ///     The <see cref="TaskResult" />.
    /// </returns>
    public override TaskResult Run()
    {
        StringBuilder errorStringBuilder = new StringBuilder();
        string errorLineTemplate = "Time: {0} Database: {1} Error Message: {2}" + Environment.NewLine;
        bool isError = false;
        IUnitOfWork configUnitOfWork = new GenericUnitOfWork<DCCShowConfigContext>();
        TradeshowService tradeshowService = new TradeshowService(configUnitOfWork);

        List<Tradeshow> tradeshows = tradeshowService.GetAll().Where(t => t.Status == "OPEN").ToList();
        string connectionStringTemplate = ConfigurationManager.ConnectionStrings["DCCTradeshow"].ConnectionString;
        int customerNotificationSettingGroupId = Convert.ToInt32(ConfigurationManager.AppSettings["NotificationSettingGroupId"]);

        foreach (Tradeshow tradeshow in tradeshows)
        {
            try
            {
                string connectionString = string.Format(connectionStringTemplate, tradeshow.DbName);
                IUnitOfWork unitOfWork = new TradeshowUnitOfWork(connectionString);

                NotificationService notificationService = new NotificationService(unitOfWork);
                notificationService.InsertCustomerAttendanceNotifications(tradeshow.TradeshowId, customerNotificationSettingGroupId, this.customerAttendanceNotificationTemplateName);
            }
            catch (Exception exception)
            {
                isError = true;
                errorStringBuilder.AppendLine(string.Format(errorLineTemplate, DateTime.Now, tradeshow.DbName, exception.Message));
            }
        }

        TaskResult result;
        if (isError)
        {
            result = new TaskResult(StateOk, TaskResultStatus.Exception, "Errors Occured", errorStringBuilder.ToString());
        }
        else
        {
            result = new TaskResult(StateOk, TaskResultStatus.Ok,"All Good", "All Good");
        }

        return result;
    }

    #endregion
}`

0 个答案:

没有答案