Case Study of Two Role Development in Windows azure

Source: Internet
Author: User
Tags filetime
Windows azure provides a concept called role. Each role can be considered as a program. unlike an ordinary application, this program can run on one or more machines at the same time. Each role can have multiple instances, and each instance corresponds to a virtual machine. For the same role, all its instances execute the same program. There are two types of Role: worker role and web role ). 1:

 
Figure 1 web role and worker role

Web role: a web application. A Web role is a Web application running on IIS. It can communicate with the outside world through HTTP or HTTPS. Generally, a Web role responds to a request, execute an action and wait for the next request to arrive. As an application at the client display layer, this role handles all functions of interacting with the client. Therefore, it supports ASP. net web form, Asp. net MVC and applications based on the FastCGI core, such as PHP, JSP, or other CGI applications. However, Web role only supports HTTP and HTTPS channels. Therefore, to use web role development services, only ASP. NET Web Service (asmx service) or http-channel WCF Service can be used. The Web role application type that can be created in Figure 1 also describes this.

Worker role: a running on background application that runs. NET Framework code. Unlike web role, worker generally does not directly interact with users, but accesses any network resources and data sources in the background and performs operations. It does not open external access interfaces, but is executed in sequence without complaints after receiving the command (the message queue in the queue service can guide its work ), this is a bit similar to a Windows Service deployed on Windows Azure, and it can support HTTP/https or TCP communication modes, especially for service applications that do not limit HTTP Communication, such as a general WCF Service.

Generally, you only need web role to develop Windows azure applications. For the architecture and scalability of applications, you can use some architectures and designs to layer the functions of applications, it is necessary to allocate some logic processing of the application to the worker role, because the worker role will process the work in the background, and the Web role only needs to cope with frontend User Interface interaction, assigning Work to appropriate members can effectively improve the application execution efficiency and reduce the coupling during development.

This article uses the fulltrust example in Microsoft Windows azure code samples to give a simple demonstration. It makes you feel the joy of cloud computing development. Since it is used in existing cases, interested readers can go from http://code.msdn.microsoft.com/windowsazuresamples

Download the complete instance code. This example demonstrates two functions: calling the activity code from the Management Code through P/invoke, and running the command line script in a loop of sub-processes. Here we first give the running result, and then analyze the development process and core code. Run the project to automatically open a web page, as shown in Figure 2:

 
Figure 2 default page of Web role

On the default page of the Web role, click the refresh button to refresh the system time of the process. The excuteprocess. ASPX page is an ASP. NET page. As a sub-process, when the sub-process ends, It outputs information, as shown in 3:

 
Figure 3 Web role execution result

 

After seeing the running results of the Web role, we will focus on developing fabric. Here we will see the running results of the worker role. To view the result of development fabric, You can see development fabric in the notification area of the taskbar. Development fabric is a simulation environment where role instances are simulated on the local machine. Select 4 in the tool button on the left.

 
Figure 4 select development Fabric

Select Show development fabric. Page 5 is displayed:

 
Figure 5 fabric web role

In Figure 5, we can see that the entrypoint of Web role is the onstart method, and the run method is also called. The running result 6 of the worker role is as follows:

 
Figure 6 fabric worker role

In worker role, you can see that the process is called, helloword. CMD is executed, and the refresh system time is displayed.

Next we will introduce the development process and core code.

  Step 1: Create a project

Open vs 2010 to create a cloud computing project fulltrust. On the page shown in figure 1, select Web role and worker role, and rename them to fulltrust_webrole and fulltrust_workerrole. When renaming a role, you can select the role type to the cloud service solution and right-click it to rename it. Of course, you can also rename it in the project, the project name is changed, but the folder Name of the files stored on the local disk is not changed.

After the project is created, add the default and executeprocess pages and the helloworld. CMD script to the fulltrust_webrole project. Copy the helloworld. CMD script file to fulltrust_workerrole.

In this way, the project creation work has come to an end.

  Step 2: Compile the Web role function

In fulltrust_webrole, click the button on the default page to display the refresh time. The core code is as follows:

Button Event code:

Protected void button#click (Object sender, eventargs E)

{

Updateprocessinfo ();

}

Method for updating process information:

Protected void updateprocessinfo ()

{

......

If (getprocesstimes (process. getcurrentprocess (). handle,

Out createdtime,

Out exitedtime,

Out kerneltime,

Out usertime ))

......

}

