microsoft/aspire · error · InvalidOperationException

Tracked browser protocol event method was not a string.

Error message

Tracked browser protocol event method was not a string.

What it means

In a CDP event frame, the "method" property must be a string naming the event (e.g. "Log.entryAdded"). If the "method" property is present but its value is not a JSON string, ParseMessageHeader throws this InvalidOperationException. Note "sessionId" tolerates non-strings (treated as null), but "method" is strict because the method name drives event dispatch.

Solutions

  1. Inspect the offending frame with DescribeFrame
  2. Confirm the browser endpoint is real Chromium CDP, not a custom/mock server
  3. Fix test fixtures or mocks to emit "method" as a JSON string
  4. Wrap parsing in try-catch and skip malformed frames

Example fix

// before
var header = BrowserLogsCdpProtocol.ParseMessageHeader(frame); // frame: {"method":123}
// after
// fix the emitting side (mock/fixture):
var json = """{"method":"Log.entryAdded"}"""; // method must be a string
Defensive patterns

Strategy: validation

Validate before calling

// ensure fixtures/mocks emit "method" as a JSON string
var valid = rawFrame.Contains("\"method\":\""); // quick sanity check for test payloads

Try / catch

try
{
    var header = BrowserLogsCdpProtocol.ParseMessageHeader(frame);
}
catch (InvalidOperationException ex) when (ex.Message.Contains("method was not a string"))
{
    logger.LogWarning("CDP event with non-string method skipped: {Frame}", BrowserLogsCdpProtocol.DescribeFrame(frame));
}

Prevention

When it happens

Trigger: ParseMessageHeader reads property name "method" and finds TokenType is not JsonTokenType.String - e.g. "method": 42, null, or an object.

Common situations: A non-Chromium automation endpoint or mocking tool emits malformed CDP events, a proxy mangles the frame, or a hand-crafted test payload has the wrong value type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/cae0ee8f218989d1. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Browsers/BrowserLogsCdpProtocol.cs:105

            if (!reader.Read())
            {
                throw new InvalidOperationException("Tracked browser protocol frame ended unexpectedly.");
            }

            switch (propertyName)
            {
                case "id":
                    if (!reader.TryGetInt64(out var parsedId))
                    {
                        throw new InvalidOperationException("Tracked browser protocol response id was not an integer.");
                    }

                    id = parsedId;
                    break;
                case "method":
                    method = reader.TokenType == JsonTokenType.String
                        ? reader.GetString()
                        : throw new InvalidOperationException("Tracked browser protocol event method was not a string.");
                    break;
                case "sessionId":
                    sessionId = reader.TokenType == JsonTokenType.String
                        ? reader.GetString()
                        : null;
                    break;
                default:
                    reader.Skip();
                    break;
            }
        }

        return new BrowserLogsCdpProtocolMessageHeader(id, method, sessionId);
    }

    internal static byte[] CreateCommandFrame(long id, string method, string? sessionId, Action<Utf8JsonWriter>? writeParameters)
    {
        var buffer = new ArrayBufferWriter<byte>();

View on GitHub (pinned to 25830f84bd)