The exception in question occurs when you call utilCreateResponseAndBypassServer
after having already sent the request to the server. If you want to use the utilCreateResponseAndBypassServer
method, you must do so inside the BeforeRequest
handler, not at some future time. Similarly, setting bBufferResponse
after having already connected to the server is pointless.
Based on your later remarks, you have a misunderstanding about how threading works with FiddlerCore. FiddlerCore processes Sessions on background threadpool threads.
When
BeforeRequest
fires, your code has a chance to run. If you callutilCreateResponseAndBypassServer
inside that method, then the response you generate is immediately returned to the client.If you don't call
utilCreateResponseAndBypassServer
insideBeforeRequest
, the request is immediately sent to the server by FiddlerCore, and the response is returned to the client when it's available.
To achieve what you're describing, you should NOT try to do your own threading-- instead do all of your work on the thread that goes into the BeforeRequest method and don't leave that method without generating the desired response. You don't need to worry about hanging the UI or anything like that, because BeforeRequest
is running on a background thread. The only thing you must do is the Invoke
methods if any of your code needs to interact with any UI owned by the UI thread.