Using CefSharp with Owin TestServer

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 01:41:26

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/34624015/using-cefsharp-with-owin-testserver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!