@@ -10,11 +10,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
     {
         public static ArraySegment<byte> GetArray(this Memory<byte> memory)
         {
-            if (!memory.TryGetArray(out var result))
-            {
-                throw new InvalidOperationException("Buffer backed by array was expected");
-            }
-            return result;
+            return ((ReadOnlyMemory<byte>)memory).GetArray();
         }
 
         public static ArraySegment<byte> GetArray(this ReadOnlyMemory<byte> memory)
﻿// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Runtime.InteropServices;

namespace Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.Internal
{
    public static class BufferExtensions
    {
        public static ArraySegment<byte> GetArray(this Memory<byte> memory)
        {
            if (!memory.TryGetArray(out var result))
            {
                throw new InvalidOperationException("Buffer backed by array was expected");
            }
            return result;
        }

        public static ArraySegment<byte> GetArray(this ReadOnlyMemory<byte> memory)
        {
            if (!MemoryMarshal.TryGetArray(memory, out var result))
            {
                throw new InvalidOperationException("Buffer backed by array was expected");
            }
            return result;
        }
    }
}

Use AsSpan instead of casting