質問

I have a code that I want to run in global.asax in ASP.NET. I want this code to run only on localhost and when the code on an EC2 instance, I want it to run another code (the second code should run when I deploy my project on Amazon Web Service EC2 server), how can I do that without using the DEBUG functionality?

役に立ちましたか?

解決

To check for whether or not the request is from the local machine, do this:

bool isLocal = HttpContext.Current.Request.IsLocal;

if(isLocal)
{
    // Do things that should only be done when request is local here

}

Note: Read HttpRequest.IsLocal documentation for more information.

他のヒント

I guess uyou can pick few environment variables frm these: Environment Variables (type ENV) EC2_AMI_ID EC2_AKI_ID EC2_ARI_ID EC2_AMI_MANIFEST_PATH EC2_PLACEMENT_AVAILABILITY_ZONE EC2_HOSTNAME EC2_INSTANCE_ID EC2_INSTANCE_TYPE EC2_LOCAL_HOSTNAME EC2_PUBLIC_HOSTNAME EC2_PUBLIC_IPV4 EC2_RESERVATION_ID EC2_SECURITY_GROUPS RS_EIP RS_SERVER RS_SKETCHY RS_SYSLOG RS_TOKEN RS_SERVER_NAME List taken from here: https://support.rightscale.com/09-Clouds/AWS/FAQs/FAQ_0013_-_What_EC2_environment_variables_are_available_in_RightScripts%3F

I think, that checking for availability of EC2_AMI_ID and EC2_INSTANCE_ID would be enough to answer your questions.

If you are using ASP.NET dev server (VS 2012 and earlier) use this method:

public static bool IsDebugWebServer()
{
  if (HttpContext.Current != null && HttpContext.Current.Request != null)
  {
    return HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"] == null || HttpContext.Current.Request.ServerVariables["SERVER_SOFTWARE"] == string.Empty;
  }
else
  {
    return false;
  }
}

And if you are using local IIS Express use this:

public static bool IsDebugWebServer()
{
  return String.Compare(Process.GetCurrentProcess().ProcessName, "iisexpress") == 0;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top