Description
This issue has been moved from a ticket on Developer Community.
I'm using EFCore with SQL Server + Lazy Loading. I also use @Html.DisplayNameForModel() to display the model's name in Views. Recently I added localization and now calling @Html.DisplayNameForModel() will cause the following exception,
System.NotSupportedException: The invoked member is not supported in a dynamic assembly.
at System.Reflection.Emit.InternalAssemblyBuilder.GetManifestResourceStream(Type type, String name)
at System.Resources.ManifestBasedResourceGroveler.GrovelForResourceSet(CultureInfo culture, Dictionary`2 localResourceSets, Boolean tryParents, Boolean createIfNotExists)
at System.Resources.ResourceManager.InternalGetResourceSet(CultureInfo culture, Boolean createIfNotExists, Boolean tryParents)
at System.Resources.ResourceManager.GetString(String name, CultureInfo culture)
at Microsoft.Extensions.Localization.ResourceManagerStringLocalizer.GetStringSafely(String name, CultureInfo culture)
at Microsoft.Extensions.Localization.ResourceManagerStringLocalizer.get_Item(String name)
at Microsoft.AspNetCore.Mvc.DataAnnotations.DataAnnotationsMetadataProvider.<>c__DisplayClass9_0.b__4()
at Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.DefaultModelMetadata.get_DisplayName()
at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.GenerateDisplayName(ModelExplorer modelExplorer, String expression)
at Microsoft.AspNetCore.Mvc.ViewFeatures.HtmlHelper.DisplayName(String expression)
at Microsoft.AspNetCore.Mvc.Rendering.HtmlHelperDisplayNameExtensions.DisplayNameForModel(IHtmlHelper htmlHelper)
at AspNetCoreGeneratedDocument.Views_MyModels_Edit.ExecuteAsync() in C:\Users\tspoh\source\repos\EFCoreLocalizationDisplayNameForModelBug\EFCoreLocalizationDisplayNameForModelBug\Views\MyModels\Edit.cshtml:line 10
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, Boolean invokeViewStarts)
at Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, String contentType, Nullable`1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, String contentType, Nullable`1 statusCode)
at Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor.ExecuteAsync(ActionContext context, ViewResult result)
at Microsoft.AspNetCore.Mvc.ViewResult.ExecuteResultAsync(ActionContext context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
I am able to replicate the issue and I'm attaching a small project to reproduce it. Open, run and follow steps,
- Click "Test Bug"
- Click "Create New" => OK
- Click "Edit", "Details", or "Delete" => Exception
I noted that the following combination caused the exception,
- EF Core + LazyLoading + Localization + Html.DisplayNameForModel().
The following combinations are ok,
- EF Core + LazyLoading + Html.DisplayNameForModel().
- EF Core + Localization + Html.DisplayNameForModel().
Original Comments
Feedback Bot on 8/3/2022, 00:47 AM:
(private comment, text removed)
Nicholas Poh on 8/3/2022, 04:32 AM:
(private comment, text removed)
Original Solutions
Nicholas Poh solved on 8/4/2022, 09:36 PM, 0 votes:
The workaround is to introduce a new extension for IHtmlHelper
public static string DisplayNameForModelEx(this IHtmlHelper html) { var serviceProvider = html.ViewContext.HttpContext.RequestServices; var modelType = html.ViewData.Model.GetType(); return GetDisplayName(modelType, serviceProvider); }
private static string GetDisplayName(this Type type, IServiceProvider serviceProvider = null)
{
string result = null;<span class="hljs-keyword">if</span> (type.Namespace.Equals(<span class="hljs-string">"Castle.Proxies"</span>)) { type = type.BaseType; } <span class="hljs-keyword">var</span> dnAttributes = GetCustomAttributes<DisplayNameAttribute>(type); <span class="hljs-keyword">if</span> (dnAttributes?.Length > <span class="hljs-number">0</span>) { result = dnAttributes.First().DisplayName; } <span class="hljs-keyword">if</span> (result == <span class="hljs-literal">null</span>) { <span class="hljs-keyword">var</span> dAttributes = GetCustomAttributes<DisplayAttribute>(type); <span class="hljs-keyword">if</span> (dAttributes?.Length > <span class="hljs-number">0</span>) { result = dAttributes.First().Name; } } <span class="hljs-keyword">if</span> (result == <span class="hljs-literal">null</span>) { result = type.Name; } <span class="hljs-keyword">if</span> (serviceProvider != <span class="hljs-literal">null</span>) { <span class="hljs-keyword">var</span> genericType = <span class="hljs-keyword">typeof</span>(IStringLocalizer<>); <span class="hljs-keyword">var</span> listOfTypeArgs = <span class="hljs-keyword">new</span>[] { type }; <span class="hljs-keyword">var</span> serviceType = genericType.MakeGenericType(listOfTypeArgs); <span class="hljs-keyword">if</span> (serviceProvider.GetService(serviceType) <span class="hljs-keyword">is</span> IStringLocalizer localizer) { <span class="hljs-keyword">return</span> localizer[result]; } } <span class="hljs-keyword">return</span> result;
}
private static T[] GetCustomAttributes<T>(Type type)
where T : Attribute
{
return (T[])(object)type.GetCustomAttributes(typeof(T), false);
}
Replace @Html.DisplayNameForModel()
with @Html.DisplayNameForModelEx()