Cloud service in Webrole is quite with our website, and Workrole quite with us wrote a Windows Service on the server, on the high availability point of view, Cloud service is better than website, Take an example, we upload a data, the data needs to be put into storage, usually website need to submit data, waiting for response, this time if the concurrency is large, the system response time will be very long. If you don't have this problem with cloud service, Webrole is only responsible for submitting the data. and the processing of data can be submitted to workrole to deal with, in the middle can use the service bus Message Queuing mechanism to carry out the information. I feel very good about myself.
Do not say so much nonsense, we actually do a webrole and workrole to complete a specific demo.
First, create our cloud service project.
And then select Add, the Webrole ASP. NET WebRole And Workrole worker role and service bus queue
Click Edit to add the WebRole1 and WorkerRoleWithSBQueue1 to modify the corresponding name.
Then determine, select Web Forms, and empty the solution template. If you have problems with authentication here, please refer to my second section.
Click the OK button to generate our solution.
Down we landed in our Windowsazure portal and clicked on the Service Bus menu to create our messaging service.
Select the service bus that we created to view its connection information.
And then we copy his connection string.
Down we open, we cloudservice the configuration file, put the connection string that connects the service bus into it.
Down we are in this configuration file, modify the contents of Webrole because webrole he needs to access service bus then we define its node here.
Down we modify the specific configuration file, but also the workrole in the Servicebus configuration, copied to Webrole.
Down to the connection string we just copied in the Windows Azure Portal and into the corresponding configuration.
Down and use the same method to formulate our third configuration file:
The relevant service bus in our cloud service is configured here.
Then we add a default page in Webrole, right-click:
Complete the following code
Default.aspx
<%@ page language= "C #" autoeventwireup= "true" codebehind= "Default.aspx.cs" inherits= "Demowebrole.default"%>
<! DOCTYPE html>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:textbox id= "Txbinput" runat= "Server" ></asp:TextBox>
<asp:button id= "Btnsend" runat= "Server" text= "send Message" onclick= "Btnsend_click"/>
<p/>
<asp:label id= "Label1" runat= "Server" text= "" ></asp:Label>
</div>
</form>
</body>
Default.aspx.cs
Using Microsoft.ServiceBus.Messaging;
Using Microsoft.windowsazure;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace Demowebrole
{
public partial class Default:System.Web.UI.Page
{
Const string queuename = "Demoqueue";
protected void Page_Load (object sender, EventArgs e)
{
}
protected void Btnsend_click (object sender, EventArgs e)
{
String strinput = TxbInput.Text.ToString ();
Send (strinput);
Txbinput.text = string. Empty;
}
private void Send (string text)
{
String connectionString = cloudconfigurationmanager.getsetting ("Microsoft.ServiceBus.ConnectionString");
Messagingfactory factory = messagingfactory.createfromconnectionstring (connectionString);
Initialize the connection to Service Bus Queue
Messagesender sender = Factory. Createmessagesender (QueueName);
Brokeredmessage message1 = new Brokeredmessage (text);
Sender. Send (MESSAGE1);
}
}
}
This time he will prompt, and service bus related components that are not referenced
This component is there, and we can see the references in Workrole to find it.
The address path is copied and then referenced in the webrole. There is no problem at this time.
Down we modify Workrole code, let Webrole pass over the content, output out:
WorkerRole.cs
Using System;
Using System.Collections.Generic;
Using System.Diagnostics;
Using System.Linq;
Using System.Net;
Using System.Threading;
Using Microsoft.servicebus;
Using Microsoft.ServiceBus.Messaging;
Using Microsoft.windowsazure;
Using Microsoft.WindowsAzure.ServiceRuntime;
Namespace Teacherworkerrole
{
public class Workerrole:roleentrypoint
{
Name of the queue
Const string queuename = "Teacherqueue";
Queueclient is thread-safe. It is recommended that you cache the
Instead of recreating it for every request
Queueclient Client;
ManualResetEvent completedevent = new ManualResetEvent (false);
public override void Run ()
{
Trace.WriteLine ("Starting processing messages");
The message pump is started, and the callback is invoked for each received message, and closing on the client stops the pump.
Client.onmessage ((receivedmessage) =
{
Try
{
Processing messages
Trace.WriteLine ("Processing Service Bus message:" + receivedMessage.SequenceNumber.ToString () + receivedmessage.getbody< String> ());
}
Catch
{
Handle any message handling a specific exception here
}
});
Completedevent.waitone ();
}
public override bool OnStart ()
{
Set maximum number of concurrent connections
Servicepointmanager.defaultconnectionlimit = 12;
If the queue does not exist, the queue is created
String connectionString = cloudconfigurationmanager.getsetting ("Microsoft.ServiceBus.ConnectionString");
var NamespaceManager = namespacemanager.createfromconnectionstring (connectionString);
if (!namespacemanager.queueexists (queuename))
{
Namespacemanager.createqueue (QueueName);
}
Initializing a connection to a Service Bus queue
Client = queueclient.createfromconnectionstring (connectionString, queuename);
Return base. OnStart ();
}
public override void OnStop ()
{
To close a connection to a Service Bus queue
Client.close ();
Completedevent.set ();
Base. OnStop ();
}
}
}
Well, the code is written and we're debugging. You can modify the number of instances in the configuration file, for example, I can set my webrole to 2 instances, WorkRole2 instances.
Two settings, 1 are directly formulated in the configuration file, the other is directly in the cloud service to select the corresponding Webrole and Workrole right-click Properties in the configuration, and then he automatically generated in the configuration file.
Let's go. Set up demo Cloud service using full emulator, the default emulator can only use single instance.
Then we run the demo Cloud service for full simulation testing
Open the simulation interface. We can test it.
Click to send a message,You can see one of themworkrole I've got the message I sent.
Hey, there is a sense of accomplishment!
Follow me to learn about Webrole and Workrole in Windows Azure four Cloud service, and the communication between them