dotnet/wpf · error · ArgumentNullException

ArgumentNullException(nameof(scriptName))

Error message

ArgumentNullException(nameof(scriptName))

What it means

WebBrowser.InvokeScript throws ArgumentNullException when scriptName is null or empty, after verifying thread access. A script function name is required to locate the dispatch target on the hosted document.

Solutions

  1. Validate the script name: if (string.IsNullOrEmpty(name)) return/skip before calling.
  2. Ensure the constant/binding supplying the name is non-empty.
  3. Check the document finished loading (LoadCompleted) before invoking so the right name is used.
  4. Wrap in try-catch (ArgumentNullException) for dynamic callers.

Example fix

// before
webBrowser.InvokeScript(scriptName, args);
// after
if (!string.IsNullOrEmpty(scriptName))
    webBrowser.InvokeScript(scriptName, args);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(scriptName)) return null;

Type guard

bool CanInvoke(string name) => !string.IsNullOrEmpty(name);

Try / catch

try { result = webBrowser.InvokeScript(name, args); } catch (ArgumentNullException) { result = null; }

Prevention

When it happens

Trigger: Calling webBrowser.InvokeScript(null, args) or InvokeScript("", args); passing a name from a config/binding that resolved to empty.

Common situations: Invoking script before page load where the name comes from parsed metadata; empty setting in app config; refactoring that lost the script name constant.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/205a1c3578f39179. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs:257

        /// <returns>The object returned by the Active Scripting call.</returns>
        public object InvokeScript(string scriptName)
        {
            return InvokeScript(scriptName, null);
        }

        /// <summary>
        /// Executes an Active Scripting function defined in the HTML document currently loaded in the WebBrowser control. 
        /// </summary>
        /// <param name="scriptName">The name of the script method to invoke.</param>
        /// <param name="args"></param>
        /// <returns>The object returned by the Active Scripting call.</returns>
        public object InvokeScript(string scriptName, params object[] args)
        {
            VerifyAccess();

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentNullException(nameof(scriptName));
            }

            UnsafeNativeMethods.IDispatchEx scriptObjectEx = null;
            UnsafeNativeMethods.IHTMLDocument2 htmlDocument = NativeHTMLDocument;
            if (htmlDocument != null)
            {
                scriptObjectEx = htmlDocument.GetScript() as UnsafeNativeMethods.IDispatchEx;
            }

            // Protect against the cross domain scripting attacks. 
            // We rely on the site locking feature in gerneral. But in IE 6 server side redirect is not blocked.
            // (In IE 7 it is blocked by turning on the DOCHOSTUIFLAG.ENABLE_REDIRECT_NOTIFICATION flag so that  
            // the additional BeforeNavigate2 event is fired for server side redirect.)

            object retVal = null;            
            if (scriptObjectEx != null)
            {
                NativeMethods.DISPPARAMS dp = new NativeMethods.DISPPARAMS

View on GitHub (pinned to 81131a70a4)