"Azure Services Platform Step by step-12th" implement Windows Azure Chat room-use table Storage

Source: Internet
Author: User
Keywords Chat room implementation we name
Tags .net account information chat room class clear click cloud code
In the "Azure Services Platform Step by step-9th" Windows Azure Storage Overview, we have discussed the role and characteristics of table storage. This article will take the example of building a simple chat room, demonstrating that if you use the simplest code, the C # entity class (Entity) is stored directly into table storage, completely leaving the SQL Server 200x and ORM Tools.

Final effect: (Demo:http://ibm.cloudapp.net/chatmain.aspx deployed to the cloud)

The picture is not clear? Please click here to view the original image (larger).

Let's review the structure of table storage first.

The picture is not clear? Please click here to view the original image (larger).

Each line is an independent entity.

Partition Key and Rowkey play a role similar to primary keys, which are used to identify entity, and can be used as categorical query criteria.

The timestamp property (not shown above) is the default for each row, and it automatically records when the row's data was last updated.

Let's look at how the Tablestorage is used in storageclient.

Look at this picture, look at the file name, feel very strange? Both the blob and the queue are implemented using rest, except that the table does not have a class that corresponds to rest. So how does it work?

Looking at the source, it turns out that it was implemented using the key classes of the two Ado.net data services in System.Data.Services.Client DataServiceQuery and DataServiceContext. Taking the Tablestoragedataservicecontext class, it inherits from DataServiceContext, or, it encapsulates DataServiceContext as an azure version!

(Consult http://msdn.microsoft.com/zh-cn/library/system.data.services.client.dataservicecontext.aspx for friends who do not know ado.net data service)

Oh, no matter what, we use the convenience is good.

Understanding the principle, let's get to the point.

First step:

In VS008, create a new Web Cloud Service, configure Serviceconfiguration.cscfg, Servicedefinition.csdef, and add a reference to the Storageclient project. This is not repeated here, please refer directly to the previous article or the end of this article attachment source code.

Use the serviceconfiguration.cscfg and Servicedefinition.csdef in the previous section directly, because the account information is the same.

 

Step Two:

Drag into the control, make a simple login interface and the main chat interface. This is not the focus is not a difficult point, please refer to the end of this article Annex source code. In fact, the difference between chat room and message bar is small, using asp.net ajax timer and UpdatePanel Let it refresh every two seconds on the line.

The picture is not clear? Please click here to view the original image (larger).

Step Three:

Create a message entity class.

Unlike traditional code or the entity code generated by the ORM tool, it needs to inherit from Tablestorageentity.

public class Message:Microsoft.Samples.ServiceHosting.StorageClient.TableStorageEntity
{
Public message ()
{
Partitionkey = "ChatRoom001";
           
When you take an entity, the default sort is based on the Rowkey increment
Rowkey = (datetime.maxvalue.ticks-datetime.now.ticks). ToString ();
}

public string Name {get; set;}
public string Body {get; set;}
       

You don't have to define the "Message Publishing Time" field,
Because table storage has an automatic timestamp property timestamp can automatically record data update times.
}

Fourth Step:

Create a Messagedataservicecontext entity class. This class inherits from Tablestoragedataservicecontext, which is indirectly inherited from DataServiceContext. Its role is to obtain a reference to the data service context. In addition, define a public property messages: You can return all message type entities, and add AddMessage methods: The message entity is saved to table Storage.

public class Messagedataservicecontext:tablestoragedataservicecontext
{
Public Messagedataservicecontext (Storageaccountinfo accountinfo)
: Base (AccountInfo)
{
}

Defines a public property messages that returns the message class entity in all data service contexts.
Public iqueryable<message> Messages
{
Get
{
return this. Createquery<message> ("Messages");
}
}

public void AddMessage (string name, String body)
{
Use the AddObject method provided by the DataServiceContext class to deposit the entity
This. AddObject ("Messages", new message {name = name, BODY = body});

The SaveChanges () method provided by the DataServiceContext class to save the modification
This. SaveChanges ();
}
}

Fifth Step:

Take entity:

Initializing account information
Storageaccountinfo AccountInfo = storageaccountinfo.getaccountinfofromconfiguration ("TableStorageEndpoint");

Automatically generate table based on the structure of entity classes
Tablestorage.createtablesfrommodel (typeof (Messagedataservicecontext), accountinfo);

Get a reference to a data service context
Messagedataservicecontext context = new Messagedataservicecontext (accountinfo);

Take the first 150 message entities and bind to messagelist as the data source.
iqueryable<message> data = context. Messages.take (150);
            
This.messageList.DataSource = data;
             
This.messageList.DataBind ();

Deposit in Entity:

protected void SubmitButton_Click (object sender, EventArgs e)
{
Storageaccountinfo AccountInfo = storageaccountinfo.getaccountinfofromconfiguration ("TableStorageEndpoint");
Messagedataservicecontext context = new Messagedataservicecontext (accountinfo);
           
Call the AddMessage method we just defined. Actually, if you want to look better,
You can change the parameter of this method to an entity:)
Context. AddMessage (This.nameBox.Text, This.messageBox.Text);
}

OK, it's done!

F5 to see the effect of the operation!

To obtain the true appearance of an entity through rest methods:

It is clear to see that this entity has 5 properties. There are 3 attributes that are required by default, and only the body and name are defined by ourselves in the entity class.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.