Open ABrizmohun opened 1 year ago
I have encountered the same error as you in my Umbraco 10 and 11 solutions. I haven't had the time to test it yet without PetaPoco ORM but could this be helpful? (from the uiomatic docs): https://timgeyssens.gitbook.io/ui-o-matic/12.advanced#iuiomaticobjectservice
I'm facing the same problem.
This should really be resolved by getting the service from the container but this used to work (not sure about newer c# versions) as a workaround
HttpContext.RequestServices.GetService(typeof(ISomeService));
I ended up forking this project and injecting IServiceProvider
into UIOMaticHelper
. Then in GetRepository
, I resolve my IUIOMaticRepository
from DI if it exists otherwise, it creates a new instance.
namespace UIOMatic
{
public class UIOMaticHelper : IUIOMaticHelper
{
private readonly AppCaches _appCaches;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IScopeProvider _scopeProvider;
private readonly UIOMaticObjectService _uioMaticObjectService;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger<IUIOMaticHelper> _logger;
public UIOMaticHelper(AppCaches appCaches,
IHostingEnvironment hostingEnvironment,
IScopeProvider scopeProvider,
UIOMaticObjectService uioMaticObjectService,
IServiceProvider serviceProvider,
ILogger<IUIOMaticHelper> logger)
{
_appCaches = appCaches;
_hostingEnvironment = hostingEnvironment;
_scopeProvider = scopeProvider;
_uioMaticObjectService = uioMaticObjectService;
_serviceProvider = serviceProvider;
_logger = logger;
}
public IUIOMaticRepository GetRepository(UIOMaticAttribute attr, UIOMaticTypeInfo typeInfo)
{
var existingRepositories = _serviceProvider.GetServices<IUIOMaticRepository>();
var existingRepository = existingRepositories.FirstOrDefault(x => x.GetType() == attr.RepositoryType);
if (existingRepository is not null)
{
return (IUIOMaticRepository)existingRepository;
}
return typeof(DefaultUIOMaticRepository).IsAssignableFrom(attr.RepositoryType)
? (IUIOMaticRepository)Activator.CreateInstance(attr.RepositoryType, attr, typeInfo, _scopeProvider, _uioMaticObjectService)
: (IUIOMaticRepository)Activator.CreateInstance(attr.RepositoryType, _scopeProvider);
}
Then in my project, I register my IUIOMaticRepository
into DI.
builder.Services.AddTransient<IUIOMaticRepository, ISBNRepository>();
builder.Services.AddTransient<IUIOMaticRepository, TitleRepository>();
I'm using UIOmatic 5.1.4 with Umbraco 10.4. I have my data in a database separate from the Umbraco DB. I have an Entity Framework project to access data from that database with services registered into DI on startup.
How can I resolve those services in my
AbstractUIOMaticRepository
?Example npoco
Example repository
Currently, I get a Constructor on type
System.MissingMethodException: [Redacted].ISBNRepository not found.
Stack Trace: