2

I'm working on implementing a custom session state provider for my ASP.NET MVC application. My requirement is I've to store the session data as xml in a custom table in Sql server.

Can I use the existing sql session state provider (overriding some methods) or I've to create from scratch by implementing the abstract class SessionStateStoreProviderBase?

VJAI
  • 29,899
  • 20
  • 93
  • 155

2 Answers2

5

yes you can customize your class provider Session even with sql server or oracle. Just inherit from a class in your model inheriting from SessionStateStoreProviderBase and implementing the required methods that he sends, check the list of required methods here.

If you want to use an example, see here. This example using odbc but simply replace for access class as OdbcConnection to SqlConnection and vice versa.

Good luck.

Fernando Arce
  • 302
  • 2
  • 12
-3

Why aren't you just using SQL server as your state provider? You can set it in the config and this happens automatically, then SQL server will store the data as serialized binary data and efficiently retrieve it?

The short answer is yes, you can, but it's re-inventing the wheel. Do you need to use the data for anything else or edit it yourself out of process? I'd be inclined to use a seperate process for this. You're going to create a bit of work for yourself doing this and you would be better to just save the xml property somwhere when you set it in sessiopn if you need to look at it later.

Make your xml document a session object

Session["MyCustomXml"] = mydoc;

var mydoc = Session["MyCustomXml"] as XmlDocument;

then use the following config so it's stored in sql server.

<sessionState 
            mode="SQLServer"
            sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
            cookieless="false" 
            timeout="20" 
    />

If you need to look at later, just save it to disk somwhere safely with the SessionId as the filename to keep it unique.

Gats
  • 3,392
  • 17
  • 19
  • you do not need to use session state, you can do it stateless. There is no need to be using a heavy session state provider. – PositiveGuy May 24 '12 at 05:16
  • A sql server session state provider is no heavier than storing session by secure session Id by yourself.. which was the question. – Gats May 25 '12 at 01:25