如何通过自动缩放删除webrole时取消订阅订阅?

时间:2015-11-05 19:55:16

标签: c# azure azure-web-roles

我将Ninject用于我的应用程序。

public class Global : NinjectHttpApplication

在应用程序启动时,我致电:

protected override void OnApplicationStarted()
...
            if (RoleEnvironment.IsAvailable)
            {
                RoleEnvironment.Stopping += (sender, args) =>
                {
                    messagesListener.Stop(true);
                    Logger.LogInfo("Website is stopping. InstanceNo = " + instanceNo);
                };
        }

但由于某种原因,此事件Stopping未被调用。请帮我。我听说可能我需要使用RoleEntryPoint类的OnStop事件,我可以在我的类中继承,但我不知道该怎么做。我读了这篇文章:What's the difference between the webrole onStart() event and Application_Start() global.asax event?

1 个答案:

答案 0 :(得分:2)

您的Web项目中应该有一个名为WebRole.cs的类。默认情况下,此类将添加到基于云服务的所有基于Web的Azure项目中。

如果您在项目中没有看到该类,则只需添加它即可。在其中,您可以为Stopping事件配置事件处理程序

public class WebRole : RoleEntryPoint
{
    public override bool OnStart()
    {
        // For information on handling configuration changes
        // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.

        RoleEnvironment.Stopping += (sender, args) =>
            {
                messagesListener.Stop(true);
                Logger.LogInfo("Website is stopping. InstanceNo = " + instanceNo);
            };

        return base.OnStart();
    }

    public override void OnStop()
    {
        // you can also put stuff here to test
        base.OnStop();
    }
}