简体   繁体   中英

Exclude method from executing while debugging on Local machine

I am using the following code to cleanup my Azure App's database.

protected void Application_End(object sender, EventArgs e)
{
     core.cleanUpDB();
}

Can I prevent this from being executed on my local machine while debugging? I just want to execute this only on the deployed Azure App.

Thanks in advance.

You can use HttpRequest.IsLocal to differntiate local and server request.

protected void Application_End(object sender, EventArgs e)
{
     if(!System.Web.HttpContext.Current.Request.IsLocal)
           core.cleanUpDB();
}

While the other answers might work in a specific scenario, they don't relate to Windows Azure. The only way to check if you're running in Windows Azure and not running in the emulator (assuming you have a Web Role), is like this:

protected void Application_End(object sender, EventArgs e)
{
    if (RoleEnvironment.IsAvailable && !RoleEnvironment.IsEmulated)
        core.cleanUpDB();
}

You use conditional compilation . The MSDN articles provides a much better explanation that I could write in the small space here.

in VB.net you would use something like this:

'#If DEBUG Then
'only do this while debugging...
'#End If

Please remove the ' mark - stack overflow useses the "#" mark as BOLD.... I think it should compile in C# as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM