简体   繁体   中英

How to use 3rd party DLL on Azure Cloud Service

I'm creating a service for checking licenses. As part of this service we need to use a 3rd party DLL. This service (WCF service, C#) is hosted on azure as a Cloud service. After deployment i need to go to the Cloud service's VM to register the DLL manually and give the service the correct rights in IIS to use the 3rd party DLL.

I want to do this configuration in IIS by script/code. Because if the cloudservces VM restarts for updates, the configuration is lost and has to be set manually again.

The registration of the DLL itself is done by script (registerCOM.cmd), and is part of the servicedefinition.csdef.

<Startup>
  <Task commandLine="RegisterCOM.cmd" executionContext="elevated" taskType="simple" />      
</Startup>

This script contains one entry:

    start "" "lpregister410.EXE"

This script starts an executable and registers the 3rd party DLL. So far everything works

But I cannot get the service to consume the DLL by script. That means I can get the service working, but I have to set the IIS manually. I this case i have to set the Identity of this application in the application pool from "Network" to "LocalSystem".

I'm not an IIS expert, but seeing this option chosen many times and the solution works, I've tried to create this into a powershell script, which is then fired by a cmd (ConfigureIIS.cmd) script:

  <Startup>
  <Task commandLine="RegisterCOM.cmd" executionContext="elevated" taskType="simple" />
  <Task commandLine="ConfigureIIS.cmd" executionContext="elevated" taskType="simple" />
</Startup>

This ConfigureIIS.cmd script contains:

 PowerShell.exe -NoProfile -ExecutionPolicy Unrestricted  -Command "& '%~dpn0.ps1'"

The PowerShell script does this:

Import-Module WebAdministration
$applicationPools = Get-ChildItem IIS:\AppPools 

foreach($pool in $applicationPools)
{    
  If($pool.name.length -gt 32)
    {
        $pool.processModel.identityType = 0
        $pool | Set-Item
        $bool = $true
    }       
}

The idea behind this script is Azure gives random Guid name to the service. This Guid contains at least 32 chars. If found, change the identityType to 0 (LocalSytem) (Only one service is hosted)

Alright, so what is the problem. If I run this script manual on the Cloudservices VM, no problem. Works fine and IIS is correctly set. But when i run a publish, IIS is not set. (I think because the service is not yet added to the applicationpool, is that a correct assumption?). When I fire the script from the application it tells me i don't have sufficient rights (I thought that a cloudservice is by default "administrator")

When I do all above in code like this:

{
                ServerManager serverManager = new ServerManager();                                
                sb.Append("ServerManager Created");
                ApplicationPoolCollection applicationPools = serverManager.ApplicationPools;
                sb.Append("Number of pools detected: " + applicationPools.Count.ToString());          
                foreach (ApplicationPool pool in applicationPools)
                {                   
                    sb.Append(pool.Name);
                    if (pool.Name.Length > 32) {
                        sb.Append(pool.Name + " has been found");                        
                        pool.ProcessModel.IdentityType = ProcessModelIdentityType.LocalSystem;                       
                        sb.Append(pool.Name + " has identity been changed to " + pool.ProcessModel.IdentityType.ToString() + ", ");
                        sb.Append("Trying to commit changes, ");
                        serverManager.CommitChanges();
                        sb.Append("Changes Committed ! ");
                    }                       
                }                          
            }

It gives me a "not sufficient permissions" when saving changes at

serverManager.CommitChanges(); 

So there is a lot of tekst and explanation, sorry for that, but I hope that someone can give me a push in the right direction. The main question is how can I use this 3rd party DLL, without manual interaction with the Cloud service.

According to the documentation :

IS may not be fully configured during the startup task stage in the startup process, so role-specific data may not be available. Startup tasks that require role-specific data should use Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint.OnStart.

If the OnStart method doesn't work for you, try to change the taskType of the ConfigureIIS.cmd to background (it will run asynchronously) and adopt the script to wait until the application pools are configured:

 <Startup>
  <Task commandLine="RegisterCOM.cmd" executionContext="elevated" taskType="simple" />
  <Task commandLine="ConfigureIIS.cmd" executionContext="elevated" taskType="background" />
</Startup>

PowerShell script:

Import-Module WebAdministration
$applicationPools = Get-ChildItem IIS:\AppPools 
$yourExpectedAppliactionPoolCount = 1;

while ($applicationPools.Count -lt $yourExpectedAppliactionPoolCount)
{
    Sleep -Seconds 3
}

foreach($pool in $applicationPools)
{    
  If($pool.name.length -gt 32)
    {
        $pool.processModel.identityType = 0
        $pool | Set-Item
        $bool = $true
    }       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM