Hello,
I am trying to config two things which are relevant to IConfigurationRoot to get config inside AppSettings.json.
I want to inject IConfiguration to file AngularAppUrlService.cs. However, those fields are read-only therefore I cannot assign new value to it.
I implement new class for Email Sender Service and I put it outside ASPNETZero Sourcecode. However, I cannot inject IConfigurationRoot to my class. I got this error.
Please help me to archive this. Thank you
7 Answer(s)
-
0
Hi @Khai,
Sorry for the delay, we have missed your question somehow.
- You canfirst add setters for those fields in AppUrlServiceBase:
public abstract string EmailActivationRoute { get; set; }
Then you can change the fields in AngularAppUrlService like this:
public override string EmailActivationRoute { get; set; }
After all, you can set the fields. I suggest you to create an Init method for AppUrlServiceBase and do it in that method instead of it's constructor.
- You need to define a module for your class library and depend on the module which contains IConfigurationRoot. Or instead of injecting IConfigurationRoot, you can simply use
IConfigurationRoot _appConfiguration = AppConfigurations.Get( typeof(NameOfYourModuleWhichContainsConfigFile).GetAssembly().GetDirectoryPathOrNull() );
-
0
For the first question, after I added setter to both of the override fields, I see that the instance of AppUrlService is NullAppUrlService and I cannot use it. Please help me to figure out that.
-
0
Hi @Khai,
So, your project builds but is this happening on runtime ? If so, can you share your AppUrlServiceBase, AngularAppUrlService, MvcAppUrlService and NullAppUrlService ?
Thanks.
-
0
Hello, these are my files.
1. AppUrlServiceBase
public abstract class AppUrlServiceBase : IAppUrlService, ITransientDependency { public abstract string EmailActivationRoute { get; set; } public abstract string PasswordResetRoute { get; set; } protected readonly IWebUrlService WebUrlService; protected readonly ITenantCache TenantCache; protected AppUrlServiceBase(IWebUrlService webUrlService, ITenantCache tenantCache, IConfigurationRoot appConfigurationRoot) { WebUrlService = webUrlService; TenantCache = tenantCache; this.Initialize(appConfigurationRoot); } private void Initialize(IConfigurationRoot appConfigurationRoot) { EmailActivationRoute = appConfigurationRoot["WebUIConfig:EmailActivationRoute"]; PasswordResetRoute = appConfigurationRoot["WebUIConfig:PasswordResetRoute"]; } public string CreateEmailActivationUrlFormat(int? tenantId) { return CreateEmailActivationUrlFormat(GetTenancyName(tenantId)); } public string CreatePasswordResetUrlFormat(int? tenantId) { return CreatePasswordResetUrlFormat(GetTenancyName(tenantId)); } public string CreateEmailActivationUrlFormat(string tenancyName) { var activationLink = WebUrlService.GetSiteRootAddress(tenancyName).EnsureEndsWith('/') + EmailActivationRoute + "?userId={userId}&confirmationCode={confirmationCode}"; if (tenancyName != null) { activationLink = activationLink + "&tenantId={tenantId}"; } return activationLink; } public string CreatePasswordResetUrlFormat(string tenancyName) { var resetLink = WebUrlService.GetSiteRootAddress(tenancyName).EnsureEndsWith('/') + PasswordResetRoute + "?userId={userId}&resetCode={resetCode}"; if (tenancyName != null) { resetLink = resetLink + "&tenantId={tenantId}"; } return resetLink; } private string GetTenancyName(int? tenantId) { return tenantId.HasValue ? TenantCache.Get(tenantId.Value).TenancyName : null; } }
2. AngularAppUrlService
public class AngularAppUrlService : AppUrlServiceBase { public override string EmailActivationRoute { get; set; } public override string PasswordResetRoute { get; set; } public AngularAppUrlService( IConfigurationRoot appConfigurationRoot, IWebUrlService webUrlService, ITenantCache tenantCache ) : base( webUrlService, tenantCache, appConfigurationRoot ) { } }
3. MvcAppUrlService (I don't see this file)
4. NullAppUrlService
public class NullAppUrlService : IAppUrlService { public static IAppUrlService Instance { get; } = new NullAppUrlService(); private NullAppUrlService() { } public string CreateEmailActivationUrlFormat(int? tenantId) { throw new NotImplementedException(); } public string CreatePasswordResetUrlFormat(int? tenantId) { throw new NotImplementedException(); } public string CreateEmailActivationUrlFormat(string tenancyName) { throw new NotImplementedException(); } public string CreatePasswordResetUrlFormat(string tenancyName) { throw new NotImplementedException(); } }
-
0
Hi,
Actually, I wanted you to add a public Initialize method like this:
public abstract class AppUrlServiceBase : IAppUrlService, ITransientDependency { public abstract string EmailActivationRoute { get; set; } public abstract string PasswordResetRoute { get; set; } protected readonly IWebUrlService WebUrlService; protected readonly ITenantCache TenantCache; IConfigurationRoot _appConfigurationRoot; protected AppUrlServiceBase(IWebUrlService webUrlService, ITenantCache tenantCache, IConfigurationRoot appConfigurationRoot) { WebUrlService = webUrlService; TenantCache = tenantCache; _appConfigurationRoot = appConfigurationRoot; } public void Initialize() { EmailActivationRoute = _appConfigurationRoot["WebUIConfig:EmailActivationRoute"]; PasswordResetRoute = _appConfigurationRoot["WebUIConfig:PasswordResetRoute"]; } public string CreateEmailActivationUrlFormat(int? tenantId) { return CreateEmailActivationUrlFormat(GetTenancyName(tenantId)); } public string CreatePasswordResetUrlFormat(int? tenantId) { return CreatePasswordResetUrlFormat(GetTenancyName(tenantId)); } public string CreateEmailActivationUrlFormat(string tenancyName) { var activationLink = WebUrlService.GetSiteRootAddress(tenancyName).EnsureEndsWith('/') + EmailActivationRoute + "?userId={userId}&confirmationCode={confirmationCode}"; if (tenancyName != null) { activationLink = activationLink + "&tenantId={tenantId}"; } return activationLink; } public string CreatePasswordResetUrlFormat(string tenancyName) { var resetLink = WebUrlService.GetSiteRootAddress(tenancyName).EnsureEndsWith('/') + PasswordResetRoute + "?userId={userId}&resetCode={resetCode}"; if (tenancyName != null) { resetLink = resetLink + "&tenantId={tenantId}"; } return resetLink; } private string GetTenancyName(int? tenantId) { return tenantId.HasValue ? TenantCache.Get(tenantId.Value).TenancyName : null; } }
Then, you can use it when you inject IAppUrlService (also add Initialize method to interface as well).
But, the thing I don't understand is, do you have a build error or runtime error ?
-
0
Hello, The error appears at runtime (it means I can compile it successfully). Is that a bug?
-
0
Hi @Khai,
I haven't tried this code in runtime but it is not designed to work this way initially. Can you share the runtime exception message ?