The getprocesstimes method is a system function used to obtain the process time.

In executeprocess. aspx, there is a key sentence:

<PRE> <% run ("~ /Helloworld. cmd "," "); %> </PRE>

This statement calls the run function of executeprocess. cs. The Code is as follows:

Protected int run (

String export path,

String arguments)

{

VaR process = new process ();

VaR startinfo = process. startinfo;

......

Process. errordatareceived + =

(Sender, EVT) =>

{

Response. Write ("");

Writeline (EVT. data );

Response. Write ("");

};

Process. outputdatareceived + =

(Sender, EVT) =>

{

Writeline (EVT. data );

};

Writeline ("executing:" + path. getfilename (startinfo. filename ));

Writeline ("=============== output ================================= ");

Process. Start ();

Process. beginerrorreadline ();

Process. beginoutputreadline ();

Process. waitforexit ();

Writeline ("========================================== ======= ");

VaR elapasedtime = process. exittime-process. starttime;

Writeline ("Exit code: {0}", process. exitcode );

Writeline ("elapsed time: {0}", elapasedtime );

Return process. exitcode;

}

This function calls the sub-process and outputs the script results of the named row. The content of helloworld. CMD is as follows:

@ Echo off

Echo Hello world!

Exit/B 0

  Step 3: Compile the worker role function

In worker role, you can also call helloworld. CMD, put executeprocess. CS's run function is copied. In fact, here we can strip some common code and put it in a public class library for convenient calling. Every worker role inherits roleentrypoint. In workerrole. CS, you must call the run function to run helloworld. cmd.

At the same time, we also need to record the process logs to view the call status. The Code is as follows:

Protected void logprocesstimes (Process)

{

System. runtime. interopservices. comtypes. filetime createdtime;

System. runtime. interopservices. comtypes. filetime exitedtime;

System. runtime. interopservices. comtypes. filetime kerneltime;

System. runtime. interopservices. comtypes. filetime usertime;

If (getprocesstimes (process. handle,

Out createdtime,

Out exitedtime,

Out kerneltime,

Out usertime ))

{

Writeline ("Real Time: {0}", filetimetodatetime (exitedtime)-filetimetodatetime (createdtime ));

Writeline ("user time: {0}", filetimetotimespan (usertime ));

Writeline ("system time: {0}", filetimetotimespan (kerneltime ));

}

}

In the run function, this function is called after the process ends and the call time information is recorded in the log. Therefore, we can see the recorded log information in figure 6.

  Step 4: Compile and debug

After all the code is compiled, compile the project and use the development fabric to simulate the real environment for debugging and running. Because we didn't set service information, we didn't set serviceconfiguration. cscfg and servicedefinition. csdef files. This needs to be determined based on the specific project.

To debug and run the task, you must first set the fulltrust project with the cloud map as the startup project. Set deault. aspx to the start page. If the program is compiled once, press Ctrl + F5 or debug | if the program starts running without debugging, the project can be automatically packaged and simulated. The interface is 2 ~ 6.

In real scenarios, after debugging and running, you need to deploy the project. For details about the deployment process and steps, refer to the Chinese cloud computing blog of Microsoft.

In this example, we only briefly introduce how to develop two types of role applications, and there are more complex applications to use these two roles. In the development process, we need to differentiate web role and worker role functions. Some application types sometimes make developers have to use worker role for processing. The most typical example is a queuing application (such as an online ticket booking system ), web role cannot be executed in queue mode, and this type of application requires queue to handle the requirements, and Web role cannot be developed at a scheduled time (scheduled, for example, every minute or every five minutes) applications that extract data processing from queue, so worker role is the best type for this type of applications.

Visual Studio Tools for Windows azure's cloud application project allows you to deploy multiple Windows azure application types at a time (up to five different roles), but note that, each application type is mounted to an independent virtual machine, and the billing of computing resources (which will be introduced later) is measured in virtual machines, therefore, do not package applications on the cloud if they are not necessarily deployed. If you only want to test it, we recommend that you first test it on the local machine (resources can be connected to Windows azure) and then deploy it on the cloud.

  Summary

This article introduces two roles in Windows azure: Web role and worker role. Almost all application development is included in these two roles. Web role applications are similar to ASP.. NET application. Worker role is similar to Windows/web service. Through a fulltrust case study, I believe that you have understood the functions and development process of these two roles.

 

 

It168: http://tech.it168.com/a2010/0612/1066/000001066069_all.shtml

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.