[Azure services platform step by step-12th] implement windows azure chat room-use table Storage

Source: Internet
Author: User

In [azure services platform step by step-9th] windows azure storage overview, we have discussed the role and features of table Storage. This article uses a simple chat room as an example to demonstrate how to useCode, Directly store the C # entity class (entity) into Table storage, and never say goodbye to SQL Server 200x and ORM tools.

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

 

First, let's review the table storage structure.

Each row is an independent entity.

The partition key and rowkey play a role similar to the primary key. They are used to identify the entity, and can also be used as the classification query condition in actual use.

The timestamp attribute (not drawn) is the default value for each row. It automatically records the last update time of the row.

 

Next, let's take a look at how tablestorage is used in storageclient.

 

Looking at this picture, looking at the file name alone, is it strange? Blob and queue both use rest, but table does not have a corresponding rest class. So how does it work?

ViewSource codeIt can be found that it used the dataservicequery and dataservicecontext in system. Data. Services. Client to implement the two key classes of ADO. NET data service. For the tablestoragedataservicecontext class, it inherits from dataservicecontext, or it encapsulates dataservicecontext into azure!

(Do not know about ADO. NET data service client friends please refer to the http://msdn.microsoft.com/zh-cn/library/system.data.services.client.dataservicecontext.aspx)

Well, no matter what, we can use it easily.

After understanding the principles, let's get started.

 

Step 1:

Create a web cloud service in vs008, configure serviceconfiguration. cscfg, servicedefinition. csdef, and add references to the storageclient project. We will not repeat it here. Please refer to the content of the previous article or the source code of the attachment at the end of this article.

Directly use serviceconfiguration. cscfg and servicedefinition. csdef in the previous section, because the account information is the same.

 

Step 2:

Drag the control to create a simple logon interface and a main chat interface. This is neither the key nor the difficulty. Please refer to the source code of the attachment at the end of this article. In fact, there is little difference between chatting rooms and leaving messages. Use the timer and updatepanel of ASP. NET ajax to refresh it every two seconds.

 

 

 

 

Step 3:

Creates a message object class.

Unlike traditional code or entity code generated by ORM tools, it must inherit from tablestorageentity.

  Public   Class Message: Microsoft. samples. servicehosting. storageclient. tablestorageentity
{
Public Message ()
{
Partitionkey =   " Chatroom001 " ;

// When an object is retrieved, the default sorting is based on the rowkey incrementing sequence.
Rowkey = (Datetime. maxvalue. ticks - Datetime. Now. ticks). tostring ();
}

Public   String Name {Get;Set;}
Public   String Body {Get;Set;}


// You do not need to define the message release time field,
// Because table storage has an automatic timestamp attribute, Timestamp can automatically record the data update time.
}

 

 

Step 4:

Create a messagedataservicecontext object class. This class is inherited from tablestoragedataservicecontext, that is, indirectly inherited from dataservicecontext. It is used to obtain a reference to the data service context. In addition, define a public attribute messages: to return all message objects; add addmessage method: to save the message object to table Storage.

 

  Public   Class Messagedataservicecontext: tablestoragedataservicecontext
{
Public Messagedataservicecontext (storageaccountinfo accountinfo)
: Base (Accountinfo)
{
}

// Defines the common attributes of messages and returns the message 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 store objects
This . Addobject ( " Messages " , New Message {Name=Name, body=Body} );

// Dataservicecontext class provides the savechanges () method to save modifications
This . Savechanges ();
}
}

 

Step 5:

Object retrieval:

 

// Initialize account information
Storageaccountinfo accountinfo = Storageaccountinfo. getaccountinfofromconfiguration ( " Tablestorageendpoint " );

// Automatically generates a table based on the structure of the object class
Tablestorage. createtablesfrommodel ( Typeof (Messagedataservicecontext), accountinfo );

// Get Data Service Context Reference
Messagedataservicecontext Context =   New Messagedataservicecontext (accountinfo );

// Take the first 150 message entities and bind them to messagelist as data sources.
Iqueryable < Message > Data = Context. Messages. Take ( 150 );

This . Messagelist. datasource = Data;

This . Messagelist. databind ();

 

Saved 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. If you want to look better,
// You can change the input parameters of this method to entities.
Context. addmessage ( This . Namebox. Text, This . MessageBox. Text );
}

 

OK!

F5, let's see how it works!

 

______ -------- ____ Uneasy split line ______--------______

Use the rest method to obtain the real appearance of an object:

We can clearly see that this entity has five attributes. Three of them are required by default. Only the body and name are defined in the object class.

 

[Attachment]

The legendary storageclient: storageclient.rar

Source code of this article: azurechatroom.rar (it has been integrated with the source code in the previous chapter)

Related Article

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.