Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore ISerializer ability, "safely" #218

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c0e9294
restore ISerializer ability, "safely"
zachrybaker Dec 18, 2023
34e33cf
make suggested changes
stanleysmall-microsoft Jan 12, 2024
4b45e2d
whitespace changes
stanleysmall-microsoft Jan 12, 2024
4661de2
update all the packages
stanleysmall-microsoft Jan 12, 2024
8f0eeec
make philo requested changes
stanleysmall-microsoft Jan 12, 2024
078989d
remove sessiondatatype from keygenerator
stanleysmall-microsoft Jan 12, 2024
a709789
update SE redis again
stanleysmall-microsoft Jan 19, 2024
e185256
add unit tests for interface and implementation
stanleysmall-microsoft Jan 25, 2024
adc6940
split ProviderConfiguration into two implementations
stanleysmall-microsoft Jan 30, 2024
6a0bd99
refactor so code is not repeated
stanleysmall-microsoft Jan 30, 2024
e1c6585
Merge branch 'Azure:main' into custom-serialization
zachrybaker Mar 11, 2024
1cd58b8
update packages
stanleysmall-microsoft Jun 5, 2024
5dba4dc
simplify code in middleware tests
stanleysmall-microsoft Jun 5, 2024
dbb3c48
update whitespace
stanleysmall-microsoft Jun 5, 2024
e46df4c
add a functional test that provides its own ISessionStateSerializer
stanleysmall-microsoft Jun 5, 2024
a53ecaf
remove changes for output cache
stanleysmall-microsoft Jun 5, 2024
2a65e87
add copyright statement to files
stanleysmall-microsoft Jun 5, 2024
835f42c
Update xunit so tests run
stanleysmall-microsoft Jun 5, 2024
f966758
Change config name to redisSerializerType
stanleysmall-microsoft Jun 5, 2024
88ca0f4
Added a serialization suffix for the serializer type
stanleysmall-microsoft Jun 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/OutputCacheProvider/RedisOutputCacheProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.OutputCache.OutputCacheModuleAsync" Version="1.0.2" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="5.0.16" />
<PackageReference Include="Microsoft.Extensions.Caching.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Extensions" Version="2.2.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
</ItemGroup>

</Project>
7 changes: 5 additions & 2 deletions src/RedisSessionStateProvider/KeyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//

using System;
using System.Web.SessionState;
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved

