DynamoDB Session State in ASP.NET MVC

1.3k Views Asked by At

I have create a new ASP.NET MVC 5 Project. I have installed through Nuget the AWS SDK for .NET and Session Provider and I have read this article in Amazon: Article

I have this configuration in the Web.Config

 <sessionState
  mode="Custom"
  customProvider="DynamoDBSessionStoreProvider">
  <providers>
    <add name="DynamoDBSessionStoreProvider"
      type="Amazon.SessionProvider.DynamoDBSessionStateStore, AWS.SessionProvider"
      AWSProfileName="default"
      Table="ASP.NET_SessionState"
      Region="eu-west-1"
      />
  </providers>
</sessionState>

I run the web app using the IIS Express and all works fine (I can login and logoff), but if I access to my DynamoDB I don't have any item in the table ASP.NET_SessionState.

It's working like the custom state provider is ignored...

What am I doing wrong?

Thanks!!

3

There are 3 best solutions below

0
On

This is what i have done

  • First need to login to amazon console and search for DynamoDB.
  • Then create a table with name ASP.NET_SessionState with key as SessionId (string), you need to set ReadCapacity to 3 and WriteCapacity to 1.
  • In Project Install packages AmazonSDK.Core and AmazonSDK.DynamoDBV2

In the web config in the system.web section

 <sessionState timeout="20"
   mode="Custom"
   customProvider="DynamoDBSessionStoreProvider">
     <providers>
      <add name="DynamoDBSessionStoreProvider"
      type="Amazon.SessionProvider.DynamoDBSessionStateStore, AWS.SessionProvider"
      AWSAccessKey="{Access key}"
      AWSSecretKey="{Sectret key}"
      Table="ASP.NET_SessionState"
      Region="us-east-1"  
      ReadCapacityUnits="3"
      WriteCapacityUnits="1"/>
  </providers>
</sessionState>

Then in the model you have give serializable Attribute. Some thing like below

[Serializable]
public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Then in the code you need to set session, something like this

TestModel testModel = new TestModel();
testModel.Id = 1;
testModel.Name = "ItemName";

// When we add this to session an entry will be written to DynamoDB Asp.NetSessionTable
Session["Checkout"] = testModel;

No need to worry about connection amazon sdk will take care of it. You need to mention the correct Access key and Secret Key.

0
On

Are you storing anything in the session? If you are not you there is nothing to store and there will be no records in DynamoDB.

If you want to check that you have it setup correctly, run a test that adds some data into the session and then check DynamoDB. You should then see records there.

0
On

Using this in EC2 instance you should set up an IAM role for DynamoDB access. At the bottom of the article there is information on the role. http://aws.amazon.com/iam/ Then there should be no need for credentialing as it is handled in the VPC of your EC2 instance. Also make sure the sessionState tag is inside of the tag in your web.config.