dotnet/wpf · error

SR.Format(SR.BamlWriterBadAssembly, assemblyName)

Error message

SR.Format(SR.BamlWriterBadAssembly, assemblyName)

What it means

BamlWriter.GetAssembly resolves an assembly by name via ReflectionHelper.LoadAssembly, caching results. If the load returns null — the assembly cannot be located under the given name — it throws ArgumentException('Bad assembly'). This happens while resolving type/assembly references during BAML writing.

Solutions

  1. Verify the assembly name string matches the target assembly's simple name exactly
  2. Ensure the assembly is deployed where the loader can find it (app base, probing path, or GAC)
  3. Use the fully qualified assembly name if the simple-name load fails
  4. Pre-load the assembly so it is in memory/probing context before BAML writing

Example fix

// before
Assembly.Load("MyControls, Version=2.0.0.0"); // wrong version referenced
// after
Assembly.Load("MyControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=...");
Defensive patterns

Strategy: validation

Validate before calling

try { if (Assembly.Load(assemblyName) == null) throw new ArgumentException($"Assembly '{assemblyName}' cannot be loaded"); } catch (Exception ex) { throw new ArgumentException($"Assembly '{assemblyName}' is not loadable: {ex.Message}"); }

Try / catch

try { ... } catch (ArgumentException ex) when (ex.Message.Contains(assemblyName)) { Console.Error.WriteLine($"Check that '{assemblyName}' is deployed and the name is correct"); throw; }

Prevention

When it happens

Trigger: BAML content references types whose assembly name cannot be resolved by simple-name load (no full name, assembly not in the app's probing path or GAC, wrong spelling, or assembly not deployed).

Common situations: Referencing custom control assemblies in XAML compiled/written at runtime; assembly renamed or version-bumped; satellite/partial-trust environments where the assembly is not loadable; typos in xmlns clr-namespace assembly= clauses.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlWriter.cs:1068

    *
    * BamlWriter.GetAssembly
    *
    * Get the Assembly given a name.  This uses the LoadWrapper to load the
    * assembly from the current directory.  
    * NOTE:  Assembly paths are not currently supported, but may be in the
    *        future if the need arises.
    *
    \***************************************************************************/

    private Assembly GetAssembly(string assemblyName)
    {
        Assembly assy = _assemblies[assemblyName] as Assembly;
        if (assy == null)
        {
            assy = ReflectionHelper.LoadAssembly(assemblyName, null);
            if (assy == null)
            {
                throw new ArgumentException(SR.Format(SR.BamlWriterBadAssembly, 
                                                   assemblyName));
            }
            else
            {
                _assemblies[assemblyName] = assy;
            }
        }

        return assy;
    }

    /***************************************************************************\
    *
    * BamlWriter.GetType
    *
    * Get the Type given an assembly name where the type is declared and the
    * type's fully qualified name
    *

View on GitHub (pinned to 81131a70a4)