Change IViewEngine.FindView to be synchronous
This commit is contained in:
parent
2e0bed750e
commit
75dae948b2
|
|
@ -19,14 +19,14 @@ namespace Microsoft.AspNet.Mvc
|
||||||
_viewEngine = viewEngine;
|
_viewEngine = viewEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string ViewName {get; set; }
|
public string ViewName { get; set; }
|
||||||
|
|
||||||
public ViewDataDictionary ViewData { get; set; }
|
public ViewDataDictionary ViewData { get; set; }
|
||||||
|
|
||||||
public async Task ExecuteResultAsync([NotNull] ActionContext context)
|
public async Task ExecuteResultAsync([NotNull] ActionContext context)
|
||||||
{
|
{
|
||||||
var viewName = ViewName ?? context.ActionDescriptor.Name;
|
var viewName = ViewName ?? context.ActionDescriptor.Name;
|
||||||
var view = await FindView(context.RouteValues, viewName);
|
var view = FindView(context.RouteValues, viewName);
|
||||||
|
|
||||||
using (view as IDisposable)
|
using (view as IDisposable)
|
||||||
{
|
{
|
||||||
|
|
@ -39,10 +39,9 @@ namespace Microsoft.AspNet.Mvc
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IView> FindView([NotNull] IDictionary<string, object> context,[NotNull] string viewName)
|
private IView FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
|
||||||
{
|
{
|
||||||
var result = await _viewEngine.FindView(context, viewName);
|
var result = _viewEngine.FindView(context, viewName);
|
||||||
|
|
||||||
return result.View;
|
return result.View;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,17 +68,16 @@ namespace Microsoft.AspNet.Mvc
|
||||||
_viewName);
|
_viewName);
|
||||||
}
|
}
|
||||||
|
|
||||||
var view = await FindView(context.ViewContext.ViewEngineContext, qualifiedViewName);
|
var view = FindView(context.ViewContext.ViewEngineContext, qualifiedViewName);
|
||||||
using (view as IDisposable)
|
using (view as IDisposable)
|
||||||
{
|
{
|
||||||
await view.RenderAsync(childViewContext);
|
await view.RenderAsync(childViewContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<IView> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
|
private IView FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName)
|
||||||
{
|
{
|
||||||
var result = await _viewEngine.FindView(context, viewName);
|
var result = _viewEngine.FindView(context, viewName);
|
||||||
|
|
||||||
return result.View;
|
return result.View;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.FileSystems;
|
using Microsoft.AspNet.FileSystems;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Razor
|
namespace Microsoft.AspNet.Mvc.Razor
|
||||||
|
|
@ -14,7 +13,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
_cache = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
|
_cache = new ConcurrentDictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<CompilationResult> GetOrAdd(IFileInfo file, Func<Task<CompilationResult>> compile)
|
public CompilationResult GetOrAdd(IFileInfo file, Func<CompilationResult> compile)
|
||||||
{
|
{
|
||||||
// Generate a content id
|
// Generate a content id
|
||||||
string contentId = file.PhysicalPath + '|' + file.LastModified.Ticks;
|
string contentId = file.PhysicalPath + '|' + file.LastModified.Ticks;
|
||||||
|
|
@ -22,7 +21,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
Type compiledType;
|
Type compiledType;
|
||||||
if (!_cache.TryGetValue(contentId, out compiledType))
|
if (!_cache.TryGetValue(contentId, out compiledType))
|
||||||
{
|
{
|
||||||
CompilationResult result = await compile();
|
CompilationResult result = compile();
|
||||||
_cache.TryAdd(contentId, result.CompiledType);
|
_cache.TryAdd(contentId, result.CompiledType);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,6 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
public interface ICompilationService
|
public interface ICompilationService
|
||||||
{
|
{
|
||||||
Task<CompilationResult> Compile(string content);
|
CompilationResult Compile(string content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
_libraryManager = libraryManager;
|
_libraryManager = libraryManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<CompilationResult> Compile(string content)
|
public CompilationResult Compile(string content)
|
||||||
{
|
{
|
||||||
var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(content) };
|
var syntaxTrees = new[] { CSharpSyntaxTree.ParseText(content) };
|
||||||
var targetFramework = _environment.TargetFramework;
|
var targetFramework = _environment.TargetFramework;
|
||||||
|
|
@ -51,14 +51,14 @@ namespace Microsoft.AspNet.Mvc.Razor.Compilation
|
||||||
|
|
||||||
var messages = result.Diagnostics.Where(IsError).Select(d => GetCompilationMessage(formatter, d)).ToList();
|
var messages = result.Diagnostics.Where(IsError).Select(d => GetCompilationMessage(formatter, d)).ToList();
|
||||||
|
|
||||||
return Task.FromResult(CompilationResult.Failed(content, messages));
|
return CompilationResult.Failed(content, messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = _loader.LoadBytes(ms.ToArray(), pdb.ToArray())
|
var type = _loader.LoadBytes(ms.ToArray(), pdb.ToArray())
|
||||||
.GetExportedTypes()
|
.GetExportedTypes()
|
||||||
.First();
|
.First();
|
||||||
|
|
||||||
return Task.FromResult(CompilationResult.Successful(String.Empty, type));
|
return CompilationResult.Successful(String.Empty, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,9 @@
|
||||||
using System.Threading.Tasks;
|
using Microsoft.AspNet.FileSystems;
|
||||||
using Microsoft.AspNet.FileSystems;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Razor
|
namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
public interface IRazorCompilationService
|
public interface IRazorCompilationService
|
||||||
{
|
{
|
||||||
Task<CompilationResult> Compile(IFileInfo fileInfo);
|
CompilationResult Compile(IFileInfo fileInfo);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
using System.Diagnostics.Contracts;
|
using System.Diagnostics.Contracts;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.FileSystems;
|
using Microsoft.AspNet.FileSystems;
|
||||||
using Microsoft.AspNet.Razor;
|
using Microsoft.AspNet.Razor;
|
||||||
using Microsoft.Net.Runtime;
|
using Microsoft.Net.Runtime;
|
||||||
|
|
@ -27,13 +26,13 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
_appRoot = EnsureTrailingSlash(environment.ApplicationBasePath);
|
_appRoot = EnsureTrailingSlash(environment.ApplicationBasePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<CompilationResult> Compile([NotNull]IFileInfo file)
|
public CompilationResult Compile([NotNull]IFileInfo file)
|
||||||
{
|
{
|
||||||
return _cache.GetOrAdd(file, () => CompileCore(file));
|
return _cache.GetOrAdd(file, () => CompileCore(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make this internal
|
// TODO: Make this internal
|
||||||
public async Task<CompilationResult> CompileCore(IFileInfo file)
|
public CompilationResult CompileCore(IFileInfo file)
|
||||||
{
|
{
|
||||||
GeneratorResults results;
|
GeneratorResults results;
|
||||||
using (Stream inputStream = file.CreateReadStream())
|
using (Stream inputStream = file.CreateReadStream())
|
||||||
|
|
@ -49,7 +48,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
throw new CompilationFailedException(messages, results.GeneratedCode);
|
throw new CompilationFailedException(messages, results.GeneratedCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await _baseCompilationService.Compile(results.GeneratedCode);
|
return _baseCompilationService.Compile(results.GeneratedCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string EnsureTrailingSlash([NotNull]string path)
|
private static string EnsureTrailingSlash([NotNull]string path)
|
||||||
|
|
|
||||||
|
|
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
private async Task RenderLayoutAsync(ViewContext context, string bodyContent)
|
private async Task RenderLayoutAsync(ViewContext context, string bodyContent)
|
||||||
{
|
{
|
||||||
var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>();
|
var virtualPathFactory = context.ServiceProvider.GetService<IVirtualPathViewFactory>();
|
||||||
var layoutView = (RazorView)(await virtualPathFactory.CreateInstance(Layout));
|
var layoutView = (RazorView)virtualPathFactory.CreateInstance(Layout);
|
||||||
|
|
||||||
if (layoutView == null)
|
if (layoutView == null)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,10 @@
|
||||||
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Razor
|
namespace Microsoft.AspNet.Mvc.Razor
|
||||||
{
|
{
|
||||||
public interface IVirtualPathViewFactory
|
public interface IVirtualPathViewFactory
|
||||||
{
|
{
|
||||||
Task<IView> CreateInstance(string virtualPath);
|
IView CreateInstance(string virtualPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,10 +28,10 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
get { return _viewLocationFormats; }
|
get { return _viewLocationFormats; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context,
|
public ViewEngineResult FindView([NotNull] IDictionary<string, object> context,
|
||||||
[NotNull] string viewName)
|
[NotNull] string viewName)
|
||||||
{
|
{
|
||||||
var viewEngineResult = await CreateViewEngineResult(context, viewName);
|
var viewEngineResult = CreateViewEngineResult(context, viewName);
|
||||||
var errorMessage = Resources.FormatViewEngine_ViewNotFound(
|
var errorMessage = Resources.FormatViewEngine_ViewNotFound(
|
||||||
viewName,
|
viewName,
|
||||||
ToLocationString(viewEngineResult.SearchedLocations));
|
ToLocationString(viewEngineResult.SearchedLocations));
|
||||||
|
|
@ -41,10 +41,10 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
return viewEngineResult;
|
return viewEngineResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context,
|
public ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context,
|
||||||
[NotNull] string partialViewName)
|
[NotNull] string partialViewName)
|
||||||
{
|
{
|
||||||
var viewEngineResult = await CreateViewEngineResult(context, partialViewName);
|
var viewEngineResult = CreateViewEngineResult(context, partialViewName);
|
||||||
var errorMessage = Resources.FormatViewEngine_PartialViewNotFound(
|
var errorMessage = Resources.FormatViewEngine_PartialViewNotFound(
|
||||||
partialViewName,
|
partialViewName,
|
||||||
ToLocationString(viewEngineResult.SearchedLocations));
|
ToLocationString(viewEngineResult.SearchedLocations));
|
||||||
|
|
@ -54,14 +54,14 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
return viewEngineResult;
|
return viewEngineResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<ViewEngineResult> CreateViewEngineResult([NotNull] IDictionary<string, object> context,
|
private ViewEngineResult CreateViewEngineResult([NotNull] IDictionary<string, object> context,
|
||||||
[NotNull] string viewName)
|
[NotNull] string viewName)
|
||||||
{
|
{
|
||||||
var nameRepresentsPath = IsSpecificPath(viewName);
|
var nameRepresentsPath = IsSpecificPath(viewName);
|
||||||
|
|
||||||
if (nameRepresentsPath)
|
if (nameRepresentsPath)
|
||||||
{
|
{
|
||||||
var view = await _virtualPathFactory.CreateInstance(viewName);
|
var view = _virtualPathFactory.CreateInstance(viewName);
|
||||||
return view != null ? ViewEngineResult.Found(viewName, view) :
|
return view != null ? ViewEngineResult.Found(viewName, view) :
|
||||||
ViewEngineResult.NotFound(viewName, new[] { viewName });
|
ViewEngineResult.NotFound(viewName, new[] { viewName });
|
||||||
}
|
}
|
||||||
|
|
@ -74,7 +74,7 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
for (int i = 0; i < _viewLocationFormats.Length; i++)
|
for (int i = 0; i < _viewLocationFormats.Length; i++)
|
||||||
{
|
{
|
||||||
var path = String.Format(CultureInfo.InvariantCulture, _viewLocationFormats[i], viewName, controllerName, areaName);
|
var path = String.Format(CultureInfo.InvariantCulture, _viewLocationFormats[i], viewName, controllerName, areaName);
|
||||||
IView view = await _virtualPathFactory.CreateInstance(path);
|
IView view = _virtualPathFactory.CreateInstance(path);
|
||||||
if (view != null)
|
if (view != null)
|
||||||
{
|
{
|
||||||
return ViewEngineResult.Found(viewName, view);
|
return ViewEngineResult.Found(viewName, view);
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,12 @@ namespace Microsoft.AspNet.Mvc.Razor
|
||||||
_compilationService = compilationService;
|
_compilationService = compilationService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IView> CreateInstance([NotNull]string virtualPath)
|
public IView CreateInstance([NotNull]string virtualPath)
|
||||||
{
|
{
|
||||||
IFileInfo fileInfo;
|
IFileInfo fileInfo;
|
||||||
if (_fileSystem.TryGetFileInfo(virtualPath, out fileInfo))
|
if (_fileSystem.TryGetFileInfo(virtualPath, out fileInfo))
|
||||||
{
|
{
|
||||||
CompilationResult result = await _compilationService.Compile(fileInfo);
|
CompilationResult result = _compilationService.Compile(fileInfo);
|
||||||
return (IView)Activator.CreateInstance(result.CompiledType);
|
return (IView)Activator.CreateInstance(result.CompiledType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
Writer = writer
|
Writer = writer
|
||||||
};
|
};
|
||||||
|
|
||||||
var viewEngineResult = await _viewEngine.FindPartialView(newViewContext.ViewEngineContext, partialViewName);
|
var viewEngineResult = _viewEngine.FindPartialView(newViewContext.ViewEngineContext, partialViewName);
|
||||||
|
|
||||||
await viewEngineResult.View.RenderAsync(newViewContext);
|
await viewEngineResult.View.RenderAsync(newViewContext);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc.Rendering
|
namespace Microsoft.AspNet.Mvc.Rendering
|
||||||
{
|
{
|
||||||
public interface IViewEngine
|
public interface IViewEngine
|
||||||
{
|
{
|
||||||
Task<ViewEngineResult> FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
|
ViewEngineResult FindView([NotNull] IDictionary<string, object> context, [NotNull] string viewName);
|
||||||
Task<ViewEngineResult> FindPartialView([NotNull] IDictionary<string, object> context, [NotNull] string partialViewName);
|
|
||||||
|
ViewEngineResult FindPartialView([NotNull] IDictionary<string, object> context, [NotNull] string partialViewName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,4 @@
|
||||||
using System;
|
using System.IO;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Microsoft.AspNet.FileSystems;
|
using Microsoft.AspNet.FileSystems;
|
||||||
using Microsoft.AspNet.Razor;
|
using Microsoft.AspNet.Razor;
|
||||||
using Microsoft.AspNet.Razor.Generator.Compiler;
|
using Microsoft.AspNet.Razor.Generator.Compiler;
|
||||||
|
|
@ -31,7 +26,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
.Verifiable();
|
.Verifiable();
|
||||||
var compiler = new Mock<ICompilationService>();
|
var compiler = new Mock<ICompilationService>();
|
||||||
compiler.Setup(c => c.Compile(It.IsAny<string>()))
|
compiler.Setup(c => c.Compile(It.IsAny<string>()))
|
||||||
.Returns(Task.FromResult(CompilationResult.Successful("", typeof(RazorCompilationServiceTest))));
|
.Returns(CompilationResult.Successful("", typeof(RazorCompilationServiceTest)));
|
||||||
|
|
||||||
var razorService = new RazorCompilationService(env.Object, compiler.Object, host.Object);
|
var razorService = new RazorCompilationService(env.Object, compiler.Object, host.Object);
|
||||||
var fileInfo = new Mock<IFileInfo>();
|
var fileInfo = new Mock<IFileInfo>();
|
||||||
|
|
@ -39,7 +34,7 @@ namespace Microsoft.AspNet.Mvc.Razor.Test
|
||||||
fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null);
|
fileInfo.Setup(f => f.CreateReadStream()).Returns(Stream.Null);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
razorService.CompileCore(fileInfo.Object).Wait();
|
razorService.CompileCore(fileInfo.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
host.Verify();
|
host.Verify();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue