Skip to content

Commit

Permalink
Created ISession with SessionFactory provider using ICacheClient.
Browse files Browse the repository at this point in the history
Can now share the same session between MVC and ServiceStack with ISession
Can now RequestContext.Get<IHttpResponse>()
Can now Filter = (req, res, dto) => req.TryResolve<IDependency>();
  • Loading branch information
mythz committed Dec 16, 2011
1 parent 4678449 commit 490cbff
Show file tree
Hide file tree
Showing 47 changed files with 359 additions and 392 deletions.
2 changes: 1 addition & 1 deletion NuGet/ServiceStack.Common/servicestack.common.nuspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Common</id> <id>ServiceStack.Common</id>
<version>3.1.3</version> <version>3.1.6</version>
<authors>Demis Bellot</authors> <authors>Demis Bellot</authors>
<owners>Demis Bellot</owners> <owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary> <summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Host.AspNet</id> <id>ServiceStack.Host.AspNet</id>
<version>3.1.5</version> <version>3.1.6</version>
<authors>Demis Bellot</authors> <authors>Demis Bellot</authors>
<owners>Demis Bellot</owners> <owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary> <summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
14 changes: 14 additions & 0 deletions NuGet/ServiceStack.Host.Mvc/content/App_Start/ControllerBase.cs.pp
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
public abstract class ControllerBase : Controller public abstract class ControllerBase : Controller
{ {
public ICacheClient Cache { get; set; } public ICacheClient Cache { get; set; }
public ISessionFactory SessionFactory { get; set; }

private ISession session;
public ISession Session
{
get
{
return session ?? (session =
SessionFactory.GetOrCreateSession(
new ServiceStack.WebHost.Endpoints.Extensions.HttpRequestWrapper(null, System.Web.HttpContext.Current.Request),
new ServiceStack.WebHost.Endpoints.Extensions.HttpResponseWrapper(System.Web.HttpContext.Current.Response)
));
}
}


protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior) protected override JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
{ {
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
//Register In-Memory Cache provider. //Register In-Memory Cache provider.
//For Distributed Cache Providers Use: PooledRedisClientManager, BasicRedisClientManager or see: https://github.com/ServiceStack/ServiceStack/wiki/Caching //For Distributed Cache Providers Use: PooledRedisClientManager, BasicRedisClientManager or see: https://github.com/ServiceStack/ServiceStack/wiki/Caching
container.Register<ICacheClient>(new MemoryCacheClient()); container.Register<ICacheClient>(new MemoryCacheClient());
container.Register<ISessionFactory>(c =>
new SessionFactory(c.Resolve<ICacheClient>()));


//Set MVC to use the same Funq IOC as ServiceStack //Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container)); ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
Expand Down
2 changes: 1 addition & 1 deletion NuGet/ServiceStack.Host.Mvc/servicestack.host.mvc.nuspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack.Host.Mvc</id> <id>ServiceStack.Host.Mvc</id>
<version>3.1.5</version> <version>3.1.6</version>
<authors>Demis Bellot</authors> <authors>Demis Bellot</authors>
<owners>Demis Bellot</owners> <owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary> <summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
2 changes: 1 addition & 1 deletion NuGet/ServiceStack/servicestack.nuspec
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <package xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd"> <metadata xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<id>ServiceStack</id> <id>ServiceStack</id>
<version>3.1.3</version> <version>3.1.6</version>
<authors>Demis Bellot</authors> <authors>Demis Bellot</authors>
<owners>Demis Bellot</owners> <owners>Demis Bellot</owners>
<summary>Opensource .NET and Mono REST Web Services framework</summary> <summary>Opensource .NET and Mono REST Web Services framework</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/RazorEngine/RazorHandler.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override object CreateRequest(IHttpRequest request, string operationName)
return null; return null;
} }


public override object GetResponse(IHttpRequest httpReq, object request) public override object GetResponse(IHttpRequest httpReq, IHttpResponse httpRes, object request)
{ {
return null; return null;
} }
Expand Down
31 changes: 31 additions & 0 deletions src/ServiceStack.Interfaces/CacheAccess/ISession.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace ServiceStack.CacheAccess
{
/// <summary>
/// A Users Session
/// </summary>
public interface ISession
{
/// <summary>
/// Store any object at key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
object this[string key] { get; set; }

/// <summary>
/// Set a typed value at key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="value"></param>
void Set<T>(string key, T value);

/// <summary>
/// Get a typed value at key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
T Get<T>(string key);
}
}
18 changes: 18 additions & 0 deletions src/ServiceStack.Interfaces/CacheAccess/ISessionFactory.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
using ServiceStack.ServiceHost;

namespace ServiceStack.CacheAccess
{
/// <summary>
/// Retrieves a User Session
/// </summary>
public interface ISessionFactory
{
/// <summary>
/// Gets the session for this request, creates one if it doesn't exist.
/// </summary>
/// <param name="httpReq"></param>
/// <param name="httpRes"></param>
/// <returns></returns>
ISession GetOrCreateSession(IHttpRequest httpReq, IHttpResponse httpRes);
}
}
2 changes: 2 additions & 0 deletions src/ServiceStack.Interfaces/ServiceHost/IHttpRequest.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IHttpRequest
{ {
object OriginalRequest { get; } object OriginalRequest { get; }


T TryResolve<T>();

string OperationName { get; } string OperationName { get; }


string ContentType { get; } string ContentType { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/ServiceStack.Interfaces/ServiceStack.Interfaces.csproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<Compile Include="CacheAccess\IMemcachedClient.cs" /> <Compile Include="CacheAccess\IMemcachedClient.cs" />
<Compile Include="CacheAccess\IPersistenceProviderCache.cs" /> <Compile Include="CacheAccess\IPersistenceProviderCache.cs" />
<Compile Include="CacheAccess\IPersistenceProviderCacheFactory.cs" /> <Compile Include="CacheAccess\IPersistenceProviderCacheFactory.cs" />
<Compile Include="CacheAccess\ISession.cs" />
<Compile Include="CacheAccess\ISessionFactory.cs" />
<Compile Include="Configuration\IFactoryProvider.cs" /> <Compile Include="Configuration\IFactoryProvider.cs" />
<Compile Include="Configuration\IContainerAdapter.cs" /> <Compile Include="Configuration\IContainerAdapter.cs" />
<Compile Include="Configuration\IResourceManager.cs" /> <Compile Include="Configuration\IResourceManager.cs" />
Expand Down
22 changes: 22 additions & 0 deletions src/ServiceStack.ServiceInterface/ServiceBase.cs
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,8 @@
using System; using System;
using System.Net; using System.Net;
using System.Web;
using ServiceStack.CacheAccess;
using ServiceStack.CacheAccess.Providers;
using ServiceStack.Common; using ServiceStack.Common;
using ServiceStack.Common.Utils; using ServiceStack.Common.Utils;
using ServiceStack.Common.Web; using ServiceStack.Common.Web;
Expand All @@ -10,6 +13,7 @@
using ServiceStack.ServiceInterface.ServiceModel; using ServiceStack.ServiceInterface.ServiceModel;
using ServiceStack.Text; using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints; using ServiceStack.WebHost.Endpoints;
using ServiceStack.WebHost.Endpoints.Extensions;


namespace ServiceStack.ServiceInterface namespace ServiceStack.ServiceInterface
{ {
Expand Down Expand Up @@ -73,6 +77,24 @@ public virtual IAppHost AppHost


public IRequestContext RequestContext { get; set; } public IRequestContext RequestContext { get; set; }


public ISessionFactory SessionFactory { get; set; }

private ISession session;
public ISession Session
{
get
{
if (SessionFactory == null)
SessionFactory = new SessionFactory(this.GetCacheClient());

return session ?? (session =
SessionFactory.GetOrCreateSession(
RequestContext.Get<IHttpRequest>(),
RequestContext.Get<IHttpResponse>()
));
}
}

/// <summary> /// <summary>
/// Resolve an alternate Web Service from ServiceStack's IOC container. /// Resolve an alternate Web Service from ServiceStack's IOC container.
/// </summary> /// </summary>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
<Compile Include="Deprecated.Session\PublicAndPrivateClientSessions.cs" /> <Compile Include="Deprecated.Session\PublicAndPrivateClientSessions.cs" />
<Compile Include="Deprecated.Session\UserSession.cs" /> <Compile Include="Deprecated.Session\UserSession.cs" />
<Compile Include="ServiceRoutesExtension.cs" /> <Compile Include="ServiceRoutesExtension.cs" />
<Compile Include="SessionFactory.cs" />
<Compile Include="SessionFeature.cs" /> <Compile Include="SessionFeature.cs" />
<Compile Include="Testing\BasicAppHost.cs" /> <Compile Include="Testing\BasicAppHost.cs" />
<Compile Include="Testing\MockHttpRequest.cs"> <Compile Include="Testing\MockHttpRequest.cs">
Expand Down
61 changes: 61 additions & 0 deletions src/ServiceStack.ServiceInterface/SessionFactory.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using ServiceStack.CacheAccess;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.Text.Common;

namespace ServiceStack.ServiceInterface
{
public class SessionFactory : ISessionFactory
{
private readonly ICacheClient cacheClient;

public SessionFactory(ICacheClient cacheClient)
{
this.cacheClient = cacheClient;
}

public class SessionCacheClient : ISession
{
private readonly ICacheClient cacheClient;
private string prefixNs;

public SessionCacheClient(ICacheClient cacheClient, string sessionId)
{
this.cacheClient = cacheClient;
this.prefixNs = "sess:" + sessionId + ":";
}

public object this[string key]
{
get
{
return cacheClient.Get<object>(this.prefixNs + key);
}
set
{
JsWriter.WriteDynamic(() =>
cacheClient.Set(this.prefixNs + key, value));
}
}

public void Set<T>(string key, T value)
{
cacheClient.Set(this.prefixNs + key, value);
}

public T Get<T>(string key)
{
return cacheClient.Get<T>(this.prefixNs + key);
}
}

public ISession GetOrCreateSession(IHttpRequest httpReq, IHttpResponse httpRes)
{
var sessionId = httpReq.GetCookieValue(SessionFeature.PermanentSessionId)
?? httpRes.CreatePermanentSessionId(httpReq);

return new SessionCacheClient(cacheClient, sessionId);
}
}
}
25 changes: 19 additions & 6 deletions src/ServiceStack.ServiceInterface/SessionFeature.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,15 +21,11 @@ public static void Init(IAppHost appHost)
appHost.RequestFilters.Add((req, res, dto) => { appHost.RequestFilters.Add((req, res, dto) => {
if (req.GetCookieValue(SessionId) == null) if (req.GetCookieValue(SessionId) == null)
{ {
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); res.CreateTemporarySessionId(req);
res.SetSessionCookie(SessionId, sessionId);
req.Items[SessionId] = sessionId;
} }
if (req.GetCookieValue(PermanentSessionId) == null) if (req.GetCookieValue(PermanentSessionId) == null)
{ {
var permanentId = Convert.ToBase64String(Guid.NewGuid().ToByteArray()); res.CreatePermanentSessionId(req);
res.SetPermanentCookie(PermanentSessionId, permanentId);
req.Items[PermanentSessionId] = permanentId;
} }
}); });
} }
Expand All @@ -43,5 +39,22 @@ public static string GetTemporarySessionId(this IHttpRequest httpReq)
{ {
return httpReq.GetItemOrCookie(SessionId); return httpReq.GetItemOrCookie(SessionId);
} }

public static string CreatePermanentSessionId(this IHttpResponse res, IHttpRequest req)
{
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetPermanentCookie(PermanentSessionId, sessionId);
req.Items[PermanentSessionId] = sessionId;
return sessionId;
}

public static string CreateTemporarySessionId(this IHttpResponse res, IHttpRequest req)
{
var sessionId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
res.SetSessionCookie(SessionId, sessionId);
req.Items[SessionId] = sessionId;
return sessionId;
}

} }
} }
5 changes: 5 additions & 0 deletions src/ServiceStack.ServiceInterface/Testing/MockHttpRequest.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public object OriginalRequest
get { return null; } get { return null; }
} }


