尝试执行传入的函数时,WebView2 HostObjectToScript 抛出运行时 System.NotImplementedException 异常

问题描述

问题: HostObjectToScript 尝试执行传入函数时抛出异常

场景: WPF 应用程序嵌入了一个 WebView2 选项卡,并使用 CoreWebView2.AddHostObjectToScript() 公开了一个用于编写脚本的对象。我可以使用 Javascript 访问这个对象:

window.blah = chrome.webview.hostObjects.sync.blah_blah_external;

调用函数没有问题。一切正常,直到我调用一个函数来创建一个类并将函数作为参数传递给该类。参数传入就好了,但是当代码尝试调用函数时,它会抛出 System.NotImplementedException 异常。这在用 WebView2 替换基于 IE 的浏览器控件之前工作得很好,所以我希望它是我遗漏的一些简单的东西。

环境:

Host 对象是这样公开的:

variablename.CoreWebView2.AddHostObjectToScript("blah_blah_external",new MYTABExternal(_tabHost));

宿主对象如下所示:

namespace MYTAB
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ComVisibleAttribute(true)]
    public class MYTABExternal
    {
        public void createTHING(String user,dynamic onError)
        {
            CLASSNAME currentCLASSNAME = new CLASSNAME(user,onError);
            currentCLASSNAME.create();
        }
    }
}

抛出错误的类是这样设置的:

namespace MYTAB
{
    public class CLASSNAME
    {
       
        protected String user;
        protected dynamic onError;

        public CLASSNAME(String user,dynamic onError)
        {
            this.user= user;
            this.onError = onError;
        }

        public void create()
        {            
            BackgroundWorker backgroundWorker = new BackgroundWorker();
            backgroundWorker.WorkerSupportsCancellation = true;
            backgroundWorker.WorkerReportsProgress = true;
            backgroundWorker.DoWork -= backgroundWorker_DoWork;
            backgroundWorker.DoWork += backgroundWorker_DoWork;
            backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;  //Tell the user how the process went
            RequestContext requestContext = EXTERNALLIBRARY.SharedContext.Instance;
            backgroundWorker.RunWorkerAsync(requestContext);
           
        }

        /// <summary>
        /// Fires when the background task is complete
        /// </summary>
        private void backgroundWorker_RunWorkerCompleted(object sender,System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                this.onError("The creation process was cancelled"); // HERE'S THE EXCEPTION
            }
        }
...

调用函数的javascript是:

window.setTimeout(function () {
  window.blah.createTHING(btn.attr('data-requestor-user-id'),function (onErrorResponse) {
                                  dialog.$modal.addClass('type-danger');
                                  dialog.setMessage('<p>' + onErrorResponse + '</p>');
                                  spinner.stop();
  });
},1000);

异常堆栈跟踪是:

Exception thrown: 'System.NotImplementedException' in System.Dynamic.dll
Exception thrown: 'System.NotImplementedException' in System.Dynamic.dll
System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Critical"><TraceIdentifier>http://msdn.microsoft.com/TraceCodes/System/ActivityTracing/2004/07/Reliability/Exception/Unhandled</TraceIdentifier><Description>Unhandled exception</Description><AppDomain>BLAH</AppDomain><Exception><ExceptionType>System.NotImplementedException,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089</ExceptionType><Message>The method or operation is not implemented.</Message><StackTrace>   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,IntPtr errorInfo)
   at System.Dynamic.ComruntimeHelpers.GetITypeInfoFromIdispatch(Idispatch dispatch,Boolean throwIfMissingExpectedTypeInfo)
   at System.Dynamic.IdispatchComObject.EnsureScanDefinedMethods()
   at System.Dynamic.IdispatchComObject.System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(Expression parameter)
   at System.Dynamic.DynamicMetaObject.Create(Object value,Expression expression)
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args,ReadOnlyCollection`1 parameters,LabelTarget returnLabel)
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site,Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site,T0 arg0,T1 arg1)
   at CallSite.Target(Closure,CallSite,Object,String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[T0,T1](CallSite site,T1 arg1)
   at MyTab.CLASSNAME.backgroundWorker_RunWorkerCompleted(Object sender,RunWorkerCompletedEventArgs e) in C:\CLASSNAME.cs:line 145
   at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
   at System.ComponentModel.BackgroundWorker.AsyncoperationCompleted(Object arg)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</StackTrace><ExceptionString>System.NotImplementedException: The method or operation is not implemented.
   at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode,RunWorkerCompletedEventArgs e) in C:\\CLASSNAME.cs:line 145
   at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
   at System.ComponentModel.BackgroundWorker.AsyncoperationCompleted(Object arg)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</ExceptionString><DataItems><Data><Key>ApplicationName</Key><Value>BLAH</Value></Data></DataItems></Exception></TraceRecord>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)