1

This is an asp.net webform website we are developing. In one xx.aspx.cs file, it gets web response from Google server.

var content = new FormUrlEncodedContent(recaptchaDetails);
var response = client.PostAsync("https://www.google.com/recaptcha/api/siteverify", content).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
var obj = JsonConvert.DeserializeObject<RecaptchaResponse>(responseString);

Each time I debug this page in my local machine, Visual Studio fires up the IIS Express to run the server code. While IIS Express process the page, it takes very long time to load the response from Google in my local machine. It is not convenient for debugging.

Here is my thought on resolving this.

  • Change the code while debugging in my local machine. Change it to a mock returned object. But every time I commit the code, I have to roll back this block. There is risk that it may be commited to git server.

    var response = new Xxx()

  • Use Fiddler to intercept this request to Google and return a file from disk.But each time before debugging, I have to open the fiddler.

Is there any Visual Studio plugin or IIS Express plugin to intercept the request to Google and directly return a response from disk file?

Robin Sun
  • 1,352
  • 3
  • 20
  • 45
  • 1
    IMHO move this implementation to a service. And provide a different service implementation in debug builds. – Jeremy Lakeman Oct 13 '21 at 01:49
  • This is a very very old asp.net webform website. How does it deploy to server? No compile, no build. It just directly copy all the files in Git to IIS in windows server. I am wonder whether debug build works in this situation? – Robin Sun Oct 13 '21 at 01:55
  • WARNING getting the `.Result` of an Async call may [result in a deadlock](https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks) – Hans Kesting Oct 13 '21 at 07:32
  • This can be done using `#if DEBUG`, see here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives#conditional-compilation And then make sure to always do a Release build before deploying to the web server. – Peter B Oct 13 '21 at 23:54
  • @PeterB It is a very old asp.net webform project. The asp.net page will be not build, it will be directly copied to IIS. Will `#if DEBUG` work this way? – Robin Sun Oct 14 '21 at 23:32
  • I have tried, and in a simple page it works fine. But it may also depend on what is in your web.config. See e.g. https://stackoverflow.com/a/1839564 – Peter B Oct 15 '21 at 08:25
  • @PeterB. Yes, it works. You can move your answer to Answer part, then I can mark it as anwsered? – Robin Sun Oct 17 '21 at 02:38

1 Answers1

0

This can be done using #if DEBUG, which is called a Conditional Compilation directive.

It is generally best to do a Release build in VS (this creates a DLL with optimized code for all pages), and then deploy the contents of the bin\Release folder to the web server. If you use Web Project -> right click -> Publish... then that should happen automatically.

If you do a manual copy of all source files to IIS, then on IIS the files might or might not run in "DEBUG" mode. I have tried, and in a simple page the Conditional Compilation directive worked fine, with DEBUG not active.

What happens in your case may depend on what is in your web.config, as discussed in this answer but I have not personally tried that.

Peter B
  • 22,460
  • 5
  • 32
  • 69