C# 具有Azure工作者角色的Ninject

C# 具有Azure工作者角色的Ninject,c#,azure,ninject,C#,Azure,Ninject,我想在我的WorkerRole应用程序中使用ninject依赖项注入器。 但我发现了一些问题。运行完我的工人角色后,他立即崩溃了,我不知道为什么会发生这种情况 我的WorkerRole.cs代码: public class WorkerRole : NinjectRoleEntryPoint { private readonly CancellationTokenSource _cancellationTokenSource = new CancellationToken

我想在我的WorkerRole应用程序中使用ninject依赖项注入器。 但我发现了一些问题。运行完我的工人角色后,他立即崩溃了,我不知道为什么会发生这种情况

我的WorkerRole.cs代码:

public class WorkerRole : NinjectRoleEntryPoint
    {
        private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
        private readonly ManualResetEvent _runCompleteEvent = new ManualResetEvent(false);

        private IKernel _kernel;
        public ITestA TestA { get; }
        protected WorkerRole(ITestA testA)
        {
            TestA = testA;
        }

        public override void Run()
        {
            Trace.TraceInformation("WorkerRole1 is running");

            try
            {
                RunAsync(_cancellationTokenSource.Token).Wait();
            }
            finally
            {
                _runCompleteEvent.Set();
            }
        }

        protected override IKernel CreateKernel()
        {
            _kernel = new StandardKernel();
            _kernel.Bind<ITestA>().To<TestA>();

            return _kernel;
        }

        private async Task RunAsync(CancellationToken cancellationToken)
        {
            // TODO: Replace the following with your own logic.
            while (!cancellationToken.IsCancellationRequested)
            {
                TestA.Hello();

                Trace.TraceInformation("Working");
                await Task.Delay(1000);
            }
        }
    }
这一切,我不知道为什么我的应用程序崩溃,请帮助我解决这个问题。
非常感谢。

您的问题最有可能发生,因为您将NinjectRoleEntryPoint和WorkerRole保留在同一个工作者角色项目中。您应该在Worker角色项目中只保留一个
RoleEntryPoint
实现,并且您的
NinjectRoleEntryPoint
应该移动到单独的类库项目中


简言之,按照设计,一个工作者角色中不能有多个继承RoleEntryPoint的类。

我不确定它是否允许您直接注入WorkerRole构造函数。构造函数在你创建内核之前就被调用了,所以你不太可能这么做。@raderick我想是的,但它不能解决我在运行后应用程序崩溃的问题。它甚至可以步进你的方法吗?恐怕您必须设置日志记录并尝试捕获您的异常,除了构造函数之外,代码对我来说或多或少都是干净的。@raderick hmmm,也许您知道我需要从哪里获得有关Ninject和WorkerRoles的手册。您可以试试这个-谷歌的第一行之一:。我还建议您一步一步地编写代码,以确定问题:1。创建空的web角色,运行2。从NinjectRoleEntryPoint继承,运行3。逐个重写方法,在某个时候您会发现问题。
public interface ITestA
    {
        void Hello();
    }

    public class TestA: ITestA
    {
        public void Hello()
        {
            Console.WriteLine("Ninject with Worker Role!");
        }
    }