Servicestack 6 aspcore set-cookie issue

Logs are full of these kinds of warnings:

2022-03-11 00:01:05.631 +00:00 [WRN] Could not Set-Cookie ‘ss-pid’: The response headers cannot be modified because the response has already started.
System.InvalidOperationException: The response headers cannot be modified because the response has already started.
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
at Microsoft.AspNetCore.Http.IHeaderDictionary.set_SetCookie(StringValues value)
at Microsoft.AspNetCore.Http.ResponseCookies.Append(String key, String value, CookieOptions options)
at ServiceStack.Host.NetCore.NetCoreResponse.SetCookie(Cookie cookie) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\NetCore\NetCoreResponse.cs:line 213

2022-03-11 00:01:05.631 +00:00 [INF] GET on /metadata responded 200 in 0 ms

2022-03-10 00:16:04.859 +00:00 [WRN] Could not Set-Cookie ‘ss-pid’: The response headers cannot be modified because the response has already started.
System.InvalidOperationException: The response headers cannot be modified because the response has already started.
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
at Microsoft.AspNetCore.Http.IHeaderDictionary.set_SetCookie(StringValues value)
at Microsoft.AspNetCore.Http.ResponseCookies.Append(String key, String value, CookieOptions options)
at ServiceStack.Host.NetCore.NetCoreResponse.SetCookie(Cookie cookie) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\NetCore\NetCoreResponse.cs:line 207
2022-03-10 00:16:04.860 +00:00 [WRN] Could not Set-Cookie ‘ss-id’: The response headers cannot be modified because the response has already started.
System.InvalidOperationException: The response headers cannot be modified because the response has already started.
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.ThrowIfReadOnly()
at Microsoft.AspNetCore.HttpSys.Internal.HeaderCollection.set_Item(String key, StringValues value)
at Microsoft.AspNetCore.Http.IHeaderDictionary.set_SetCookie(StringValues value)
at Microsoft.AspNetCore.Http.ResponseCookies.Append(String key, String value, CookieOptions options)
at ServiceStack.Host.NetCore.NetCoreResponse.SetCookie(Cookie cookie) in C:\BuildAgent\work\3481147c480f4a2f\src\ServiceStack\Host\NetCore\NetCoreResponse.cs:line 207
2022-03-10 00:16:04.860 +00:00 [INF] GET on /metadata responded 200 in 0 ms

any way to fix this? looks like its just calls to /metadata for the most part, but its filling my logs.

looks like its always either line 207 or 213

Try overriding SetCookieFilter and returning false if the response has started:

public override bool SetCookieFilter(IRequest req, Cookie cookie)
{
    if (req.Response.HasStarted)
        return false;
    return base.SetCookieFilter(req, cookie);
}

I’ve added this check as well, which will be in the next version.

awesome. I will give it a try. thanks!

1 Like