简体   繁体   English

将CefSharp与Owin TestServer一起使用

[英]Using CefSharp with Owin TestServer

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). 由于潜在的防火墙限制以及需要使我的应用程序的许多实例无法竞争端口的缘故,我想使用Microsoft.Owin.Testing.TestServer类创建一个内存中的Web服务器,该服务器将向服务器提供请求嵌入式CefSharp浏览器(WPF或Winforms都可以)。

I can create the TestServer with no issues. 我可以毫无问题地创建TestServer。 How can I configure the CefSharp WebBrowser control to use that Test Server rather than using the standard OS network stack? 如何配置CefSharp WebBrowser控件使用该测试服务器,而不使用标准OS网络堆栈?

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. 如果我要形成自己的请求,则可以使用TestServer提供的HttpClient,也可以使用TestServer的处理程序创建新的HttpClient。 Is there any equivalent functionality in CefSharp WebBrowser? 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 ) 如果要实现IResourceHandlerFactory随后IResourceHandler (只是返回您的自定义的新实例IResourceHandlerIResourceHandlerFactory.GetResourceHandler

https://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp/IResourceHandlerFactory.cs#L22 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. 您的自定义ResourceHandler新实例将针对每个请求实例化。 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 . async方式处理请求相对简单,只需在ProcessRequestAsync调用您的请求,当它完成时就用数据execute callback.Continue()填充本地流,返回该流并在GetResponse填充响应。 I like to wrap callback in a using statement as it wraps a managed resource and should be explicitly disposed of. 我喜欢将callback包装在using语句中,因为它包装了托管资源,应明确处理。

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://github.com/cefsharp/CefSharp/blob/cefsharp/45/CefSharp.Example/CefSharpSchemeHandler.cs#L92中看到一个基本示例(不发出网络请求,但原理相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM