Skip to content

Commit 49df7bf

Browse files
authored
fix: Null reference exception in Asp.NET Core 6+ browser monitoring injection. (#3102)
1 parent bd9ef4c commit 49df7bf

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

src/Agent/NewRelic/Agent/Core/Agent.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ public async Task TryInjectBrowserScriptAsync(string contentType, string request
324324

325325
if (rumBytes == null)
326326
{
327-
transaction.LogFinest("Skipping RUM Injection: No script was available.");
327+
transaction?.LogFinest("Skipping RUM Injection: No script was available.");
328328
await baseStream.WriteAsync(buffer, 0, buffer.Length);
329329
}
330330
else

src/Agent/NewRelic/Agent/Extensions/Providers/Wrapper/AspNetCore6Plus/BrowserInjectionMiddleware.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ public BrowserInjectionMiddleware(RequestDelegate next, IAgent agent)
2121

2222
public Task Invoke(HttpContext context)
2323
{
24-
if (!BrowserInjectingStreamWrapper.Disabled)
24+
// don't invoke the middleware if browser injection is disabled or if we don't have a valid transaction
25+
if (BrowserInjectingStreamWrapper.Disabled || !_agent.CurrentTransaction.IsValid)
2526
{
26-
// wrap the response body in our stream wrapper which will inject the RUM script if appropriate
27-
using var injectedResponse = new BrowserInjectingStreamWrapper(_agent, context.Response.Body, context);
28-
context.Features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(injectedResponse));
27+
return _next(context);
2928
}
3029

30+
// wrap the response body in our stream wrapper which will inject the RUM script if appropriate
31+
using var injectedResponse = new BrowserInjectingStreamWrapper(_agent, context.Response.Body, context);
32+
context.Features.Set<IHttpResponseBodyFeature>(new StreamResponseBodyFeature(injectedResponse));
33+
3134
return _next(context);
3235
}
3336
}

0 commit comments

Comments
 (0)