In the first step I'm injecting Services to retrieve Data from a third-party-API into my Umbraco-Controllers. This works fine.
Now I have a custom dashboard which saves the API-Urls and I'm trying to inject a service to get these URLs into the other Services. This doesn't work. And I'm stuck.
A Service will look something like this:
namespace My.Namespace.Interfaces.Services
{
public interface ISomeModelService : IDisposable
{
List<SomeModel> GetSomeModel(string parameter);
}
}
namespace My.Namespace.Umbraco.Shared.Services
{
public class SomeModelService : ISomeModelService
{
public void Dispose()
{
}
public List<SomeModel> GetVertragskonten(string parameter)
{
}
}
}
My Global Asax:
namespace Namespace
{
public class Global : UmbracoApplication
{
protected override void OnApplicationStarted(object sender, EventArgs e)
{
base.OnApplicationStarted(sender, e);
var configuration = GlobalConfiguration.Configuration;
var builder = new ContainerBuilder();
builder.RegisterInstance(ApplicationContext.Current).AsSelf();
//register all controllers found in this assembly
builder.RegisterControllers(Assembly.GetExecutingAssembly());
var assemblies = AppDomain.CurrentDomain.GetAssemblies().Where(a => a.FullName.StartsWith("My.Namespace."));
foreach (var a in assemblies)
{
builder.RegisterControllers(a);
builder.RegisterApiControllers(a);
}
builder.RegisterApiControllers(typeof(UmbracoContext).Assembly);
builder.RegisterType<SomeModelService>().As<ISomeModelService>();
var container = builder.Build();
//setup the webapi dependency resolver to use autofac
configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
//setup the mvc dependency resolver to user autofac
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
}
}
Some Umbraco Controller
namespace Namespace.Controllers
{
public class SomeController : SurfaceController
{
private ISomeModelService someModelService;
public KontoController(ISomeModelService s)
{
this.someModelService = s;
}
}
}
How would I inject a Service that can retrieve Data from Umbraco into the other Service. I have problems with ciruclar build dependency.
Thanks for your help.
You could just have the service you need injected via constructor injection in your service.
To do that, make sure you register the service interface (IDataTypeService or whatever you need to use) in your IoC container so it knows how to resolve it for using it when constructing your service.
When that is done you should be able to just use:
public class SomeModelService : ISomeModelService
{
private readonly IDataTypeService _dataTypeService;
public SomeModelService(IDataTypeService dataTypeService)
{
_dataTypeService = dataTypeService;
}
public List<SomeModel> GetVertragskonten(string parameter)
{
_dataTypeService.dostuff();
}
}
Circular Dependency Injection Problem
I have a Problem with a dependency injection.
In the first step I'm injecting Services to retrieve Data from a third-party-API into my Umbraco-Controllers. This works fine.
Now I have a custom dashboard which saves the API-Urls and I'm trying to inject a service to get these URLs into the other Services. This doesn't work. And I'm stuck.
A Service will look something like this:
My Global Asax:
Some Umbraco Controller
How would I inject a Service that can retrieve Data from Umbraco into the other Service. I have problems with ciruclar build dependency. Thanks for your help.
You could just have the service you need injected via constructor injection in your service.
To do that, make sure you register the service interface (IDataTypeService or whatever you need to use) in your IoC container so it knows how to resolve it for using it when constructing your service.
When that is done you should be able to just use:
But the build-order will still be a problem, won't it?
I think it is a bit hard to answer without knowing a bit more about what you're trying to do here.
You are trying to use the Umbraco services for getting something stored inside Umbraco I assume - in your own service... is that correct?
If that is the case I don't see where the circular dependency would come from as our services do not rely on any of your services.
If you could highlight a bit more precisely on where your problem arises, I'll see if I can give you some more answers ;)
I have Services which inject a 3rd party API into Umbraco. So my Umbraco-System depends on these Services.
Now I need to inject Data from a Custom-Dashboard into these Services which makes them dependend on Umbraco.
I think that's the reason my project doesn't build.
is working on a reply...