My setup is :
<PackageReference Include="Prism.DryIoc.Extensions" Version="7.2.0.1054" />
<PackageReference Include="Prism.Forms.Extended" Version="7.2.0.1054" />
<PackageReference Include="Shiny.Prism" Version="7.2.0.1054" />
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterServices(services =>
{
services.AddLogging();
}
}
and when i try to get service to assign to property as below :
` Logger = provider.GetRequiredService<ILogger<AgentBase>>(); `
This error happens :
`No service for type 'Microsoft.Extensions.Logging.ILogger`1[Hyperledger.Aries.Agents.AgentBase]' has been registered.`
is there any missing config ?
In my project Dryioc is the main container that prism use for Di but my objects are completely depend on Microsoft DI, also some of class must registered from internal access modifier.
Replacing DryIOC with Microsoft Di not recommended by prism team, but there is an extension for combining .
finally after combining them and some change in dryioc setting for internal class registration, it worked .
Also error was solved after Host Builder correct initialization.
Thankyou
#habib not sure how host Builder caused this error, as according to error Logger is not getting resolved. As you are using Shiny.Prism, you can use ShinyStartup to register ILogger. You can register Logger as open generic using:
services.AddSingleton(typeof(ILogger<>), typeof(Logger<>));
Or, you can use Delegate Registration from Prism container Extension, if you need more control over how ILogger is getting resolved Delegate Registration
P.S can you share how this is linked to Host Builder and your implementation of it to solve it
Related
Trying to use Prism 8, ContainerLocator, IHttpClientFactory and Platform-Specific Service registration with DryIoc Extensions (not Magician) in a Xamarin Forms application
I have these nuget packages installed
Prism.DryIoc.Extensions
Prism.Forms
My main Xamarin Application inherits from PrismApplicationBase.
In platform code (eg Android) I have IPlatformInitializer implented by MainActivity and for platform-specific services I use:
LoadApplication(new App(this));
as documented here:
https://prismlibrary.com/docs/dependency-injection/platform-specific-services.html
This line causes the IContainerExtension to be resolved in the shared code
protected override IContainerExtension CreateContainerExtension()
...but I can't find a way to successfully return a valid IContainerExtension implementation.
I've attempted:
https://prismlibrary.com/docs/dependency-injection/container-locator.html
var createContainerExtension = () => new DryIocContainerExtension();
ContainerLocator.SetContainerExtension(createContainerExtension);
But the code given here can't even compile - the DryIocContainerExtension class created is defined INTERNAL and isn't available to my application code.
If I use...
PrismContainerExtension.Init();
or
return ContainerLocator.Current;
...as worked previously (eg Prism 7.2) I get a runtime error as described here:
https://githubmemory.com/repo/dansiegel/Prism.Container.Extensions/issues/180
ValueFactory attempted to access the Value property of this instance.
Please can someone advise what I'm doing wrong, or do I have to get my company to pay for Magician to resolve my issue?
So I was jsut wondering why in the Prism Doc and VS template this line is included in the App Class.
public App() : this(null) { }
Since today I commented it out and the App still started without any issues in both iOs and Android.
Best Regards
Basecrusher
If you don't need/want an IPlatformInitializer, it's fine to pass null, and the default implementation does so.
I guess the code is in there to remind you that you could pass an IPlatformInitializer if you needed/wanted to.
"With Xamarin.Forms you may have read how you can add the Dependency attribute for an impelementing type in your Platform Specific code and then resolve it with the Xamarin.Forms DependencyService. This is considered a major Anti-Pattern that should be avoided when you are using a proper Dependency Injection container. It is for this reason that Prism has dropped all support for working with the DependencyService as of Prism 7.0. Starting with Prism 6.3 the IPlatformInitializer was introduced. This allows you to easily register types with Prism's container."
https://prismlibrary.com/docs/xamarin-forms/dependency-injection/platform-specific-services.html
I have a few question related to Xamarin Prism dependency, could you please help me?
In the App.xmal.cs I've registered a service called UserService
protected override void RegisterTypes()
{
Container.RegisterType<IUserService, UserService>(); ....
And this works perfectly via ViewModels as in numerous examples on the Internet.
But in my UC on application load I need to call a method from UserService to check some login details.
At the moment on OnInitialized I redirect a user to some dummy page in order to use dependency instanced UserService.
Is it possible to get an instance of UserService directly in the App.xaml.cs via Prism dependency injection (outside of a model)
Rather easy:
Container.Resolve<IUserservice>();
...not before the registration, of course...
...and not nice, too, try not to use the container directly, but in this case, it's your only option.
I have a fair idea of using the Repository Pattern and have been attempting to "upgrade" our current way of creating ASP .Net websites. So i do the following
Create a solution with a class project called DataAccessLayer and another class project called BusinessLogicLayer. Finally a 3rd project which is my ASP .Net website (a normal site).
I add a dbml file to the DAL and drag a table, then in my BLL i add an interface and a class which implements this interface:
My interface
namespace BLL.Interfaces
{
interface IUser
{
List<User> GetAllUsers();
}
}
In my class
namespace BLL.Services
{
public class UserService : BLL.Interfaces.IUser
{
public List<User> GetUsers()
{
throw new NotImplementedException();
}
}
}
I know the code is not fully completed, but there for illustrative purposes.
So i right click the BLL project > Manage NuGet Packages > Searched for Ninject and found a few. I was overwhelmed with the number of entries returned after after further research i am lost in how to add Ninject to a normal ASP .Net website? Specifically which addin i require? As there are many MVC and reading further i think im a little confused.
I was trying to add it to the BLL project as thats where i THINK it should go so i can register my services in there.
Could anyone guide me in what i need to so in order to use Ninject entries but im not using MVC?
Install Ninject.Web either from "Package Manager Console" or NuGet.
Version is 3.2.1 as of this writing.
OR
It will install the following 4 packages -
Sample Service Class
public interface IUserService
{
List<string> GetUsers();
}
public class UserService : IUserService
{
public List<string> GetUsers()
{
return new List<string> {"john", "eric"};
}
}
Then add binding to ~/App_Start/NinjectWebCommon.cs.
In code behind page, property inject using [Inject] attribute.
In Addition in answer by win I would advise people not to get confused by using Constructor based injection in ASP.NET Webforms as Web Forms doesn't support constructor based injection simply. In default configuration they only support Property based Injections as already demonstrated by Win.
After some struggling I managed to get a working ASP.NET webforms application that uses the Ninject logging extension and log4net as logging framework. (credits to this blog for the basics).
But I have some questions on how to continue.
First, I needed to make the ILogger property public, because it remained null if it was private or protected.
So now I have this:
[Inject]
protected ILogger _logger { get; set; }
Instead of:
private static readonly ILog Logger = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
I can live with it, but it seems strange.
Another thing is the ThreadContext that log4net uses to fill the current context. I can still use it like this
using (ThreadContext.Stacks["NDC"].Push(MethodBase.GetCurrentMethod().Name))
{
_logger.Info("test");
}
But offcourse that takes away all the abstraction that I just added..
So I'm looking for some experiences / best practices that other people may have with this scenario.
Additional question: I've upgraded log4net to 1.2.11.0 which is the current version in NuGet, and now Ninject.Logging.log4net is broken because it expects version 1.2.10.0 ... is there a way to fix that?
Additional question: I've upgraded log4net to 1.2.11.0 which is the
current version in NuGet, and now Ninject.Logging.log4net is broken
because it expects version 1.2.10.0 ... is there a way to fix that?
Did you try to fix the allowed versions with [xx] in the Packages.config file ?
<package id="log4net" version="1.2.10" allowedVersions="[1.2.10]" /> />
It will prevent next updates to upgrade to 1.2.11 or more.
http://docs.nuget.org/docs/reference/versioning