.net - 什么是法师,它有什么用处?

标签 .net clickonce mage

我一直看到对 Mage 的引用,但我不明白它的具体作用以及它为什么有用/受欢迎。如果有人可以快速概述它,将不胜感激!

谢谢~

最佳答案

Mitch Wheat 给出了很好的答案,如果您刚开始使用 Mage,阅读他提供的最后一个链接!

我想分享一些真实的代码,为其他人的 ClickOnce 项目提供帮助。我发现使用 MSBUILD from the command-line to "create" the deployment非常适合自动化构建过程。我从不 从 Visual Studio 执行发布向导。虽然,我确实在 VS 中的“发布”选项卡上指定了几乎所有信息,因此我不必从命令行执行此操作。例如,“应用程序文件”是我不知道如何在命令行上执行的操作。

然后在应用程序成功部署到服务器之后......我使用“Mage”作为我将 ClickOnce 部署从一台服务器迁移到另一台服务器的一部分(例如测试-> 暂存-> 生产)

例如(构建从 CruiseControl 作业运行的解决方案的 PowerShell 脚本):

&"$Env:windir\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" "C:\Projects\MyCoolApp.sln" /t:clean /t:publish /p:Configuration=Release /p:ApplicationRevision=$Env:CCNETLABEL /p:PublishDir="\\TestServer\MyCoolAppFolder/" /p:PublishUrl="\\TestServer\MyCoolAppFolder/"

然后,当您想将 ClickOnce 应用程序从“TestServer”迁移到“QAServer”或“Staging”或“Production”时……您需要编写一个复杂的脚本来执行此操作。这是我想出的:
#########################################################################################
# PowerShell Script to Migrate a ClickOnce Deployment from one server to another.
# This is my first attempt at PowerShell... pardon the bad or incorrect code. :-)
# To run a PowerShell script from CruiseControl.Net:
# http://www.cruisecontrolnet.org/projects/ccnet/wiki/PowerShell_Task
# NOTE: When doing the initial build, ensure that the ProviderURL and ProviderDir are set.
#########################################################################################

$SourceDir = "\\TestServer\MyCoolAppFolder"
$DestDir = "\\StagingServer\MyCoolAppFolder"
$DeploymentManifestName = "MyCoolApp.application"
$DeploymentDestUrl = "file://StagingServer/MyCoolAppFolder"

# If your application is one that connects to a database, then likely you want it to point 
# to a different database depending what environment it's been deployed to.
# I use a SQL Server connection for this example.
$ConnStringName = "MyCoolAppConnectionString"
$ConnStringValue = "data source=StagingServerInstance;Initial Catalog=MyCoolAppDB;persist security info=True;user id=Gregg;password=Gregg"

# Unfortunately, you *must* specify the publisher when doing Mage, even though you specified it
# when you did the original publish, otherwise Mage will change the Publisher value to the 
# name of your Application. A bug in Mage I suspect.
$Publisher = "Gregg Cleland" 

# Risk: This next line assumes that the pfx certificate file is readily available.
# Just be certain it's the same key you used when you published originally.
$AuthenticationKeyPath = "C:\Projects\MyCoolApp\MyCoolApp_TemporaryKey.pfx"

# Note: This references the .NET 3.5 version of mage... the .NET 4.0 version of mage.exe can be found at:
# C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\mage.exe
$MAGE = "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\mage.exe"

#########################################################################################
# Start off at the source location.
Set-Location $SourceDir

#########################################################################################
# Get the application manifest directory name and application manifest file name.
[xml]$doc = Get-Content $DeploymentManifestName
$ns = New-Object Xml.XmlNamespaceManager $doc.NameTable
$ns.AddNamespace( "asmv1", "urn:schemas-microsoft-com:asm.v1" )
$ns.AddNamespace( "asmv2", "urn:schemas-microsoft-com:asm.v2" )
$xpath = "/asmv1:assembly/asmv2:dependency/asmv2:dependentAssembly" 
$appManifestPath = $doc.SelectSingleNode( $xpath, $ns ).codebase # Example: = "Application Files\MyCoolApp_1_0_0_5\MyApp.exe.manifest"
$position = $appManifestPath.LastIndexOf('\');
$appManifestDir = $appManifestPath.SubString(0, $position); # Example: "Application Files\MyCoolApp_1_0_0_5"
$appManifestFile = $appManifestPath.SubString($position + 1); # Example: "MyCoolApp.exe.manifest"

#########################################################################################
# Copy the deployment files and the latest application files to destination.
# Note: Do not forget to ensure the CruiseControl Service Logon has permissions to write to destination!
# Todo: If robocopy fails, throw "robocopy failed!" Most likely it is an Error 5, Access Denied 
# b/c the CruiseControl Service logon account doesn't have permission to copy to create/write to destination.
$CurrentDir = "$DestDir\$appManifestDir"
robocopy "$SourceDir" "$DestDir" /XO
robocopy "$SourceDir\$appManifestDir" $CurrentDir /MIR /XO

#########################################################################################
# Now that we have copied the latest build, let us navigate down into the destination's
# application manifest directory and do some work.
Set-Location $CurrentDir

#########################################################################################
# Remove the .deploy extension from all files. (Mage will throw an exception if you don't do this)
Get-ChildItem -Include *.deploy -Recurse | Rename-Item -NewName { [System.IO.Path]::ChangeExtension($_.Name, "") }

#########################################################################################
# Modify the XML in the app.config file per your needs (e.g. change the connectionStrings)
[xml]$doc = Get-Content $AppConfigFileName

$node = $doc.SelectSingleNode("configuration/connectionStrings/add[@name='$ConnStringName']")
$node.connectionString = $ConnStringValue

$xmldocPath = $PWD.ProviderPath # hack to avoid getting the silly namespace prefixed to the path for UNC paths
$doc.Save("$xmldocPath\$AppConfigFileName") # For some reason, seems to require the fully qualified path

#########################################################################################
# Finally... We get to the part where we use MAGE!
# Use MAGE to update the application manifest hash and sign it.
&"$MAGE" -Update $appManifestFile -FromDirectory "$CurrentDir" -CertFile $AuthenticationKeyPath

#########################################################################################
# Re-Add the ".deploy" extension to all files EXCEPT those that end in "application" or 
# "manifest". Do this AFTER signing.
Get-ChildItem -Recurse | Where-Object { !$_.PSIsContainer -and !$_.Name.EndsWith(".application") -and !$_.Name.EndsWith(".manifest") } | Rename-Item -NewName { $_.Name + ".deploy" }

#########################################################################################
# Finally, go back up to the Deployment folder and update the deployment manifest
Set-Location "..\..\"
&"$MAGE" -Update $DeploymentManifestName -ProviderUrl "$DeploymentDestUrl/$DeploymentManifestName" -AppManifest "$appManifestPath" -Publisher $Publisher -CertFile $AuthenticationKeyPath

关于.net - 什么是法师,它有什么用处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1480632/

相关文章:

.net - C++/CLI 终结器和运算符

deployment - ClickOnce 安装完成后如何避免运行应用程序?

visual-studio-2008 - 使用 ClickOnce 发布修订版的 Subversion 修订版?

clickonce - 该应用程序格式不正确

php - 添加到 grid.php 文件的网格选择

clickonce - 使用 mage.exe 为 *.deploy 文件创建 ClickOnce 部署 list

c# - 自托管服务上的 WCF Streaming 大数据 (500MB/1GB)

.net - 从 vb.net 中的 List(Of t) 中删除项目失败

c# - 为什么 KeyValueConfigurationCollection 没有密封?

c# - 另一个 ClickOnce GAC 引用问题