Azure SDK 2.5(RoleEntryPoint)WorkerRole的问题

时间:2014-12-19 13:26:37

标签: c# azure

我最近将我的项目从Azure sdk 2.3升级到sdk 2.5,我遇到了一个非常令人沮丧的问题。 我正在使用以下代码:

ThreadedRoleEntryPoint1.cs

public abstract class ThreadedRoleEntryPoint1 : RoleEntryPoint
{
    protected ThreadedRoleEntryPoint1()
    {
        Threads = new List<Thread>();
        EventWaitHandle = new EventWaitHandle(false, EventResetMode.ManualReset);
    }

    private List<Thread> Threads;
    private WorkerEntryPoint1[] _workers;
    protected EventWaitHandle EventWaitHandle;

    public WorkerEntryPoint1[] workers { get; set; }

    public override void Run()
    {
        foreach (WorkerEntryPoint1 worker in _workers) { Threads.Add(new Thread(worker.ProtectedRun)); }

        foreach (Thread thread in Threads) { thread.Start(); }

        while (!EventWaitHandle.WaitOne(0))
        {
            for (Int32 i = 0; i < Threads.Count; i++)
            {
                if (!Threads[i].IsAlive)
                {
                    Threads[i] = new Thread(_workers[i].Run);
                    Threads[i].Start();
                }
            }

            EventWaitHandle.WaitOne(1000);
        }
    }

    //public override bool OnStart(WorkerEntryPoint1[] workers)
    public override bool OnStart()
    {
        this._workers = workers;

        foreach (WorkerEntryPoint1 worker in workers) { worker.OnStart(); }

        return base.OnStart();
    }

    //public override bool OnStart()
    //{
    //    throw (new InvalidOperationException());
    //}

    public override void OnStop()
    {
        EventWaitHandle.Set();

        foreach (Thread thread in Threads) { while (thread.IsAlive) { thread.Abort(); } }

        foreach (Thread thread in Threads) { while (thread.IsAlive) { Thread.Sleep(10); } }

        foreach (WorkerEntryPoint1 worker in _workers) { worker.OnStop(); }

        base.OnStop();
    }
}

WorkerEntryPoint1.cs

public abstract class WorkerEntryPoint1
{
    public virtual bool OnStart()
    {
        return (true);
    }

    public virtual void Run()
    {

    }

    public virtual void OnStop()
    {

    }

    internal void ProtectedRun()
    {
        try
        {
            Run();
        }

        catch (SystemException)
        {
            throw;
        }
        catch (Exception)
        {

        }
    }
}

WorkerRole.cs

internal class GlobalWorkerRole : ThreadedRoleEntryPoint1
{
    public GlobalWorkerRole()
    {
        _maxConnections = 10
    }

    private readonly int _maxConnections;

    public override void Run()
    {
        try
        {
            base.Run();
        }

        catch (Exception error)
        {
            Trace.WriteLine(error.ToString(), "EXCEPTION");
            throw error;
        }
    }

    public override bool OnStart()
    {
        ServicePointManager.DefaultConnectionLimit = _maxConnections;

        List<WorkerEntryPoint1> workers = new List<WorkerEntryPoint1>
        {
            new ParseFeedMultiThreadWorker(),
            new CheckFeedStateWorker(),
            new ParseNewProductsWorker(),
            new UpdateProductInformationWorker(),
            new CleanTablesWorker(),
            new RebuildTablesWorker()
        };

        this.workers = workers.ToArray();

        return base.OnStart();
    }
}

从sdk 2.5开始,我在Azure模拟器中收到以下错误:

[runtime]无法创建角色入口点:System.MissingMethodException:无法创建抽象类

我的猜测是,这与ThreadedRoleEntryPoint1从RoleEntryPoint继承的事实有关,而RoleEntryPoint调用抽象类的OnStart而不是GlobalWorkerRole。

任何了解并可以帮助我解决此问题的人,谢谢!

信任是一个弱点......

1 个答案:

答案 0 :(得分:2)

问题与主机如何搜索入口点有关。它似乎找到了继承自RoleEntryPoint的第一个类,然后实例化该类而不是寻找非抽象类。

解决方法是将抽象类移动到不同的程序集中,并将要使用的类保留在辅助角色项目中。