问题
I have been having a murderous time with CEFSharp in WinForms since Visual Studio decided to update it to build 77.1.18 and Nuget messed the whole thing up leaving three versions installed but refusing to uninstall any of them! In order to resolve the issues I had to manually edit various solution and project files, and got it so that I have build 75 installed and mostly working.
The system then notified me that DefaultRequestHandler was now obsolete, and that I should use RequestHandler instead. That is fine, and required a few changes such as making the overrides protected instead of public. All done, except for one thing: OnResourceResponse. It insists that this no longer exists. I have searched everything I can find, and all references in the documentation and Google searches etc. refer to build 55 which is rather old. Unfortunately, for the particular purpose at hand I need to be able to trap any response that is not a 200 and pass the error information back to the view before closing it.
According to the docs I have been able to find, RequestHandler implements IRequestHandler which should contain this event handler but Intellisense does not offer me any implementation and if I ignore the Intellisense then obviously I cannot compile...
Here is the original code I was using, and need to replicate:
public override bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//NOTE: You cannot modify the response, only the request
// You can now access the headers
//var headers = response.Headers;
// If it is not an OK response, set the error and close the dialog
if (response.StatusCode != (int)HttpStatusCode.OK)
{
var error = $"{response.StatusCode}: {response.StatusText}";
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
var parent = (MyBrowserForm)chromiumWebBrowser.Parent.Parent.Parent;
parent.Errors = error;
browser.CloseBrowser(true);
return true;
}
return false;
}
Please can anyone tell me how to get this to work as it did before the update?
EDIT:
Subject to final testing because I am still having other (different) nuget issues, the answer was sort of as suggested in the accepted answer. In case anyone else needs it, here is what I had to do:
public class MyBrowserResourceRequestHandler : ResourceRequestHandler
{
protected override bool OnResourceResponse(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//NOTE: You cannot modify the response, only the request
// You can now access the headers
//var headers = response.Headers;
// If it is not an OK response, set the error and close the dialog
if (response.StatusCode != (int)HttpStatusCode.OK)
{
var error = $"{response.StatusCode}: {response.StatusText}";
var chromiumWebBrowser = (ChromiumWebBrowser)browserControl;
var parent = (MyBrowserForm)chromiumWebBrowser.Parent.Parent.Parent;
parent.Errors = error;
browser.CloseBrowser(true);
return true;
}
return false;
}
}
Then, in the original class, add this overridden method:
protected override IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
{
return new MyBrowserResourceRequestHandler();
}
It now all compiles correctly, and initial tests look good. I will update if any further changes prove necessary.
回答1:
It appears that in version 75, you should look into ResourceRequestHandler
instead, so you you will inherit from this class and use it. OnResourceResponse
can be found inside it.
The following is a sample code from ResourceRequestHandler.cs
which you can find in the CEFSharp repository inside CefSharp/CefSharp/Handler/ResourceRequestHandler.cs
bool IResourceRequestHandler.OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
return OnResourceResponse(chromiumWebBrowser, browser, frame, request, response);
}
So you need just to override OnResourceResponse
from the ResourceRequestHandler
:
protected override bool OnResourceResponse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
//Do whatever you like
}
Hope this helped you.
来源:https://stackoverflow.com/questions/59124372/cefsharp-how-can-i-access-onresourceresponse-from-build-75-1-143