Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
425 views
in Technique[技术] by (71.8m points)

c# - AutoStart a WCF on Azure WebRole

I have a WCF hosted on Azure (WebRole). That WCF does a lot of background tasks and replies to some petitions.

The problem is that if the WCF doesn't receive any petition for a long time (10 hours or more) the application pool is recycled on the azure instance and the WCF tasks stops. I did a little investigation an I can enable a AutoStart feature touching the machine.config, but this is not an option with a azure deploy.

Can I enable AutoStart within web.config or deploy config files?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can add some code in the WebRole.cs to modify the application pool:

public class WebRole : RoleEntryPoint
{
    public override void Run()
    {
        using (var serverManager = new ServerManager())
        {
            var mainSite = serverManager.Sites[RoleEnvironment.CurrentRoleInstance.Id + "_Web"];
            var mainApplication = mainSite.Applications["/"];
            var mainApplicationPool = serverManager.ApplicationPools[mainApplication.ApplicationPoolName];
            mainApplicationPool["autoStart"] = true;
            mainApplicationPool["startMode"] = "AlwaysRunning";

            serverManager.CommitChanges();
        }

        base.Run();
    }

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

        return base.OnStart();
    }
}

Note: To use ServerManager you will need to:

  • reference C:Windowssystem32inetsrvMicrosoft.Web.Administration.dll (or available through NuGet)
  • add <Runtime executionContext="elevated" /> in your Service Definition under the WebRole element

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...