Azure Study Notes: Cloud Service (4)

Source: Internet
Author: User

Cloud Service includes two types of roles: WebRole and WorkerRole. These Roles can be understood as different virtual machines. The only difference is that WebRole is used to host Web sites and can be understood as a virtual machine with IIS installed. WorkerRole can be used to host backend services, such as windows Services. Initially, we can use Cloud Service to quickly create commercial applications with N-tier racks, what are Layer 3, Layer 4, and so on. This article will introduce how to use Cloud Service to build a three-tier Web site. The functions of this site are very simple: 1) display records stored in Azure SQL Server on the web page, 2) Mesage occurs to the WorkerRole Handler through the Service Bus Queue, 3) workerRole receives the Message in the Queue and inserts the record stored in the Message into Azure SQL Server.Engineering code structure
WebRole and WorkerRole Engineering Structure



Program functions


WebRole implementation codeAspx

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">    


Aspx. cs
private void SendAddCustomerMsg(Hashtable item)        {            NamespaceManager nm = NamespaceManager.Create();            QueueClient client = QueueClient.Create("jeffqueue",ReceiveMode.ReceiveAndDelete);            BrokeredMessage msg = new BrokeredMessage(item);            try            {                 client.Send(msg);            }            catch            {                throw;            }            finally            {                client.Close();            }                    }
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)        {                        Hashtable item = new Hashtable();            item.Add("Id", e.Values["Id"]);            item.Add("Name", e.Values["Name"]);            item.Add("Email", e.Values["Email"]);            SendAddCustomerMsg(item);            e.Cancel = true;        }



WorkerRole implementation code
using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Data;using System.Data.Sql;using System.Data.SqlClient;using System.Linq;using System.Net;using System.Threading;using Microsoft.WindowsAzure;using Microsoft.WindowsAzure.Diagnostics;using Microsoft.WindowsAzure.ServiceRuntime;using Microsoft.WindowsAzure.Storage;using Microsoft.ServiceBus;using Microsoft.ServiceBus.Messaging;namespace gbworkerrole1{    public class WorkerRole : RoleEntryPoint    {        NamespaceManager nm;        QueueClient client;               public override void Run()        {            // This is a sample worker implementation. Replace with your logic.            Trace.TraceInformation("gbworkerrole1 entry point called", "Information");            while (true)            {                                    BrokeredMessage msg = client.Receive(TimeSpan.FromSeconds(5));                    if (msg != null)                    {                        SqlConnection conn = new SqlConnection("Data Source=gaalnvmd7w.database.windows.net;Initial Catalog=gbsql01;Persist Security Info=True;User ID=account;Password=pwd");                        SqlCommand cmd = new SqlCommand("insert into customers values(@id,@name,@email)", conn);                        Hashtable item = msg.GetBody<Hashtable>();                        cmd.Parameters.AddWithValue("@Id", item["Id"]);                        cmd.Parameters.AddWithValue("@Name", item["Name"]);                        cmd.Parameters.AddWithValue("@Email", item["Email"]);                        try                        {                            conn.Open();                            cmd.ExecuteNonQuery();                        }                        catch { }                        finally {                            if (conn.State == ConnectionState.Open)                            {                                conn.Close();                            }                        }                    }                           }        }        public override bool OnStart()        {            // Set the maximum number of concurrent connections             ServicePointManager.DefaultConnectionLimit = 12;            // For information on handling configuration changes            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.            nm = NamespaceManager.Create();            client = QueueClient.Create("jeffqueue", ReceiveMode.ReceiveAndDelete);            return base.OnStart();        }        public override void OnStop()        {            client.Close();                        base.OnStop();        }    }}




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.