我想在我的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);
            }
        }
    }


我为此创建了一个简单的接口和类:

public interface ITestA
    {
        void Hello();
    }

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


所有这些,我不知道为什么我的应用程序崩溃,请帮我解决这个问题。
非常感谢。

最佳答案

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

简而言之-根据设计,您不能有多个类在一个辅助角色中继承RoleEntryPoint。

关于c# - Ninject与Azure Worker角色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34465811/

10-14 18:09
查看更多