Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top