I am integrating Browser into my software. It is working when I keep on opening new browser tabs but as soon as I close one tab and try to open new one it produces an exception in following code.
public async Task InitCore()
{
try
{
// Initialization.
await webView.EnsureCoreWebView2Async(null);
// This line gives exception if I close a tab and reopen as it gives exception in Initialization.
}
catch (Exception ex)
{
Enumerations.WriteToLog(Enumerations.LogType.Misc, "Browser.InitCore " + ex.ToString());
}
}
// Subscribing events.
private void AfterCoreReady(object sender,EventArgs e)
{
label1.Visible = false;
this.webView.CoreWebView2.ContentLoading += webView_ContentLoading;
this.webView.CoreWebView2.NewWindowRequested += webView_NewWindowRequested;
}
Following exception occurs while re-initialization after closing a tab:
CustomWebView2.OnEnter System.Runtime.InteropServices.COMException (0x8007139F):
The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.<CreateCoreWebView2ControllerAsync>d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Web.WebView2.WinForms.WebView2.<InitCoreWebView2Async>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at ProChart.Controls.Browser.<InitCore>d__16.MoveNext() in
Browser.cs:line 98
It's like you are calling your "Init" method every time you navigate to a new page.
Without seeing your FULL CODE I cannot be sure, but WebView2 controls are ONLY SUPPOSED TO BE INITIALIZED ONCE.
Generally the best way to do it is to call your await call
await webView.EnsureCoreWebView2Async(null);
Inside your main application start up,, for example the constructor of your main form in a windows forms, or WPF application.
In the newer versions however, you do not need to await on the call you've been using.
Simply add your init code in your constructor as follows:
public FrmMainForm()
{
InitializeComponent();
webview.Height = 720;
// Webview initialisation handler, called when control instantiated and ready
webview.CoreWebView2InitializationCompleted += Webview_CoreWebView2InitializationCompleted;
}
In my example above, I'm embedding the control in a windows forms desktop app, so I put it in my main form constructor.
The "CoreWebView2InitializationCompleted" event will be fired once the webview2 control is ready to be used, you can then initialise things in your webview, such as url interceptions, javascript injection and C# class injection, in that event handler.
private void Webview_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
// Custom URL handler (All URLS starting "http://app/" are intercepted directly by the application
webview.CoreWebView2.AddWebResourceRequestedFilter("http://app/*", CoreWebView2WebResourceContext.All);
webview.CoreWebView2.WebResourceRequested += WebResourceRequested;
// Load in our custom JS API files
webview.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(JsLoader.LoadApi("BrowserOverrides.js"));
// Show dev tools by default
webview.CoreWebView2.OpenDevToolsWindow();
// Other misc settings
webview.CoreWebView2.Settings.UserAgent = DEFAULTUA;
}
I have some demo code on my github that contains more examples on how to use all these features:
https://github.com/shawty/hbbtvbrowserEXPERIMENTAL
NOTE however, that this code uses an older version of webview2, some of the things in my code are done slightly differently in the newer versions.
User contributions licensed under CC BY-SA 3.0