Medium Trust: Making console run under full trust.

--HG--
branch : dev
This commit is contained in:
Andre Rodrigues
2010-11-17 12:30:36 -08:00
parent 58499abb10
commit c56f157c97
8 changed files with 21 additions and 32 deletions

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Web.Hosting;
using Orchard.Parameters;
using Orchard.ResponseFiles;
@@ -19,11 +20,13 @@ namespace Orchard.Host {
HostingEnvironment.RegisterObject(this);
}
[SecurityCritical]
public override object InitializeLifetimeService() {
// never expire the license
return null;
}
[SecuritySafeCritical]
void IRegisteredObject.Stop(bool immediate) {
HostingEnvironment.UnregisterObject(this);
}

View File

@@ -9,8 +9,6 @@ namespace Orchard.HostContext {
public DirectoryInfo OrchardDirectory { get; set; }
public int StartSessionResult { get; set; }
public bool DisplayUsageHelp { get; set; }
public ApplicationManager AppManager { get; set; }
public ApplicationObject AppObject { get; set; }
public CommandHost CommandHost { get; set; }
public Logger Logger { get; set; }
}

View File

@@ -3,7 +3,9 @@ using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security;
using System.Web;
using System.Web.Compilation;
using System.Web.Hosting;
using Orchard.Host;
using Orchard.Parameters;
@@ -20,6 +22,7 @@ namespace Orchard.HostContext {
_args = args;
}
[SecurityCritical]
public CommandHostContext CreateContext() {
var context = new CommandHostContext();
context.RetryResult = 240;/*special return code for "Retry"*/
@@ -27,6 +30,7 @@ namespace Orchard.HostContext {
return context;
}
[SecurityCritical]
public void Shutdown(CommandHostContext context) {
try {
if (context.CommandHost != null) {
@@ -38,9 +42,9 @@ namespace Orchard.HostContext {
LogInfo(context, " (AppDomain already unloaded)");
}
if (context.AppObject != null) {
if (context.CommandHost != null) {
LogInfo(context, "Shutting down ASP.NET AppDomain...");
context.AppManager.ShutdownApplication(context.AppObject.ApplicationId);
ApplicationManager.GetApplicationManager().ShutdownAll();
}
}
@@ -72,9 +76,7 @@ namespace Orchard.HostContext {
LogInfo(context, "Orchard root directory: \"{0}\"", context.OrchardDirectory.FullName);
LogInfo(context, "Creating ASP.NET AppDomain for command agent...");
context.AppManager = ApplicationManager.GetApplicationManager();
context.AppObject = CreateWorkerAppDomainWithHost(context.AppManager, context.Arguments.VirtualPath, context.OrchardDirectory.FullName, typeof(CommandHost));
context.CommandHost = (CommandHost)context.AppObject.ObjectInstance;
context.CommandHost = CreateWorkerAppDomainWithHost(context.Arguments.VirtualPath, context.OrchardDirectory.FullName, typeof(CommandHost));
LogInfo(context, "Starting Orchard session");
context.StartSessionResult = context.CommandHost.StartSession(_input, _output);
@@ -113,29 +115,9 @@ namespace Orchard.HostContext {
string.Format("Directory \"{0}\" doesn't seem to contain an Orchard installation", new DirectoryInfo(directory).FullName));
}
private static ApplicationObject CreateWorkerAppDomainWithHost(ApplicationManager appManager, string virtualPath, string physicalPath, Type hostType) {
// this creates worker app domain in a way that host doesn't need to be in GAC or bin
// using BuildManagerHost via private reflection
string uniqueAppString = string.Concat(virtualPath, physicalPath).ToLowerInvariant();
string appId = (uniqueAppString.GetHashCode()).ToString("x", CultureInfo.InvariantCulture);
// create BuildManagerHost in the worker app domain
var buildManagerHostType = typeof(HttpRuntime).Assembly.GetType("System.Web.Compilation.BuildManagerHost");
var buildManagerHost = appManager.CreateObject(appId, buildManagerHostType, virtualPath, physicalPath, false);
// call BuildManagerHost.RegisterAssembly to make Host type loadable in the worker app domain
buildManagerHostType.InvokeMember(
"RegisterAssembly",
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.NonPublic,
null,
buildManagerHost,
new object[] { hostType.Assembly.FullName, hostType.Assembly.Location });
// create Host in the worker app domain
return new ApplicationObject {
ApplicationId = appId,
ObjectInstance = appManager.CreateObject(appId, hostType, virtualPath, physicalPath, false)
};
private static CommandHost CreateWorkerAppDomainWithHost(string virtualPath, string physicalPath, Type hostType) {
ClientBuildManager clientBuildManager = new ClientBuildManager(virtualPath, physicalPath);
return (CommandHost)clientBuildManager.CreateObject(hostType, false);
}
}
}

View File

@@ -71,7 +71,6 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ApplicationObject.cs" />
<Compile Include="HostContext\CommandHostContext.cs" />
<Compile Include="HostContext\CommandHostContextProvider.cs" />
<Compile Include="HostContext\ICommandHostContextProvider.cs" />

View File

@@ -1,10 +1,11 @@
using System;
using System.Collections.Generic;
using System.Security;
using Orchard.Parameters;
namespace Orchard {
public class OrchardParametersParser : IOrchardParametersParser {
[SecurityCritical]
public OrchardParameters Parse(CommandParameters parameters) {
var result = new OrchardParameters {

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Security;
using System.Text;
namespace Orchard.Parameters {
@@ -8,6 +9,7 @@ namespace Orchard.Parameters {
}
public class CommandLineParser : ICommandLineParser {
[SecurityCritical]
public IEnumerable<string> Parse(string commandLine) {
return SplitArgs(commandLine);
}

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Security;
namespace Orchard.Parameters {
public class CommandParametersParser : ICommandParametersParser {
[SecurityCritical]
public CommandParameters Parse(IEnumerable<string> args) {
var result = new CommandParameters {
Arguments = new List<string>(),

View File

@@ -1,6 +1,7 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
@@ -34,3 +35,4 @@ using System.Runtime.InteropServices;
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.8.0")]
[assembly: AssemblyFileVersion("0.8.0")]
[assembly: SecurityCritical]