Due to potential firewall limitations and the need to have many instances of my app that cannot be competing for ports, I would like to use the Microsoft.Owin.Testing.TestServer class to create an in-memory web server that will serve requests to an embedded CefSharp browser (either WPF or Winforms is fine).
I can create the TestServer with no issues. How can I configure the CefSharp WebBrowser control to use that Test Server rather than using the standard OS network stack?
If I was forming my own requests, I would either use the HttpClient provided by the TestServer or create a new HttpClient using the TestServer's handler. Is there any equivalent functionality in CefSharp WebBrowser?
You can intercept requests and fulfill them yourself. If you implement IResourceHandlerFactory
then subsequently IResourceHandler
(Just return a new instance of your custom IResourceHandler
in IResourceHandlerFactory.GetResourceHandler
)
https://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp/IResourceHandlerFactory.cs#L22
A new instance of your custom ResourceHandler
will be instantiated for every request. It's relatively simple to process the request in an async
fashion, just invoke your request in ProcessRequestAsync
, when it's complete fill a local stream with the data execute callback.Continue()
, return the stream and populate the response in GetResponse
. I like to wrap callback
in a using
statement as it wraps a managed resource and should be explicitly disposed of.
You can see a basic example at https://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp.Example/CefSharpSchemeHandler.cs#L92 (Doesn't make network requests, the principal is the same though)