2016-11-11 44 views
3

我正在创建用于检查许可证的服务。作为此服务的一部分,我们需要使用第三方DLL。此服务(WCF服务,C#)作为云服务在Azure上托管。部署完成后,我需要转到云服务的VM来手动注册该DLL,并为该服务提供IIS中使用第三方DLL的正确权限。如何在Azure云服务上使用第三方DLL

我想通过脚本/代码在IIS中执行此配置。因为如果cloudservces虚拟机重新启动以进行更新,配置将丢失并且必须再次手动设置。

DLL本身的注册由脚本(registerCOM.cmd)完成,并且是servicedefinition.csdef的一部分。

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

此脚本包含一个条目:

start "" "lpregister410.EXE" 

这个脚本启动一个可执行文件和注册的第三方DLL。到目前为止,一切正常

但我无法获得通过脚本消耗DLL的服务。这意味着我可以使服务工作,但我必须手动设置IIS。在这种情况下,我必须将应用程序池中的此应用程序的标识从“网络”设置为“本地系统”。我试过把这个选项创建成一个PowerShell脚本,然后由一个cmd(ConfigureIIS.cmd)脚本触发它:我不是一个IIS专家,但看到此选项多次选择并且解决方案有效,

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

这ConfigureIIS.cmd脚本包含:

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

PowerShell脚本做到这一点:

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 
    }  
} 

此脚本背后的想法是Azure为服务提供了随机Guid名称。这个Guid包含至少32个字符。如果找到,将identityType更改为0(LocalSytem)(仅托管一项服务)

好的,那么问题是什么。如果我在Cloudservices虚拟机上运行这个脚本手册,没问题。工作正常,IIS设置正确。但是当我运行发布时,IIS没有设置。 (我认为是因为该服务尚未添加到应用程序池中,这是一个正确的假设吗?)。当我火从应用程序的脚本,它告诉我,我没有足够的权限(我认为一个cloudservice是默认的“管理员”)

当我上面都做这样的代码:

{ 
       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 ! "); 
        }      
       }       
      } 

它给了我一个“没有足够的权限”保存修改时在

serverManager.CommitChanges(); 

所以有很多tekst和解释,遗憾的是的,但我希望有人可以给我在正确的方向上推。主要问题是如何在不与Cloud Service进行手动交互的情况下使用此第三方DLL。

回答

2

按照documentation

IS可能不完全过程中 启动过程的启动任务阶段配置的,所以角色特定的数据可能无法使用。启动 需要角色特定数据的任务应该使用Microsoft.WindowsAzure.ServiceRuntime.RoleEntryPoint.OnStart 。

如果OnStart方法不为你工作,尝试改变的ConfigureIIS.cmdbackground(它将异步运行)的taskType,并采用脚本等到应用程序池配置:

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

PowerShell脚本:

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 
    }  
} 
+0

你让我很快乐。它现在完美运作。我提供的代码是在RoleEntryPoint.OnStart方法中,但仍然出现错误。脚本和taskType中的更改确实有效!非常感谢你 ! – Martijn

+0

不客气:-) –