简体   繁体   English

将DLL移动到Azure Cloud Service部署包

[英]Move DLLs to Azure Cloud Service deployment package

Is there solid, no-fuss way to copy a file to an Azure cloud service deployment package without including or referencing it in the web project? 有没有可靠的方法将文件复制到Azure云服务部署包而不在Web项目中包含或引用它?

We have a site that we will be hosting both as a web role in an Azure cloud service project and on-premises, each requiring different libraries for the data access layer. 我们有一个站点,我们将作为Azure云服务项目和本地的Web角色托管,每个站点都需要不同的数据访问层库。 We could dump all the necessary files into the project and have them copied on build and then dynamically select which assemblies to load based on environmental variables, but that seems a little sloppy--and there may be legitimate reasons why we wouldn't want those libraries sitting on a server in a distant Azure region. 我们可以将所有必要的文件转储到项目中并将它们复制到构建中,然后根据环境变量动态选择要加载的程序集,但这看起来有点草率 - 可能有合理的原因我们不希望这些库坐在遥远的Azure区域的服务器上。 These assemblies are not referenced directly by the project, but are loaded at runtime using MEF or some similar mechanism. 这些程序集不是由项目直接引用,而是在运行时使用MEF或某种类似机制加载。

Here's a post-build script that we've tried: 这是我们尝试过的构建后脚本:

if "$(TargetProfile)" == "" (
     echo "OnPrem"
     xcopy /Y "$(ProjectDir)\lib\OnPrem\TestLibrary.dll" "$(ProjectDir)\$(OutputPath)\$(Configuration)"

) else (
     echo "Cloud"
     copy /Y "$(ProjectDir)\lib\Cloud\TestLibrary.dll" "$(ProjectDir)\$(OutputPath)\$(Configuration)"
)

This works, in as much as the file is copied as expected upon building and during the portion of the msbuild /t:publish step that builds the website project. 这可以在文件在构建时以及构建网站项目的msbuild /t:publish步骤的部分期间按预期复制。 However, the TestLibrary.dll file is not included in the cloud service package, not deployed, and fails to load at runtime. 但是,TestLibrary.dll文件未包含在云服务包中,未部署,并且无法在运行时加载。

Has something like this been done before with Azure deployments? Azure部署之前是否已完成此类操作? I will concede that it's possible we are approaching this the wrong way, but I haven't yet discovered an elegant approach the fits our needs. 我会承认,我们可能会以错误的方式接近这一点,但我还没有找到符合我们需求的优雅方法。

After playing around with this for some time, I think I've found a strategy that achieves more or less what I want. 在玩了一段时间之后,我想我已经找到了一个或多或少达到我想要的策略。 You can add content files to the CloudService Role that are packaged and deployed along with the cloud service like so: 您可以将内容文件添加到与云服务一起打包和部署的CloudService角色,如下所示:

  1. In Visual Studio, right click the WebRole inside the CloudService project and select "Add Existing Item" 在Visual Studio中,右键单击CloudService项目中的WebRole,然后选择“添加现有项”
  2. Browse and select the .dll you wish to add. 浏览并选择要添加的.dll。
  3. You may even create folders and such to organize these files. 您甚至可以创建文件夹等来组织这些文件。

These files are added to the approot in the .cspkg, and similarly end up in the approot folder when deployed. 这些文件将添加到.cspkg中的approot中,同样在部署时最终会在approot文件夹中。 To resolve this directory I did this: 要解析此目录,我执行了以下操作:

string appRoot = Environment.GetEnvironmentVariable("RoleRoot");
appRoot = Path.Combine(appRoot + @"\", @"approot\");

You'll still need a way to copy the on-premises .dlls for on-premises deployments, but we already have a method of doing that in our existing deployment process. 您仍然需要一种方法来复制本地部署的本地.dll,但我们已经有了在现有部署过程中执行此操作的方法。 Using configuration setting in CloudConfigurationManager you can tell the app how to resolve the directory where these .dlls ought to reside, eg: 使用CloudConfigurationManager配置设置,您可以告诉应用程序如何解析这些.dll应该驻留的目录,例如:

    var libDir = CloudConfigurationManager.GetSetting("Environment").Equals("Cloud") 
        ?
        Environment.GetEnvironmentVariable("RoleRoot")
            .Path.Combine(appRoot + @"\", @"approot\")
        :
        HostingEnvironment.MapPath("~\pathToOnPremDlls\")

It feels vaguely hack-ish, but it seems to work well enough. 它感觉模糊不清,但似乎运作得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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