I am kind of new to the dependency injection resolver techniques topic. May I know how we can resolve dependency through configuration file section(s). Below are my classes and interface along with the config file.
I am sure that missing some portion of code/setting. Can you please help me with this.
public interface IUserAuthentication
{
string Authenticate(string username, string password);
string GetUserRole(string username);
}
public class CloudAppAuthetication : IUserAuthentication
{
public string Authenticate(string username, string password)
{
//Jwt token based authentication logic should be there
return "This Authenticate method executed from cloud class";
}
public string GetUserRole(string username)
{
//New logic to user management api call
return "This GetUserRole method executed from cloud class";
}
}
public class StandaloneAppAuthetication : IUserAuthentication
{
public string Authenticate(string username, string password)
{
//current logic should be here
return "This Authenticate method executed from standalone class";
}
public string GetUserRole(string username)
{
//current logic should be here
return "This GetUserRole method executed from standalone class";
}
}
Console application calling of interface method:
class Program
{
static void Main(string[] args)
{
IUnityContainer container = new UnityContainer();
container.LoadConfiguration("TestContainer");
IUserAuthentication _userAuthentication = null;
string validatedUser = _userAuthentication.Authenticate(
"testuser@user.com", "testpassword");
string validatedUserRole =
_userAuthentication.GetUserRole("testuser@user.com");
}
}
My App.config file of console application is:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns = "http://schemas.microsoft.com/practices/2010/unity" >
< container name="TestContainer">
<register
type="UnityConfiguration_Testing.IUserAuthentication,UnityConfiguration_Testing"
mapTo="UnityConfiguration_Testing.StandaloneAppAuthetication,UnityConfiguration_Testing"
/>
</container>
</unity>
<startup>
<supportedRuntime version = "v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I am getting object reference not set to an instance of object error. Expecting that StandaloneAppAuthetication
class method will get executed as per my configuration.