c# - 如何在Azure云服务上使用第3方DLL

标签 c# powershell azure iis dll

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

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

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

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

此脚本包含一个条目:

    start "" "lpregister410.EXE"

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

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

我不是 IIS 专家,但看到这个选项被选择了很多次并且解决方案有效,我尝试将其创建到 powershell 脚本中,然后由 cmd (ConfigureIIS.cmd) 脚本触发:

  <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 名称。该指南至少包含 32 个字符。如果找到,将identityType更改为0(LocalSytem)(仅托管一项服务)

好吧,那么问题出在哪里呢?如果我在 Cloudservices VM 上运行此脚本手册,没有问题。工作正常并且 IIS 设置正确。但是当我运行发布时,IIS 未设置。 (我认为因为该服务尚未添加到应用程序池中,所以这是一个正确的假设吗?)。当我从应用程序中启动脚本时,它告诉我我没有足够的权限(我认为云服务默认是“管理员”)

当我在这样的代码中完成上述所有操作时:

{
                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(); 

所以有很多技术和解释,对此感到抱歉,但我希望有人能够插入我朝正确的方向前进。主要问题是我如何使用这个第 3 方 DLL,而不需要与云服务进行手动交互。

最佳答案

根据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.

如果 OnStart 方法不适合您,请尝试将 ConfigureIIS.cmdtaskType 更改为 background (它将异步运行)并采用 等待应用程序池配置完成的脚本:

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

关于c# - 如何在Azure云服务上使用第3方DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40544527/

相关文章:

c# - 带有基于日期的子点的 WPF ItemsControl

windows - 以独立于语言环境的方式访问 Windows 性能计数器

powershell - 如何解决 Get-NetConnectionProfile : Provider load failure on x86 Powershell?

azure - 将私钥证书导入为可在 azure 应用服务中导出

c# - Azure SQL : How to determine if table was altered or created and by whom

c# - 为什么 .NET 5.0 和 C# 中的框架不返回相同的值

c# - 检测文本框 mvvm windows phone 8 中更改的文本?

c# - 如何更改 Crystal Reports 用户界面语言

.net - 如何在 PowerShell 5 中定义和使用自定义属性?

即使在 web.config 之后,Azure Web 应用程序也不会将 http 重定向到 https