-
Notifications
You must be signed in to change notification settings - Fork 217
port changes in SDK after Microsoft.NET.HostModel.AppHost is created #7373
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.NET.HostModel.AppHost | ||
{ | ||
/// <summary> | ||
/// Failed to delete apphost when trying to delete incomplete appphost | ||
/// </summary> | ||
public class FailedToDeleteApphostException : AppHostUpdateException | ||
{ | ||
public string ExceptionMessage { get; } | ||
public FailedToDeleteApphostException(string exceptionMessage) | ||
{ | ||
ExceptionMessage = exceptionMessage; | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.IO.MemoryMappedFiles; | ||
using System.Text; | ||
|
@@ -43,45 +44,68 @@ public static void CreateAppHost( | |
|
||
BinaryUtils.CopyFile(appHostSourceFilePath, appHostDestinationFilePath); | ||
|
||
// Re-write the destination apphost with the proper contents. | ||
bool appHostIsPEImage = false; | ||
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) | ||
try | ||
{ | ||
using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) | ||
// Re-write the destination apphost with the proper contents. | ||
bool appHostIsPEImage = false; | ||
using (var memoryMappedFile = MemoryMappedFile.CreateFromFile(appHostDestinationFilePath)) | ||
{ | ||
BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); | ||
using (MemoryMappedViewAccessor accessor = memoryMappedFile.CreateViewAccessor()) | ||
{ | ||
BinaryUtils.SearchAndReplace(accessor, AppBinaryPathPlaceholderSearchValue, bytesToWrite); | ||
|
||
appHostIsPEImage = BinaryUtils.IsPEImage(accessor); | ||
appHostIsPEImage = BinaryUtils.IsPEImage(accessor); | ||
|
||
if (windowsGraphicalUserInterface) | ||
{ | ||
if (!appHostIsPEImage) | ||
if (windowsGraphicalUserInterface) | ||
{ | ||
throw new AppHostNotPEFileException(); | ||
if (!appHostIsPEImage) | ||
{ | ||
throw new AppHostNotPEFileException(); | ||
} | ||
|
||
BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); | ||
} | ||
} | ||
} | ||
|
||
BinaryUtils.SetWindowsGraphicalUserInterfaceBit(accessor); | ||
if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) | ||
{ | ||
if (ResourceUpdater.IsSupportedOS()) | ||
{ | ||
// Copy resources from managed dll to the apphost | ||
new ResourceUpdater(appHostDestinationFilePath) | ||
.AddResourcesFromPEImage(assemblyToCopyResorcesFrom) | ||
.Update(); | ||
} | ||
else | ||
{ | ||
throw new AppHostCustomizationUnsupportedOSException(); | ||
} | ||
} | ||
} | ||
|
||
if (assemblyToCopyResorcesFrom != null && appHostIsPEImage) | ||
// Memory-mapped write does not updating last write time | ||
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ResourceUpdater.IsSupportedOS()) | ||
FailedToDeleteApphostException failedToDeleteApphostException = null; | ||
// Delete the destination file so we don't leave an unmodified apphost | ||
try | ||
{ | ||
// Copy resources from managed dll to the apphost | ||
new ResourceUpdater(appHostDestinationFilePath) | ||
.AddResourcesFromPEImage(assemblyToCopyResorcesFrom) | ||
.Update(); | ||
File.Delete(appHostDestinationFilePath); | ||
} | ||
else | ||
catch (Exception failedToDeleteEx) when (failedToDeleteEx is IOException || failedToDeleteEx is UnauthorizedAccessException) | ||
wli3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
throw new AppHostCustomizationUnsupportedOSException(); | ||
failedToDeleteApphostException = new FailedToDeleteApphostException(failedToDeleteEx.Message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put failedToDeleteEx in innner exception so that the AggregateException has all stack traces. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, do we need FailedToDeleteException at all? Can we just throw an AggregateException of the deletion exception and the original exception? The goal was not to lose the info if Delete is also failing, but an arbitrary exception here is going to become a big scary stack trace in MSBuild, right? If we aggregate both, we'll have all the info in that bug report. The SDK wouldn't need to handle AggregateException specially and log it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also remove the when from the Delete catch. try
{
// create apphost
}
catch (Exception ex)
{
try
{
// delete apphost
}
catch (Exception deleteEx)
{
throw new AggregateException(ex, deleteEx);
}
throw;
} |
||
} | ||
} | ||
|
||
// Memory-mapped write does not updating last write time | ||
File.SetLastWriteTimeUtc(appHostDestinationFilePath, DateTime.UtcNow); | ||
if (failedToDeleteApphostException != null) | ||
{ | ||
throw new AggregateException(ex, failedToDeleteApphostException); | ||
} | ||
|
||
throw; | ||
} | ||
} | ||
|
||
/// <summary> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't this passed to base so that we get it as Exception.Message?