Reputation: 1772
I have a strange issue. I use Unity as IOC/DI. I want to create instance of class LoggerService, but the error appers 'LoggerService.LogException(System.Exception, string, string)' is inaccessible due to its protection level'. I have a few classes used by Unity which internal access modifier (no access modifier for class), and thay are accessible in code. Why it not working here?
My code is following:
//console app executable module:
static void CommonOnUnhandledException(Exception exception, string messagePrefix)
{
var logger = Ioc.Resolve<LoggerService>(Config);
logger.LogException(exception, String.Empty, String.Empty);
Console.WriteLine(String.Format("{0} unhandled exception: {1}; {2}", messagePrefix, exception.Message, exception.StackTrace));
}
//library Services.dll
class LoggerService : ILoggerService
{
public LoggerService(AppConfiguration configuration)
{
...
}
public void LogException(Exception ex, string url = "", string messageContent = "")
{
...
}
}
//library services dll, started in console app
public static class ServicesModule
{
public static void Init(UnityContainer container)
{
// register all other ordinary types:
container.RegisterType<IIntelShopService, IntelShopService>(new ContainerControlledLifetimeManager());
container.RegisterType<IBackendService, BackendService>(new ContainerControlledLifetimeManager());
container.RegisterType<IIntelODataService, IntelODataService>(new ContainerControlledLifetimeManager());
container.RegisterType<ILoggerService, Log4NetLoggerService>(new ContainerControlledLifetimeManager());
container.RegisterType<IPerformanceTrackerService, PerformanceTrackerService>(new ContainerControlledLifetimeManager());
}
}
Upvotes: 0
Views: 2564
Reputation: 10863
You could try to resolve ILoggerService
instead of a specific implementation.
If you resolve concrete types instead of interfaces, you're using only half of the power of unity or dependency injection/inversion of control in general.
BTW: the implementations should most of the time be internal
, while interfaces on the other hand tend to be public
.
Upvotes: 1
Reputation: 2277
You have to make your class public
. It's useful to always use a modificator for methods, properties and classes.
Upvotes: 2
Reputation: 960
You have to make your class public
. Without a modifier the default is internal
, which is not "visible" enough for your purposes. See also here.
Upvotes: 1