EFCore + LazyLoadingProxies + Localization + Html.DisplayNameForModel() == NotSupportedException: The invoked member is not supported in a dynamic asm
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/EFCore--LazyLoadingProxies--Localizati/10110219)._
---
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,
1. Click "Test Bug"
2. Click "Create New" => OK
3. Click "Edit", "Details", or "Delete" => Exception
I noted that the following combination caused the exception,
1. EF Core + LazyLoading + Localization + Html.DisplayNameForModel().
The following combinations are ok,
1. EF Core + LazyLoading + Html.DisplayNameForModel().
2. 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;
if (type.Namespace.Equals("Castle.Proxies"))
{
type = type.BaseType;
}
var dnAttributes = GetCustomAttributes<DisplayNameAttribute>(type);
if (dnAttributes?.Length > 0)
{
result = dnAttributes.First().DisplayName;
}
if (result == null)
{
var dAttributes = GetCustomAttributes<DisplayAttribute>(type);
if (dAttributes?.Length > 0)
{
result = dAttributes.First().Name;
}
}
if (result == null)
{
result = type.Name;
}
if (serviceProvider != null)
{
var genericType = typeof(IStringLocalizer<>);
var listOfTypeArgs = new[] { type };
var serviceType = genericType.MakeGenericType(listOfTypeArgs);
if (serviceProvider.GetService(serviceType) is IStringLocalizer localizer)
{
return localizer[result];
}
}
return 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()
Contributor guide
Assessment
This issue has not been assessed yet.