public T TryResolve<T>()
{
throw new NotImplementedException();
}

public string OperationName { get; set; } public string OperationName { get; set; }
public string ContentType { get; set; } public string ContentType { get; set; }
public string HttpMethod { get; set; } public string HttpMethod { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceStack.ServiceInterface/Testing/TestBase.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public object ExecutePath(
); );


var request = httpHandler.CreateRequest(httpReq, httpHandler.RequestName); var request = httpHandler.CreateRequest(httpReq, httpHandler.RequestName);
var response = httpHandler.GetResponse(httpReq, request); var response = httpHandler.GetResponse(httpReq, null, request);


var httpRes = response as IHttpResult; var httpRes = response as IHttpResult;
if (httpRes != null) if (httpRes != null)
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceStack/Properties/AssemblyInfo.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.1.3.*")] [assembly: AssemblyVersion("3.1.6.*")]
//[assembly: AssemblyFileVersion("1.0.*")] //[assembly: AssemblyFileVersion("1.0.*")]
12 changes: 8 additions & 4 deletions src/ServiceStack/ServiceHost/HttpRequestContext.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class HttpRequestContext
: IRequestContext : IRequestContext
{ {
private readonly IHttpRequest httpReq; private readonly IHttpRequest httpReq;
private readonly IHttpResponse httpRes;


public HttpRequestContext(object dto) public HttpRequestContext(object dto)
: this(dto, null) : this(dto, null)
Expand All @@ -22,15 +23,16 @@ public HttpRequestContext(object dto, EndpointAttributes endpointAttributes)
{ {
} }


public HttpRequestContext(IHttpRequest httpReq, object dto) public HttpRequestContext(IHttpRequest httpReq, IHttpResponse httpRes, object dto)
: this(httpReq, dto, EndpointAttributes.None) : this(httpReq, httpRes, dto, EndpointAttributes.None)
{ {
} }


public HttpRequestContext(IHttpRequest httpReq, object dto, EndpointAttributes endpointAttributes) public HttpRequestContext(IHttpRequest httpReq, IHttpResponse httpRes, object dto, EndpointAttributes endpointAttributes)
: this(dto, endpointAttributes, null) : this(dto, endpointAttributes, null)
{ {
this.httpReq = httpReq; this.httpReq = httpReq;
this.httpRes = httpRes;
if (this.httpReq != null) if (this.httpReq != null)
{ {
this.Files = httpReq.Files; this.Files = httpReq.Files;
Expand Down Expand Up @@ -83,7 +85,9 @@ public string ResponseContentType
public T Get<T>() where T : class public T Get<T>() where T : class
{ {
if (typeof(T) == typeof(IHttpRequest)) if (typeof(T) == typeof(IHttpRequest))
return (T) this.httpReq; return (T)this.httpReq;
if (typeof(T) == typeof(IHttpResponse))
return (T)this.httpRes;


var isDto = this.Dto as T; var isDto = this.Dto as T;
return isDto ?? (this.Factory != null ? this.Factory.Resolve<T>() : null); return isDto ?? (this.Factory != null ? this.Factory.Resolve<T>() : null);
Expand Down
4 changes: 2 additions & 2 deletions src/ServiceStack/WebHost.EndPoints/EndpointHost.cs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -188,12 +188,12 @@ public static void SetOperationTypes(ServiceOperations operationTypes, ServiceOp
AllServiceOperations = allOperationTypes; AllServiceOperations = allOperationTypes;
} }


internal static object ExecuteService(object request, EndpointAttributes endpointAttributes, IHttpRequest httpReq) internal static object ExecuteService(object request, EndpointAttributes endpointAttributes, IHttpRequest httpReq, IHttpResponse httpRes)
{ {
using (Profiler.Current.Step("Execute Service")) using (Profiler.Current.Step("Execute Service"))
{ {
return config.ServiceController.Execute(request, return config.ServiceController.Execute(request,
new HttpRequestContext(httpReq, request, endpointAttributes)); new HttpRequestContext(httpReq, httpRes, request, endpointAttributes));
} }
} }


Expand Down
Loading

0 comments on commit 490cbff

Please sign in to comment.