Skip to content

Fix Invalid cast exception #112

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ Build/BinScopeOutDir/
Build/BinScopeTargetDir/
Scripts/

/_ReSharper.Caches/*
/.idea/*
/.vs/*
11 changes: 9 additions & 2 deletions src/RedisSessionStateProvider/RedisConnectionWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,15 @@ private bool SetPrepare(ISessionStateItemCollection data, int sessionTimeout, ou
valueArgs = null;
if (data != null && data.Count > 0)
{
ChangeTrackingSessionStateItemCollection sessionItems = (ChangeTrackingSessionStateItemCollection)data;
List<object> list = redisUtility.GetNewItemsAsList(sessionItems);
List<object> list;
if (data is ChangeTrackingSessionStateItemCollection sessionItems)
{
list = redisUtility.GetNewItemsAsList(sessionItems);
}
else
{
list = redisUtility.GetAllItemsAsList(data);
}
if (list.Count > 0)
{
keyArgs = new string[] { Keys.DataKey, Keys.InternalKey };
Expand Down
12 changes: 12 additions & 0 deletions src/Shared/RedisUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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

namespace Microsoft.Web.Redis
{
Expand Down Expand Up @@ -73,6 +74,17 @@ public List<object> GetNewItemsAsList(ChangeTrackingSessionStateItemCollection s
return list;
}

public List<object> GetAllItemsAsList(ISessionStateItemCollection sessionItems)
{
List<object> list = new List<object>(sessionItems.Keys.Count * 2);
foreach (string key in sessionItems.Keys)
{
list.Add(key);
list.Add(GetBytesFromObject(sessionItems[key]));
}
return list;
}

internal byte[] GetBytesFromObject(object data)
{
return _serializer.Serialize(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Threading.Tasks;
using Xunit;
using System.Threading;
using System.Web;
using System.Web.SessionState;
using Microsoft.AspNet.SessionState;

namespace Microsoft.Web.Redis.FunctionalTests
Expand Down Expand Up @@ -148,6 +150,20 @@ public async Task ReleaseItemExclusiveWithNullLockId()
DisposeRedisConnectionWrapper();
}
}

[Fact]
public async Task SetAndReleaseItemExclusiveWithSessionStateItemCollection()
{
using (RedisServer redisServer = new RedisServer())
{
string sessionId = ResetRedisConnectionWrapperAndConfiguration();
RedisSessionStateProvider ssp = new RedisSessionStateProvider();
var item = new SessionStateStoreData(new SessionStateItemCollection(), new HttpStaticObjectsCollection(), 1);
item.Items["testKey"] = "test data";
await ssp.SetAndReleaseItemExclusiveAsync(null, sessionId, item, null, true, CancellationToken.None);
DisposeRedisConnectionWrapper();
}
}

[Fact]
public async Task RemoveItemWithNullLockId()
Expand Down