Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design of a web application with ServiceStack

After getting an advice about using ServiceStack for my asp.net MVC website (Maintaining state in Asp.Net MVC website), I started implementing it in my project - but some stuff is still unclear for me.

Currently I have two projects: one is the asp.net MVC project and the other is the BL project (a class library that holds all the business logic). All controllers in the MVC project make calls to classes/functions in the BL project.
For now, the mvc project loads the BL's DLL, but in the future when the website will grow, the BL's project will run on separate machines.

I would like to use ServiceStack for session management/caching and authentication (which both of them usually depended on each other).

My questions:
1) Is it possible to use only these two features without the functionality of message based web service? ServiceStack need to be initialized, and it throws me an error when initialized twice (in both projects).
2) Is it possible to split the implementation of ServiceStack between the two projects? I would like to maintain the process of authentication in the BL project using the ServiceStack's authentication providers, but handle all the UI/cookies by myself (or with the help of ServiceStack) in the mvc project.
3) I would like to use ServiceStack's caching in the BL project, but I guess that I still need to maintain some session cookies to receive the session id. What is the right way to do it? Are there any built-in helper functions for this purpose?

Thanks in advance!

like image 292
Edi Avatar asked Feb 16 '13 13:02

Edi


People also ask

What is ServiceStack framework?

ServiceStack is a configuration free, code-first, light-weight framework built on top of ASP.NET for building services and web applications. As the name suggests, it is a stack of services. It provides with just everything that one needs for building end-to-end web services.

Can we develop web application using .NET core?

The "Create a new project" dialog box includes different . NET Core 3.0 application templates. Each will create predefined project files and folders depends on the application type. Here we will create a simple web application, so select ASP.NET Core Web Application template and click Next, as shown below.


2 Answers

1) Is it possible to use only these two features without the functionality of message based web service? ServiceStack need to be initialized, and it throws me an error when initialized twice (in both projects).

If you install the latest ServiceStack.Mvc NuGet package you will get the base ServiceStackController which is an MVC Controller providing convenient access to ServiceStack's built-in providers. Although you still need to auto-wire your controllers with the dependencies it needs, e.g. an injected ICacheClient.

Although even if you're not using ServiceStack's WebFramework, having an AppHost is a convenient place to register your dependencies. ServiceStack is triggered by ASP.NET's IHttpHandler mappings specified in the Web.config, so if you don't have any mappings specified ServiceStack is never able to be called externally, but the registered dependencies are still able to be accessed internally with:

var cache = AppHost.Resolve<ICacheClient>(); //Get ICacheClient for SS IOC

2) Is it possible to split the implementation of ServiceStack between the two projects?

If you do have an AppHost, you cannot have more than one instance in a host project (by design) since an AppHost should be analogous to a host project where all your service dependencies should be registered and settings configured that apply to your entire web application or service.

You can however split the implementation of your services across multiple assemblies and have ServiceStack scan them all by specifying them in your AppHostBase constructor, e.g:

public class AppHost : AppHostBase 
{
   public AppHost() : base("My Service", 
     typeof(AServiceInDll1).Assembly, typeof(AServiceInDll2).Assembly/*, etc.*/){}
}

2) cont. I would like to maintain the process of authentication in the BL project using the ServiceStack's authentication providers, but handle all the UI/cookies by myself (or with the help of ServiceStack) in the mvc project.

Look at the ServiceStack.UseCases CustomAuthenticationMvc example project for an example of using MVC but authenticating with ServiceStack.

3) I would like to use ServiceStack's caching in the BL project, but I guess that I still need to maintain some session cookies to receive the session id. What is the right way to do it? Are there any built-in helper functions for this purpose?

You can use any of ServiceStack's Caching providers just like any other C# class, i.e. have your Business Logic binded to ICacheClient and inject the concrete implementation in your IOC.

For sessions you can use the base.SessionAs<T> method in the ServiceStack.Mvc ServiceStackController to access the session. To Save back the session you can use the IHttpRequest.SaveSession() extension methods. Although both these methods require the ASP.NET context (it uses ASP.NET's HttpContext singleton if not provided) to work since it relies on ServiceStack's ss-id/ss-pid cookies that are automatically instructed to be added on the client (by the server) whenever you access the Session.

If you don't want your business logic services to have a dependency on ASP.NET's System.Web I recommend accessing and saving the session to be done in your controllers and passed to your business logic.

I recommend reading the Sessions Wiki Page for more background info on how ServiceStack's sessions work.

Integration of ASP.NET Context between ServiceStack and ASP.NET or MVC

I'll add this info since it's useful for anyone doing advanced integration between ServiceStack and ASP.NET or MVC as some of ServiceStack's extension methods rely on these built-in types.

You can create a ServiceStack IHttpRequest or IHttpResponse (within any HTTP/Controller request) with:

var ssHttpRequest = System.Web.HttpContext.Current.Request.ToRequest();
var ssHttpResponse = System.Web.HttpContext.Current.Response.ToResponse(); 

Finally you can create a complete request context (that encapsulates both a IHttpRequest and IHttpResponse) with:

var ssRequestContext = System.Web.HttpContext.Current.ToRequestContext();
like image 157
mythz Avatar answered Oct 05 '22 20:10

mythz


Not sure I fully grasp your questions and how you would like to split the projects across multiple servers. I'll try my best to answer your questions...

Is it possible to use only these two features without the functionality of message based web service? ServiceStack need to be initialized, and it throws me an error when initialized twice

It seems like you're trying to run 2 instances of ServiceStack (maybe even 2 websites) within one solution (one in your web project and once in your BL layer). I don't think that's possible. Your BL layer can share ServiceStack libraries and you can configure (within AppHost.Configure method) those in your web project that references your BL project.

Is it possible to split the implementation of ServiceStack between the two projects?

I think the answer is yes, but you would have have one instance of ServiceStack used by both the projects. This would share the Session state across the projects. There might be a way to have two projects with there own instances of ServiceStack...see https://github.com/ServiceStack/ServiceStack/wiki/Self-hosting.

I would like to use ServiceStack's caching in the BL project, but I guess that I still need to maintain some session cookies to receive the session id. If ServiceStack is being used across both projects you can access all session data in UserSession (https://github.com/ServiceStack/ServiceStack/wiki/Sessions). If you MVC Controllers inherit from ServiceStackController you can use SessionFeature.GetSessionId() to get the session Id. In your ServiceStack Service (classes that implement Service) you can get the session data from using base.Session.

Hope this helps.

like image 25
paaschpa Avatar answered Oct 05 '22 22:10

paaschpa