mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-09-23 04:43:35 +08:00
Initial hookin of ASP.Net WebApi V4 RC
--HG-- branch : 1.x extra : transplant_source : %3A6%C7B%92%01%0Ep%D8%C7%17l%27%91%A4%7Cy%29BH
This commit is contained in:
BIN
lib/aspnetwebapi/Microsoft.Web.Infrastructure.dll
Normal file
BIN
lib/aspnetwebapi/Microsoft.Web.Infrastructure.dll
Normal file
Binary file not shown.
BIN
lib/aspnetwebapi/System.Net.Http.Formatting.dll
Normal file
BIN
lib/aspnetwebapi/System.Net.Http.Formatting.dll
Normal file
Binary file not shown.
1121
lib/aspnetwebapi/System.Net.Http.Formatting.xml
Normal file
1121
lib/aspnetwebapi/System.Net.Http.Formatting.xml
Normal file
File diff suppressed because it is too large
Load Diff
BIN
lib/aspnetwebapi/System.Net.Http.WebRequest.dll
Normal file
BIN
lib/aspnetwebapi/System.Net.Http.WebRequest.dll
Normal file
Binary file not shown.
59
lib/aspnetwebapi/System.Net.Http.WebRequest.xml
Normal file
59
lib/aspnetwebapi/System.Net.Http.WebRequest.xml
Normal file
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>System.Net.Http.WebRequest</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:System.Net.Http.RtcRequestFactory">
|
||||
<summary>Represents the class that is used to create special <see cref="T:System.Net.Http.HttpRequestMessage" /> for use with the Real-Time-Communications (RTC) background notification infrastructure.</summary>
|
||||
</member>
|
||||
<member name="M:System.Net.Http.RtcRequestFactory.Create(System.Net.Http.HttpMethod,System.Uri)">
|
||||
<summary>Creates a special <see cref="T:System.Net.Http.HttpRequestMessage" /> for use with the Real-Time-Communications (RTC) background notification infrastructure.</summary>
|
||||
<returns>Returns <see cref="T:System.Net.Http.HttpRequestMessage" />.An HTTP request message for use with the RTC background notification infrastructure.</returns>
|
||||
<param name="method">The HTTP method.</param>
|
||||
<param name="uri">The Uri the request is sent to.</param>
|
||||
</member>
|
||||
<member name="T:System.Net.Http.WebRequestHandler">
|
||||
<summary>Provides desktop-specific features not available to Metro apps or other environments. </summary>
|
||||
</member>
|
||||
<member name="M:System.Net.Http.WebRequestHandler.#ctor">
|
||||
<summary>Initializes a new instance of the <see cref="T:System.Net.Http.WebRequestHandler" /> class.</summary>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.AllowPipelining">
|
||||
<summary> Gets or sets a value that indicates whether to pipeline the request to the Internet resource.</summary>
|
||||
<returns>Returns <see cref="T:System.Boolean" />.true if the request should be pipelined; otherwise, false. The default is true. </returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.AuthenticationLevel">
|
||||
<summary>Gets or sets a value indicating the level of authentication and impersonation used for this request.</summary>
|
||||
<returns>Returns <see cref="T:System.Net.Security.AuthenticationLevel" />.A bitwise combination of the <see cref="T:System.Net.Security.AuthenticationLevel" /> values. The default value is <see cref="F:System.Net.Security.AuthenticationLevel.MutualAuthRequested" />.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.CachePolicy">
|
||||
<summary>Gets or sets the cache policy for this request.</summary>
|
||||
<returns>Returns <see cref="T:System.Net.Cache.RequestCachePolicy" />.A <see cref="T:System.Net.Cache.RequestCachePolicy" /> object that defines a cache policy. The default is <see cref="P:System.Net.WebRequest.DefaultCachePolicy" />.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.ClientCertificates">
|
||||
<summary>Gets or sets the collection of security certificates that are associated with this request.</summary>
|
||||
<returns>Returns <see cref="T:System.Security.Cryptography.X509Certificates.X509CertificateCollection" />.The collection of security certificates associated with this request.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.ContinueTimeout">
|
||||
<summary>Gets or sets the amount of time, in milliseconds, the application will wait for 100-continue from the server before uploading data.</summary>
|
||||
<returns>Returns <see cref="T:System.TimeSpan" />.The amount of time, in milliseconds, the application will wait for 100-continue from the server before uploading data. The default value is 350 milliseconds.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.ImpersonationLevel">
|
||||
<summary>Gets or sets the impersonation level for the current request.</summary>
|
||||
<returns>Returns <see cref="T:System.Security.Principal.TokenImpersonationLevel" />.The impersonation level for the request. The default is <see cref="F:System.Security.Principal.TokenImpersonationLevel.Delegation" />.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.MaxResponseHeadersLength">
|
||||
<summary>Gets or sets the maximum allowed length of the response headers.</summary>
|
||||
<returns>Returns <see cref="T:System.Int32" />.The length, in kilobytes (1024 bytes), of the response headers.</returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.ReadWriteTimeout">
|
||||
<summary>Gets or sets a time-out in milliseconds when writing a request to or reading a response from a server.</summary>
|
||||
<returns>Returns <see cref="T:System.Int32" />.The number of milliseconds before the writing or reading times out. The default value is 300,000 milliseconds (5 minutes). </returns>
|
||||
</member>
|
||||
<member name="P:System.Net.Http.WebRequestHandler.UnsafeAuthenticatedConnectionSharing">
|
||||
<summary>Gets or sets a value that indicates whether to allow high-speed NTLM-authenticated connection sharing.</summary>
|
||||
<returns>Returns <see cref="T:System.Boolean" />.true to keep the authenticated connection open; otherwise, false.</returns>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
BIN
lib/aspnetwebapi/System.Net.Http.dll
Normal file
BIN
lib/aspnetwebapi/System.Net.Http.dll
Normal file
Binary file not shown.
1920
lib/aspnetwebapi/System.Net.Http.xml
Normal file
1920
lib/aspnetwebapi/System.Net.Http.xml
Normal file
File diff suppressed because it is too large
Load Diff
BIN
lib/aspnetwebapi/System.Web.Http.WebHost.dll
Normal file
BIN
lib/aspnetwebapi/System.Web.Http.WebHost.dll
Normal file
Binary file not shown.
96
lib/aspnetwebapi/System.Web.Http.WebHost.xml
Normal file
96
lib/aspnetwebapi/System.Web.Http.WebHost.xml
Normal file
@@ -0,0 +1,96 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<doc>
|
||||
<assembly>
|
||||
<name>System.Web.Http.WebHost</name>
|
||||
</assembly>
|
||||
<members>
|
||||
<member name="T:System.Web.Http.GlobalConfiguration">
|
||||
<summary> Provides a global <see cref="T:System.Web.Http.HttpConfiguration" /> for ASP applications. </summary>
|
||||
</member>
|
||||
<member name="P:System.Web.Http.GlobalConfiguration.Configuration">
|
||||
<summary> Gets the global <see cref="T:System.Web.Http.HttpConfiguration" />. </summary>
|
||||
</member>
|
||||
<member name="T:System.Web.Http.RouteCollectionExtensions">
|
||||
<summary> Extension methods for <see cref="T:System.Web.Routing.RouteCollection" /></summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.RouteCollectionExtensions.MapHttpRoute(System.Web.Routing.RouteCollection,System.String,System.String,System.Object)">
|
||||
<summary> Maps the specified route template and sets default constraints, and namespaces. </summary>
|
||||
<returns>A reference to the mapped route.</returns>
|
||||
<param name="routes">A collection of routes for the application.</param>
|
||||
<param name="name">The name of the route to map.</param>
|
||||
<param name="routeTemplate">The route template for the route.</param>
|
||||
<param name="defaults">An object that contains default route values.</param>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.RouteCollectionExtensions.MapHttpRoute(System.Web.Routing.RouteCollection,System.String,System.String,System.Object,System.Object)">
|
||||
<summary> Maps the specified route template and sets default route values, constraints, and namespaces. </summary>
|
||||
<returns>A reference to the mapped route.</returns>
|
||||
<param name="routes">A collection of routes for the application.</param>
|
||||
<param name="name">The name of the route to map.</param>
|
||||
<param name="routeTemplate">The route template for the route.</param>
|
||||
<param name="defaults">An object that contains default route values.</param>
|
||||
<param name="constraints">A set of expressions that specify values for routeTemplate.</param>
|
||||
</member>
|
||||
<member name="T:System.Web.Http.WebHost.HttpControllerHandler">
|
||||
<summary> A <see cref="T:System.Web.IHttpAsyncHandler" /> that passes ASP.NET requests into the <see cref="T:System.Web.Http.HttpServer" /> pipeline and write the result back. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.#ctor(System.Web.Routing.RouteData)">
|
||||
<summary> Initializes a new instance of the <see cref="T:System.Web.Http.WebHost.HttpControllerHandler" /> class. </summary>
|
||||
<param name="routeData">The route data.</param>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.BeginProcessRequest(System.Web.HttpContextBase,System.AsyncCallback,System.Object)">
|
||||
<summary> Begins the process request. </summary>
|
||||
<returns>An <see cref="T:System.IAsyncResult" /> that contains information about the status of the process. </returns>
|
||||
<param name="httpContextBase">The HTTP context base.</param>
|
||||
<param name="callback">The callback.</param>
|
||||
<param name="state">The state.</param>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.EndProcessRequest(System.IAsyncResult)">
|
||||
<summary> Provides an asynchronous process End method when the process ends. </summary>
|
||||
<param name="result">An <see cref="T:System.IAsyncResult" /> that contains information about the status of the process.</param>
|
||||
</member>
|
||||
<member name="P:System.Web.Http.WebHost.HttpControllerHandler.IsReusable">
|
||||
<summary> Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler" /> instance. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.ProcessRequest(System.Web.HttpContextBase)">
|
||||
<summary> Processes the request. </summary>
|
||||
<param name="httpContextBase">The HTTP context base.</param>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.System#Web#IHttpAsyncHandler#BeginProcessRequest(System.Web.HttpContext,System.AsyncCallback,System.Object)">
|
||||
<summary> Begins processing the request. </summary>
|
||||
<returns>An <see cref="T:System.IAsyncResult" /> that contains information about the status of the process. </returns>
|
||||
<param name="httpContext">The HTTP context.</param>
|
||||
<param name="callback">The callback.</param>
|
||||
<param name="state">The state.</param>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.System#Web#IHttpAsyncHandler#EndProcessRequest(System.IAsyncResult)">
|
||||
<summary> Provides an asynchronous process End method when the process ends. </summary>
|
||||
<param name="result">An <see cref="T:System.IAsyncResult" /> that contains information about the status of the process.</param>
|
||||
</member>
|
||||
<member name="P:System.Web.Http.WebHost.HttpControllerHandler.System#Web#IHttpHandler#IsReusable">
|
||||
<summary> Gets a value indicating whether another request can use the <see cref="T:System.Web.IHttpHandler" /> instance. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerHandler.System#Web#IHttpHandler#ProcessRequest(System.Web.HttpContext)">
|
||||
<summary> Processes the request. </summary>
|
||||
<param name="httpContext">The HTTP context base.</param>
|
||||
</member>
|
||||
<member name="T:System.Web.Http.WebHost.HttpControllerRouteHandler">
|
||||
<summary> A <see cref="T:System.Web.Routing.IRouteHandler" /> that returns instances of <see cref="T:System.Web.Http.WebHost.HttpControllerHandler" /> that can pass requests to a given <see cref="T:System.Web.Http.HttpServer" /> instance. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerRouteHandler.#ctor">
|
||||
<summary> Initializes a new instance of the <see cref="T:System.Web.Http.WebHost.HttpControllerRouteHandler" /> class. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerRouteHandler.GetHttpHandler(System.Web.Routing.RequestContext)">
|
||||
<summary> Provides the object that processes the request. </summary>
|
||||
<returns> An object that processes the request. </returns>
|
||||
<param name="requestContext">An object that encapsulates information about the request.</param>
|
||||
</member>
|
||||
<member name="P:System.Web.Http.WebHost.HttpControllerRouteHandler.Instance">
|
||||
<summary> Gets the singleton <see cref="T:System.Web.Http.WebHost.HttpControllerRouteHandler" /> instance. </summary>
|
||||
</member>
|
||||
<member name="M:System.Web.Http.WebHost.HttpControllerRouteHandler.System#Web#Routing#IRouteHandler#GetHttpHandler(System.Web.Routing.RequestContext)">
|
||||
<summary> Provides the object that processes the request. </summary>
|
||||
<returns> An object that processes the request. </returns>
|
||||
<param name="requestContext">An object that encapsulates information about the request.</param>
|
||||
</member>
|
||||
</members>
|
||||
</doc>
|
BIN
lib/aspnetwebapi/System.Web.Http.dll
Normal file
BIN
lib/aspnetwebapi/System.Web.Http.dll
Normal file
Binary file not shown.
2015
lib/aspnetwebapi/System.Web.Http.xml
Normal file
2015
lib/aspnetwebapi/System.Web.Http.xml
Normal file
File diff suppressed because it is too large
Load Diff
@@ -3,6 +3,8 @@ using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Web.Hosting;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using System.Web.Mvc;
|
||||
using Autofac;
|
||||
using Autofac.Configuration;
|
||||
@@ -28,6 +30,7 @@ using Orchard.Mvc.DataAnnotations;
|
||||
using Orchard.Mvc.ViewEngines.Razor;
|
||||
using Orchard.Mvc.ViewEngines.ThemeAwareness;
|
||||
using Orchard.Services;
|
||||
using Orchard.WebApi;
|
||||
|
||||
namespace Orchard.Environment {
|
||||
public static class OrchardStarter {
|
||||
@@ -141,6 +144,11 @@ namespace Orchard.Environment {
|
||||
}
|
||||
|
||||
ControllerBuilder.Current.SetControllerFactory(new OrchardControllerFactory());
|
||||
|
||||
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new DefaultOrchardWebApiHttpControllerSelector(GlobalConfiguration.Configuration));
|
||||
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new DefaultOrchardWebApiHttpHttpControllerActivator(GlobalConfiguration.Configuration));
|
||||
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
|
||||
|
||||
ViewEngines.Engines.Clear();
|
||||
ViewEngines.Engines.Add(new ThemeAwareViewEngineShim());
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Mvc;
|
||||
using Autofac.Core;
|
||||
using Orchard.ContentManagement;
|
||||
@@ -50,6 +51,7 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
var modules = BuildBlueprint(features, IsModule, BuildModule, excludedTypes);
|
||||
var dependencies = BuildBlueprint(features, IsDependency, (t, f) => BuildDependency(t, f, descriptor), excludedTypes);
|
||||
var controllers = BuildBlueprint(features, IsController, BuildController, excludedTypes);
|
||||
var httpControllers = BuildBlueprint(features, IsHttpController, BuildController, excludedTypes);
|
||||
var records = BuildBlueprint(features, IsRecord, (t, f) => BuildRecord(t, f, settings), excludedTypes);
|
||||
|
||||
var result = new ShellBlueprint {
|
||||
@@ -134,6 +136,10 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
return typeof(IController).IsAssignableFrom(type);
|
||||
}
|
||||
|
||||
private static bool IsHttpController(Type type) {
|
||||
return typeof(IHttpController).IsAssignableFrom(type);
|
||||
}
|
||||
|
||||
private static ControllerBlueprint BuildController(Type type, Feature feature) {
|
||||
var areaName = feature.Descriptor.Extension.Id;
|
||||
|
||||
|
@@ -17,6 +17,7 @@ namespace Orchard.Environment.ShellBuilders.Models {
|
||||
|
||||
public IEnumerable<DependencyBlueprint> Dependencies { get; set; }
|
||||
public IEnumerable<ControllerBlueprint> Controllers { get; set; }
|
||||
public IEnumerable<ControllerBlueprint> HttpControllers { get; set; }
|
||||
public IEnumerable<RecordBlueprint> Records { get; set; }
|
||||
}
|
||||
|
||||
|
@@ -2,6 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Web.Hosting;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Mvc;
|
||||
using Autofac;
|
||||
using Autofac.Builder;
|
||||
@@ -106,6 +107,17 @@ namespace Orchard.Environment.ShellBuilders {
|
||||
});
|
||||
}
|
||||
|
||||
foreach (var item in blueprint.HttpControllers) {
|
||||
var serviceKeyName = (item.AreaName + "/" + item.ControllerName).ToLowerInvariant();
|
||||
var serviceKeyType = item.Type;
|
||||
RegisterType(builder, item)
|
||||
.EnableDynamicProxy(dynamicProxyContext)
|
||||
.Keyed<IHttpController>(serviceKeyName)
|
||||
.Keyed<IHttpController>(serviceKeyType)
|
||||
.WithMetadata("ControllerType", item.Type)
|
||||
.InstancePerDependency();
|
||||
}
|
||||
|
||||
// Register code-only registrations specific to a shell
|
||||
_shellContainerRegistrations.Registrations(builder);
|
||||
|
||||
|
@@ -49,6 +49,7 @@ namespace Orchard.Mvc.Routes {
|
||||
|
||||
// otherwise wrap handler and return it
|
||||
routeData.RouteHandler = new RouteHandler(_workContextAccessor, routeData.RouteHandler);
|
||||
routeData.Values["IWorkContextAccessor"] = _workContextAccessor; //NGM : Added for WebApi
|
||||
routeData.DataTokens["IWorkContextAccessor"] = _workContextAccessor;
|
||||
return routeData;
|
||||
}
|
||||
|
@@ -105,6 +105,9 @@
|
||||
<RequiredTargetFramework>3.5</RequiredTargetFramework>
|
||||
</Reference>
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http">
|
||||
<HintPath>..\..\lib\aspnetwebapi\System.Net.Http.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Runtime.Serialization">
|
||||
<RequiredTargetFramework>3.0</RequiredTargetFramework>
|
||||
</Reference>
|
||||
@@ -113,6 +116,12 @@
|
||||
<Reference Include="System.Transactions" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.ApplicationServices" />
|
||||
<Reference Include="System.Web.Http">
|
||||
<HintPath>..\..\lib\aspnetwebapi\System.Web.Http.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http.WebHost">
|
||||
<HintPath>..\..\lib\aspnetwebapi\System.Web.Http.WebHost.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||
@@ -536,6 +545,11 @@
|
||||
<Compile Include="Wcf\OrchardInstanceContext.cs" />
|
||||
<Compile Include="Wcf\OrchardInstanceProvider.cs" />
|
||||
<Compile Include="Wcf\OrchardServiceHostFactory.cs" />
|
||||
<Compile Include="WebApi\AutofacWebApiDependencyResolver.cs" />
|
||||
<Compile Include="WebApi\AutofacWebApiDependencyScope.cs" />
|
||||
<Compile Include="WebApi\DefaultOrchardWebApiHttpControllerSelector.cs" />
|
||||
<Compile Include="WebApi\DefaultOrchardWebApiHttpHttpControllerActivator.cs" />
|
||||
<Compile Include="WebApi\Extensions\RouteExtension.cs" />
|
||||
<Compile Include="WorkContextExtensions.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorCompilationEventsShim.cs" />
|
||||
<Compile Include="Mvc\ViewEngines\Razor\RazorViewEngineProvider.cs" />
|
||||
|
41
src/Orchard/WebApi/AutofacWebApiDependencyResolver.cs
Normal file
41
src/Orchard/WebApi/AutofacWebApiDependencyResolver.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Web.Http.Dependencies;
|
||||
using Autofac;
|
||||
|
||||
namespace Orchard.WebApi {
|
||||
public class AutofacWebApiDependencyResolver : IDependencyResolver {
|
||||
readonly ILifetimeScope _container;
|
||||
readonly IDependencyScope _rootDependencyScope;
|
||||
|
||||
//internal static readonly string ApiRequestTag = "AutofacWebRequest";
|
||||
|
||||
public AutofacWebApiDependencyResolver(ILifetimeScope container) {
|
||||
if (container == null) throw new ArgumentNullException("container");
|
||||
|
||||
_container = container;
|
||||
_rootDependencyScope = new AutofacWebApiDependencyScope(container);
|
||||
}
|
||||
|
||||
public ILifetimeScope Container {
|
||||
get { return _container; }
|
||||
}
|
||||
|
||||
public object GetService(Type serviceType) {
|
||||
return _rootDependencyScope.GetService(serviceType);
|
||||
}
|
||||
|
||||
public IEnumerable<object> GetServices(Type serviceType) {
|
||||
return _rootDependencyScope.GetServices(serviceType);
|
||||
}
|
||||
|
||||
public IDependencyScope BeginScope() {
|
||||
ILifetimeScope lifetimeScope = _container.BeginLifetimeScope();
|
||||
return new AutofacWebApiDependencyScope(lifetimeScope);
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
_rootDependencyScope.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
33
src/Orchard/WebApi/AutofacWebApiDependencyScope.cs
Normal file
33
src/Orchard/WebApi/AutofacWebApiDependencyScope.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Http.Dependencies;
|
||||
using Autofac;
|
||||
|
||||
namespace Orchard.WebApi {
|
||||
public class AutofacWebApiDependencyScope : IDependencyScope {
|
||||
readonly ILifetimeScope _lifetimeScope;
|
||||
|
||||
public AutofacWebApiDependencyScope(ILifetimeScope lifetimeScope) {
|
||||
_lifetimeScope = lifetimeScope;
|
||||
}
|
||||
|
||||
public object GetService(Type serviceType) {
|
||||
return _lifetimeScope.ResolveOptional(serviceType);
|
||||
}
|
||||
|
||||
public IEnumerable<object> GetServices(Type serviceType) {
|
||||
if (!_lifetimeScope.IsRegistered(serviceType))
|
||||
return Enumerable.Empty<object>();
|
||||
|
||||
var enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType);
|
||||
var instance = _lifetimeScope.Resolve(enumerableServiceType);
|
||||
return (IEnumerable<object>)instance;
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
if (_lifetimeScope != null)
|
||||
_lifetimeScope.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using Autofac.Features.Metadata;
|
||||
using Orchard.WebApi.Extensions;
|
||||
|
||||
namespace Orchard.WebApi {
|
||||
public class DefaultOrchardWebApiHttpControllerSelector : DefaultHttpControllerSelector, IHttpControllerSelector {
|
||||
private readonly HttpConfiguration _configuration;
|
||||
|
||||
public DefaultOrchardWebApiHttpControllerSelector(HttpConfiguration configuration) : base(configuration) {
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to resolve an instance for the controller associated with a given service key for the work context scope.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the controller.</typeparam>
|
||||
/// <param name="workContext">The work context.</param>
|
||||
/// <param name="serviceKey">The service key for the controller.</param>
|
||||
/// <param name="instance">The controller instance.</param>
|
||||
/// <returns>True if the controller was resolved; false otherwise.</returns>
|
||||
protected bool TryResolve<T>(WorkContext workContext, object serviceKey, out T instance) {
|
||||
if (workContext != null && serviceKey != null) {
|
||||
var key = new KeyedService(serviceKey, typeof(T));
|
||||
object value;
|
||||
if (workContext.Resolve<ILifetimeScope>().TryResolveService(key, out value)) {
|
||||
instance = (T)value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
instance = default(T);
|
||||
return false;
|
||||
}
|
||||
|
||||
public override HttpControllerDescriptor SelectController(HttpRequestMessage request) {
|
||||
var routeData = request.GetRouteData();
|
||||
|
||||
// Determine the area name for the request, and fall back to stock orchard controllers
|
||||
var areaName = routeData.GetAreaName();
|
||||
|
||||
var controllerName = base.GetControllerName(request);
|
||||
|
||||
// Service name pattern matches the identification strategy
|
||||
var serviceKey = (areaName + "/" + controllerName).ToLowerInvariant();
|
||||
|
||||
HttpControllerContext controllerContext = new HttpControllerContext(_configuration, routeData, request);
|
||||
|
||||
// Now that the request container is known - try to resolve the controller information
|
||||
Meta<Lazy<IHttpController>> info;
|
||||
var workContext = controllerContext.GetWorkContext();
|
||||
if (TryResolve(workContext, serviceKey, out info)) {
|
||||
var type = (Type)info.Metadata["ControllerType"];
|
||||
|
||||
return
|
||||
new HttpControllerDescriptor(_configuration, controllerName, type);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Web.Http;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Http.Dispatcher;
|
||||
using Autofac;
|
||||
using Autofac.Core;
|
||||
using Autofac.Features.Metadata;
|
||||
using Orchard.WebApi.Extensions;
|
||||
|
||||
namespace Orchard.WebApi {
|
||||
public class DefaultOrchardWebApiHttpHttpControllerActivator : IHttpControllerActivator {
|
||||
private readonly HttpConfiguration _configuration;
|
||||
|
||||
public DefaultOrchardWebApiHttpHttpControllerActivator(HttpConfiguration configuration) : base() {
|
||||
_configuration = configuration;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to resolve an instance for the controller associated with a given service key for the work context scope.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the controller.</typeparam>
|
||||
/// <param name="workContext">The work context.</param>
|
||||
/// <param name="serviceKey">The service key for the controller.</param>
|
||||
/// <param name="instance">The controller instance.</param>
|
||||
/// <returns>True if the controller was resolved; false otherwise.</returns>
|
||||
protected bool TryResolve<T>(WorkContext workContext, object serviceKey, out T instance) {
|
||||
if (workContext != null && serviceKey != null) {
|
||||
var key = new KeyedService(serviceKey, typeof(T));
|
||||
object value;
|
||||
if (workContext.Resolve<ILifetimeScope>().TryResolveService(key, out value)) {
|
||||
instance = (T)value;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
instance = default(T);
|
||||
return false;
|
||||
}
|
||||
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) {
|
||||
var routeData = request.GetRouteData();
|
||||
|
||||
HttpControllerContext controllerContext = new HttpControllerContext(_configuration, routeData, request);
|
||||
|
||||
// Determine the area name for the request, and fall back to stock orchard controllers
|
||||
var areaName = routeData.GetAreaName();
|
||||
|
||||
// Service name pattern matches the identification strategy
|
||||
var serviceKey = (areaName + "/" + controllerDescriptor.ControllerName).ToLowerInvariant();
|
||||
|
||||
// Now that the request container is known - try to resolve the controller information
|
||||
Meta<Lazy<IHttpController>> info;
|
||||
var workContext = controllerContext.GetWorkContext();
|
||||
if (TryResolve(workContext, serviceKey, out info)) {
|
||||
controllerContext.ControllerDescriptor =
|
||||
new HttpControllerDescriptor(_configuration, controllerDescriptor.ControllerName, controllerType);
|
||||
|
||||
var controller = info.Value.Value;
|
||||
|
||||
controllerContext.Controller = controller;
|
||||
|
||||
return controller;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
34
src/Orchard/WebApi/Extensions/RouteExtension.cs
Normal file
34
src/Orchard/WebApi/Extensions/RouteExtension.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.Http.Routing;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
|
||||
namespace Orchard.WebApi.Extensions {
|
||||
public static class RouteExtension {
|
||||
public static string GetAreaName(this IHttpRoute route){
|
||||
var routeWithArea = route as IRouteWithArea;
|
||||
if (routeWithArea != null) {
|
||||
return routeWithArea.Area;
|
||||
}
|
||||
|
||||
var castRoute = route as Route;
|
||||
if (castRoute != null && castRoute.DataTokens != null) {
|
||||
return castRoute.DataTokens["area"] as string;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetAreaName(this IHttpRouteData routeData) {
|
||||
object area;
|
||||
if (routeData.Route.DataTokens.TryGetValue("area", out area)) {
|
||||
return area as string;
|
||||
}
|
||||
|
||||
return GetAreaName(routeData.Route);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,4 +1,5 @@
|
||||
using System.Web;
|
||||
using System.Web.Http.Controllers;
|
||||
using System.Web.Mvc;
|
||||
using System.Web.Routing;
|
||||
using Autofac;
|
||||
@@ -29,6 +30,26 @@ namespace Orchard {
|
||||
return workContextAccessor.GetContext(requestContext.HttpContext);
|
||||
}
|
||||
|
||||
public static WorkContext GetWorkContext(this HttpControllerContext controllerContext) {
|
||||
if (controllerContext == null)
|
||||
return null;
|
||||
|
||||
var routeData = controllerContext.RouteData;
|
||||
if (routeData == null || routeData.Values == null)
|
||||
return null;
|
||||
|
||||
object workContextValue;
|
||||
if (!routeData.Values.TryGetValue("IWorkContextAccessor", out workContextValue)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (workContextValue == null || !(workContextValue is IWorkContextAccessor))
|
||||
return null;
|
||||
|
||||
var workContextAccessor = (IWorkContextAccessor)workContextValue;
|
||||
return workContextAccessor.GetContext();
|
||||
}
|
||||
|
||||
private static object FindWorkContextInParent(RouteData routeData) {
|
||||
object parentViewContextValue;
|
||||
if (!routeData.DataTokens.TryGetValue("ParentActionViewContext", out parentViewContextValue)
|
||||
|
Reference in New Issue
Block a user