Display error message when missing injected services.
This commit is contained in:
@ -124,6 +124,9 @@
|
||||
<value>Module Type Is Invalid For {0}</value>
|
||||
</data>
|
||||
<data name="Error.Module.Exception" xml:space="preserve">
|
||||
<value>An Unexpected Error Has Occurred</value>
|
||||
<value>An Unexpected Error Has Occurred</value>
|
||||
</data>
|
||||
<data name="Error.Module.InvalidInjectedServices" xml:space="preserve">
|
||||
<value>Missing service(s): {0}. Please make sure they have been registered correctly.</value>
|
||||
</data>
|
||||
</root>
|
||||
@ -1,7 +1,11 @@
|
||||
@namespace Oqtane.UI
|
||||
@using System.Reflection
|
||||
@using Module = Oqtane.Models.Module
|
||||
@inject IServiceProvider ServiceProvider
|
||||
@inject SiteState ComponentSiteState
|
||||
@inject IStringLocalizer<ModuleInstance> Localizer
|
||||
@inject ILogService LoggingService
|
||||
@inject NavigationManager NavigationManager
|
||||
@inherits ErrorBoundary
|
||||
|
||||
<CascadingValue Value="@PageState" IsFixed="true">
|
||||
@ -67,37 +71,50 @@
|
||||
{
|
||||
if (ShouldRender())
|
||||
{
|
||||
if (!string.IsNullOrEmpty(ModuleState.ModuleType))
|
||||
{
|
||||
ModuleType = Type.GetType(ModuleState.ModuleType);
|
||||
if (ModuleType != null)
|
||||
{
|
||||
// repopulate the SiteState service based on the values passed in the SiteState parameter (this is how state is marshalled across the render mode boundary)
|
||||
ComponentSiteState.Hydrate(SiteState);
|
||||
|
||||
DynamicComponent = builder =>
|
||||
{
|
||||
builder.OpenComponent(0, ModuleType);
|
||||
builder.AddAttribute(1, "RenderModeBoundary", this);
|
||||
builder.CloseComponent();
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
// module does not exist with typename specified
|
||||
_messageContent = string.Format(Localizer["Error.Module.InvalidName"], Utilities.GetTypeNameLastSegment(ModuleState.ModuleType, 0));
|
||||
_messageType = MessageType.Error;
|
||||
_messagePosition = "top";
|
||||
_messageStyle = MessageStyle.Alert;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (string.IsNullOrEmpty(ModuleState.ModuleType))
|
||||
{
|
||||
_messageContent = string.Format(Localizer["Error.Module.InvalidType"], ModuleState.ModuleDefinitionName);
|
||||
_messageType = MessageType.Error;
|
||||
_messagePosition = "top";
|
||||
_messageStyle = MessageStyle.Alert;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
ModuleType = Type.GetType(ModuleState.ModuleType);
|
||||
var moduleName = Utilities.GetTypeNameLastSegment(ModuleState.ModuleType, 0);
|
||||
if (ModuleType == null)
|
||||
{
|
||||
// module does not exist with typename specified
|
||||
_messageContent = string.Format(Localizer["Error.Module.InvalidName"], moduleName);
|
||||
_messageType = MessageType.Error;
|
||||
_messagePosition = "top";
|
||||
_messageStyle = MessageStyle.Alert;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//only validate the services injection in development environment
|
||||
if (NavigationManager.BaseUri.Contains("localhost:") && !ValidateModuleTypeInjectedServices(ModuleType, out IList<string> missingServices))
|
||||
{
|
||||
// module type is not valid for instantiation
|
||||
_messageContent = string.Format(Localizer["Error.Module.InvalidInjectedServices"], string.Join(",", missingServices));
|
||||
_messageType = MessageType.Error;
|
||||
_messagePosition = "top";
|
||||
_messageStyle = MessageStyle.Alert;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// repopulate the SiteState service based on the values passed in the SiteState parameter (this is how state is marshalled across the render mode boundary)
|
||||
ComponentSiteState.Hydrate(SiteState);
|
||||
|
||||
DynamicComponent = builder =>
|
||||
{
|
||||
builder.OpenComponent(0, ModuleType);
|
||||
builder.AddAttribute(1, "RenderModeBoundary", this);
|
||||
builder.CloseComponent();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -165,4 +182,26 @@
|
||||
_error = "";
|
||||
base.Recover();
|
||||
}
|
||||
|
||||
private bool ValidateModuleTypeInjectedServices(Type moduleType, out IList<string> missingServices)
|
||||
{
|
||||
missingServices = new List<string>();
|
||||
|
||||
var properties = Utilities.GetPropertiesIncludingInherited(moduleType, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
||||
foreach(var property in properties)
|
||||
{
|
||||
var injectAttribute = property.GetCustomAttribute(typeof(InjectAttribute));
|
||||
if (injectAttribute != null)
|
||||
{
|
||||
var serviceType = property.PropertyType;
|
||||
var service = ServiceProvider.GetService(serviceType);
|
||||
if (serviceType != null && service == null)
|
||||
{
|
||||
missingServices.Add(Utilities.GetTypeNameLastSegment(serviceType.FullName, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return !missingServices.Any();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user