如何在Azure云服务上使用第三方DLL

时间:2016-11-11 09:09:29

标签: c# powershell azure iis dll

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

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

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

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

此脚本包含一个条目:

    start "" "lpregister410.EXE"

此脚本启动可执行文件并注册第三方DLL。到目前为止一切正常

但我无法通过脚本获取服务DLL的服务。这意味着我可以使服务正常工作,但我必须手动设置IIS。在这种情况下,我必须在应用程序池中将此应用程序的标识从“网络”设置为“LocalSystem”。

我不是IIS专家,但是多次选择此选项并且解决方案有效,我尝试将其创建为powershell脚本,然后由cmd(ConfigureIIS.cmd)脚本触发:< / p>

  <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 VM上运行此脚本手册,没问题。工作正常,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和解释,对不起,但我希望有人可以给我一个正确的方向。主要问题是如何在不与云服务进行手动交互的情况下使用此第三方DLL。

1 个答案:

答案 0 :(得分:2)

根据documentation

  

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

如果OnStart方法对您不起作用,请尝试将taskType的{​​{1}}更改为ConfigureIIS.cmd(它将异步运行)并采用要等到配置应用程序池的脚本:

background

PowerShell脚本:

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