我有以下代码
SmtpSection smtpsection =
(SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
EmailSettings emailSettings = new EmailSettings
{
WriteAsFile = bool.Parse(ConfigurationManager.AppSettings[""] ?? "false"),
ServerPort = smtpsection.Network.Port,
Username = smtpsection.Network.UserName,
Password = smtpsection.Network.Password,
UseSsl = smtpsection.Network.EnableSsl
};
kernel.Bind<IEmailRepository>().To<EmailRepository>().WithConstructorArgument("settings", emailSettings);
和
public class EmailSettings
{
public string MailToAddress = "test@Test.com";
public string MailFromAddress = "test@test.com";
public bool UseSsl = true;
public string Username = "";
public string Password = "";
public string ServerName = "";
public int ServerPort = 587;
public bool WriteAsFile = false;
public string FileLocation = @"C:\sports_store_emails";
}
public class EmailController : Controller
{
private IEmailRepository emailRepository;
private EmailSettings emailSettings;
public EmailController(IEmailRepository emailRepository, EmailSettings settings)
{
this.emailRepository = emailRepository;
this.emailSettings = settings;
}
}
public class EmailRepository : RepositoryBase<EmailEnquiry>, IEmailRepository
{
public EmailRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
}
public interface IEmailRepository : IRepository<EmailEnquiry>
{
}
public interface IRepository<T> where T : class
{
void Add(T entity);
void Update(T entity);
void Delete(T entity);
void Delete(Expression<Func<T, bool>> where);
T GetById(long Id);
T GetById(string Id);
T Get(Expression<Func<T, bool>> where);
IEnumerable<T> GetAll();
IEnumerable<T> GetMany(Expression<Func<T, bool>> where);
IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate);
}
由于某种原因,NInject 的参数设置没有被传入。有谁知道这是为什么。settings 使用的是内部的所有值,EmailSettings
而不是 NInject 传入的值?