namespace Microsoft.Web.Redis
{
Expand All @@ -13,17 +14,19 @@ internal class KeyGenerator
public string DataKey { get; private set; }
public string LockKey { get; private set; }
public string InternalKey { get; private set; }
public string SessionDataType { get; private set; }

private void GenerateKeys(string id, string app)
{
this.id = id;
DataKey = $"{{{app}_{id}}}_SessionStateItemCollection";
DataKey = $"{{{app}_{id}}}_{SessionDataType}";
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
LockKey = $"{{{app}_{id}}}_WriteLock";
InternalKey = $"{{{app}_{id}}}_SessionTimeout";
}

public KeyGenerator(string sessionId, string applicationName)
public KeyGenerator(string sessionId, string applicationName, string sessionStateDataType = "SessionStateItemCollection")
{
SessionDataType = sessionStateDataType;
GenerateKeys(sessionId, applicationName);
}

Expand Down
10 changes: 3 additions & 7 deletions src/RedisSessionStateProvider/RedisConnectionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.SessionState;

namespace Microsoft.Web.Redis
Expand All @@ -23,7 +22,7 @@ internal class RedisConnectionWrapper : ICacheConnection
public RedisConnectionWrapper(ProviderConfiguration configuration, string id)
{
this.configuration = configuration;
Keys = new KeyGenerator(id, configuration.ApplicationName);
Keys = new KeyGenerator(id, configuration.ApplicationName, configuration.SessionDataSerializer.StorageTypeName);

// only single object of RedisSharedConnection will be created and then reused
if (sharedConnection == null)
Expand Down Expand Up @@ -127,11 +126,8 @@ internal byte[] SerializeSessionStateItemCollection(ISessionStateItemCollection
{
return null;
}
MemoryStream ms = new MemoryStream();
BinaryWriter writer = new BinaryWriter(ms);
((SessionStateItemCollection)sessionStateItemCollection).Serialize(writer);
writer.Close();
return ms.ToArray();

return configuration.SessionDataSerializer.Serialize((SessionStateItemCollection)sessionStateItemCollection);
}

public void Set(ISessionStateItemCollection data, int sessionTimeout)
Expand Down
12 changes: 6 additions & 6 deletions src/RedisSessionStateProvider/RedisSessionStateProvider.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.SessionState.SessionStateModule" Version="2.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>
32 changes: 32 additions & 0 deletions src/Shared/BinaryFormattingSessionSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.IO;
using System.Web.SessionState;

namespace Microsoft.Web.RedisSessionStateProvider
{
/// <summary>
/// The default, "safe" serialization.
/// Hint. It's not safe. But it is what MS allows today.
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
/// Better is to use json or MessagePack.
/// </summary>
internal class BinaryFormattingSessionSerializer : ISessionDataSerializer
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
{
public string StorageTypeName { get => "SessionStateItemCollection"; }
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved

public SessionStateItemCollection Deserialize(byte[] data)
{
MemoryStream ms = new MemoryStream((byte[])data);
BinaryReader reader = new BinaryReader(ms);
return SessionStateItemCollection.Deserialize(reader);

stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
}

public byte[] Serialize(SessionStateItemCollection data)
{
MemoryStream ms = new MemoryStream();
BinaryWriter writer = new BinaryWriter(ms);
((SessionStateItemCollection)data).Serialize(writer);
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
writer.Close();
return ms.ToArray();
}
}
}
11 changes: 11 additions & 0 deletions src/Shared/ISessionDataSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Web.SessionState;

namespace Microsoft.Web.RedisSessionStateProvider
{
public interface ISessionDataSerializer
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
{
string StorageTypeName { get; }
byte[] Serialize(SessionStateItemCollection data);
SessionStateItemCollection Deserialize(byte[] data);
}
}
19 changes: 19 additions & 0 deletions src/Shared/ProviderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Reflection;
using System.Web.Configuration;
using System.Web.Hosting;
using Microsoft.Web.RedisSessionStateProvider;

namespace Microsoft.Web.Redis
{
Expand All @@ -28,6 +29,7 @@ internal class ProviderConfiguration
public int ConnectionTimeoutInMilliSec { get; set; }
public int OperationTimeoutInMilliSec { get; set; }
public string ConnectionString { get; set; }
public ISessionDataSerializer SessionDataSerializer { get; set; } = new BinaryFormattingSessionSerializer();

/* Empty constructor required for testing */

Expand Down Expand Up @@ -87,6 +89,7 @@ private ProviderConfiguration(NameValueCollection config)
// All below parameters are only fetched from web.config
DatabaseId = GetIntSettings(config, "databaseId", 0);
ApplicationName = GetStringSettings(config, "applicationName", null);

if (ApplicationName == null)
{
try
Expand Down Expand Up @@ -117,6 +120,22 @@ private ProviderConfiguration(NameValueCollection config)

ConnectionTimeoutInMilliSec = GetIntSettings(config, "connectionTimeoutInMilliseconds", 0);
OperationTimeoutInMilliSec = GetIntSettings(config, "operationTimeoutInMilliseconds", 0);

var serializationTypeNameSpace = GetStringSettings(config, "sessionSerializationNamespaceAndType", null);
var serializationTypeAssembly = GetStringSettings(config, "sessionSerializationTypeAssembly", null);

if (!string.IsNullOrEmpty(serializationTypeNameSpace) && !string.IsNullOrEmpty(serializationTypeAssembly))
{
try
{
var serializer = Activator.CreateInstance(serializationTypeAssembly, serializationTypeNameSpace);
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
SessionDataSerializer = (ISessionDataSerializer)serializer.Unwrap();
}
catch(Exception e)
{
throw new TypeLoadException($"Could not activate Session Serialization Type from assembly {serializationTypeAssembly} and namespace {serializationTypeNameSpace}.", e);
}
}
}

// 1) Use key available inside AppSettings
Expand Down
6 changes: 3 additions & 3 deletions src/Shared/StackExchangeClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,9 @@ internal SessionStateItemCollection DeserializeSessionStateItemCollection(RedisR
{
try
{
MemoryStream ms = new MemoryStream((byte[])serializedSessionStateItemCollection);
BinaryReader reader = new BinaryReader(ms);
return SessionStateItemCollection.Deserialize(reader);
var bytes = (byte[])serializedSessionStateItemCollection;
stanleysmall-microsoft marked this conversation as resolved.
Show resolved Hide resolved
return _configuration.SessionDataSerializer.Deserialize(bytes);

}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.AspNet.OutputCache.OutputCacheModuleAsync" Version="1.0.2" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.AspNet.OutputCache.OutputCacheModuleAsync" Version="1.0.2" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.4" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6</TargetFrameworks>
Expand All @@ -10,10 +10,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="coverlet.collector" Version="3.1.0" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.AspNet.SessionState.SessionStateModule" Version="2.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
<PackageReference Include="Castle.Core" Version="4.4.1" />
<PackageReference Include="FakeItEasy" Version="7.3.1" />
<PackageReference Include="Microsoft.AspNet.SessionState.SessionStateModule" Version="2.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="StackExchange.Redis" Version="2.6.122" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="6.0.1" />
<PackageReference Include="System.IO.Pipelines" Version="6.0.2" />
<PackageReference Include="StackExchange.Redis" Version="2.7.4" />
<PackageReference Include="System.Diagnostics.PerformanceCounter" Version="8.0.0" />
<PackageReference Include="System.IO.Pipelines" Version="8.0.0" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="6.0.0" />
<PackageReference Include="System.Threading.Channels" Version="8.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0" />
</ItemGroup>
</Project>