Reputation: 229
I am attempting to use dependency injection (.NET Framework 4.6, ASP.NET MVC), but I am getting the error: No parameterless constructor defined for this object.
If I also provide a parameterless constructor for HomeController
then _token
is null.
Owin Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IGenerateToken, GenerateTokenService>();
}
Controller:
public class HomeController : Controller
{
private readonly ueSecurityToken _token;
public HomeController(ueSecurityToken token)
{
_token = token;
}
public async Task<ActionResult> RunCreditCard()
{
var client = new UsaEPayClient();
var request = new TransactionRequestObject
{
CreditCardData = new CreditCardData()
{
CardNumber = "4000100011112224",
CardExpiration = "0919",
CardCode = "123"
}
};
var response = await client.runSaleAsync(_token, request);
return View(response);
}
}
GenerateTokenService:
public ueSecurityToken GenerateToken(string key, string pin)
{
var token = new ueSecurityToken
{
SourceKey = key,
ClientIP = new WebClient().DownloadString("http://bot.whatismyipaddress.com/")
};
var hash = new ueHash
{
Type = "md5",
Seed = Guid.NewGuid().ToString()
};
var input = string.Concat(token.SourceKey, hash.Seed, pin); // combine data into single string
// Create a new instance of the MD5CryptoServiceProvider object.
var md5Hasher = MD5.Create();
// Convert the input string to a byte array and compute the hash.
var data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
// Create a new Stringbuilder to collect the bytes
// and create a string.
var sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
foreach (var t in data)
{
sBuilder.Append(t.ToString("x2"));
}
hash.HashValue = sBuilder.ToString();
token.PinHash = hash;
return token;
}
public interface IGenerateToken
{
ueSecurityToken GenerateToken(string key, string pin);
}
Upvotes: 2
Views: 6453
Reputation: 113
Ran into a similar problem, after digging out found the article above. I had to modify the method BuildUnityContainer to register my service.
Upvotes: 0
Reputation: 3914
With ASP.NET 6 you might also get this error with a Blazor/Razor component. The component needs the empty constructor.
To solve this, you can use a property and mark it with Inject
like this:
[Inject]
protected IDataAccess DataRepository { get; set; }
More info: Microsoft Docs
Upvotes: 0
Reputation: 1716
Instead of a comment... the DI can't resolve this, and thus you get an error :) What does GenerateToken do? Is this a static function/class? You might want to create an interface:
public interface IGenerateToken
{
string GenerateToken();
}
and a class:
public class GenerateTokenService : IGenerateToken
{
//your logic
}
then in your DI you can add a singleton for creating a token:
services.AddSingleton<IGenerateToken, GenerateTokenService >();
And in your constructur:
private readonly IGenerateToken _token;
public HomeController(IGenerateToken token)
{
_token = token;
}
This makes testing also better :)
Try it!
Upvotes: 0
Reputation: 1716
Sure you can use DI. I prefer Simple Injector because it is simple and most cases it covers all the scenarios. For more and advancing you can use Autofac or Ninject.
But your method in controller it not hit by the DI. They set the constructors for the classes. So your DI has no use in this case.
Upvotes: 1