In http://www.aliyun.com/zixun/aggregation/13357.html ">azure Web role, developers often face scenarios where the IIS server needs to be configured and optimized to implement Web Functions in role, such as turning off static file encryption (site compression), enforcing SSL access, and so on, The emergence of such scenarios has a common feature: the Web itself (including code and Web.config) does not meet the needs of developers, and developers must configure the Web server (IIS) to implement their desired web features.
In PAAs, user modifications to the Web role virtual machine via remote connections (RDP) can only take effect temporarily, and the virtual machine environment is restored to the newly released state after the Web role virtual machine has experienced a system update (once or two per month) of the cloud platform. That is, the user's settings through remote connections (such as IIS settings) will be lost!
Therefore, in the Azure Web role, developers who need to customize the IIS server must be implemented by code, and here's an example:
1. In the example Web role, the HTTP and HTTPS ports are turned on, and the Web site has a folder test, where all pages in the test directory are accessible only through HTTPS. Example Project:
2. Add code to the Webrole.cs file to implement SSL settings in IIS. The destination directory name needs to be specified here.
public override bool OnStart ()
{
By default, the website name is ' [Current role Instance Id]_web '
String sitename = RoleEnvironment.CurrentRoleInstance.Id + "_web";
The folder you want to expose it is only via HTTPS
String foldername = "Test";
string cmd = string. Format ("set config \" {0}/{1}/\ "/section:access/sslflags:ssl/commit:apphost", SiteName, FolderName);
String appcmdpath = @ "D:\Windows\System32\inetsrv\appcmd.exe";
Try
{
Process.Start (New ProcessStartInfo (Appcmdpath, cmd));
Trace.traceinformation ("Initialize IIS SSL succeed.");
System.IO.File.AppendAllText ("Log.txt", DateTime.Now.ToString () + "| Initialize IIS SSL succeed. ");
}
catch (Exception ex)
{
Trace.traceerror (ex. message);
System.IO.File.AppendAllText ("Log.txt", DateTime.Now.ToString () + "|" + ex. Message + "|" + cmd);
Throw;
}
Return base. OnStart ();
}
3. In the Servicedefinition file, locate the corresponding Web role and add the following settings.
<?xml version= "1.0" encoding= "Utf-8"?>
<servicedefinition name= "WindowsAzure4" xmlns= "http://schemas.microsoft.com/ServiceHosting/2008/10/ Servicedefinition "schemaversion=" 2013-03.2.0 ">
<webrole name= "WebRole1" Vmsize= "Sgt" >
<runtime executioncontext= "elevated"/>
<Sites>
<site name= "Web" >
<Bindings>
......
4. Deploy to cloud, effective.
Https://***.cloudapp.net/test/test1.aspx (Work)
Http://***.cloudapp.net/test/test1.aspx (does not work)
https://***.cloudapp.net/(Work)
http://***.cloudapp.net/(Work)
In Azure Web role, other IIS settings can be implemented in a similar way. The main idea is to call IIS's own Appcmd tool through code, and to set up the IIS server in a command-line fashion.
About the Appcmd tool: http://technet.microsoft.com/zh-cn/library/cc772200 (v=ws.10). aspx
Appcmd Tool sample 1--Set SSL access: http://technet.microsoft.com/en-us/library/cc753983 (v=ws.10). aspx
Appcmd Tool Example 2--set site compression:http://support.microsoft.com/kb/930909
The following code closes the site Compression,iis server in a similar manner, by default, site compression is turned on.
1. ADD code part to your Webrole.cs
public override bool OnStart ()
{
string cmd = string. Format ("Set Config/section:urlcompression/dostaticcompression:false");
String appcmdpath = @ "D:\Windows\System32\inetsrv\appcmd.exe";
Try
{
Process.Start (New ProcessStartInfo (Appcmdpath, cmd));
Trace.traceinformation ("Success:turn off compression for static content in IIS.");
System.IO.File.AppendAllText ("Log.txt", DateTime.Now.ToString () + "| Success:turn off compression for static content in IIS. ");
}
catch (Exception ex)
{
Trace.traceerror (ex. message);
System.IO.File.AppendAllText ("Log.txt", DateTime.Now.ToString () + "|" + ex. Message + "|" + cmd);
Throw;
}
Return base. OnStart ();
}
2. Do some change in service definition file:
<?xml version= "1.0" encoding= "Utf-8"?>
<servicedefinition name= "WindowsAzure7" xmlns= "http://schemas.microsoft.com/ServiceHosting/2008/10/ Servicedefinition "schemaversion=" 2014-01.2.3 ">
<webrole name= "WebRole1" Vmsize= "Sgt" >
<runtime executioncontext= "elevated"/>
<Sites>
<site name= "Web" >
......
Summary: In Azure Web role, if you need to set up an IIS server, you need to invoke Appcmd in code to achieve the server features of special requirements. Any manual modifications to a remote connection are undesirable and may be invalidated at any time.