0

My asp.net static constructor which was getting called when sending mail

static EmailHelper()
        {
            //load configuration from config file

            int intSmtpPort = 0;
            int.TryParse(WebConfigurationManager.AppSettings["smtpport"], out intSmtpPort);
            host = WebConfigurationManager.AppSettings["smtpclient"];
            port = intSmtpPort;
            user = WebConfigurationManager.AppSettings["username"];
            password = WebConfigurationManager.AppSettings["password"];
            fromEmail = WebConfigurationManager.AppSettings["frommail"];

        }

My asp.net core static constructor class which throws error as it should be parameterless but i need to use the configuration

 static EmailHelper(IOptions<SmtpConfig> smtpConfig)
        {
            int intSmtpPort = 0;
            int.TryParse(SmtpConfig.smtpport, out intSmtpPort);
            host = SmtpConfig.smtpclient;
            port = intSmtpPort;
            user = SmtpConfig.username;
            password = SmtpConfig.password;
            fromEmail = SmtpConfig.frommail;

        }

If i use a public constructor its not getting called. What should i do? Is there another way to inject configuration settings in my class without using dependency injection?

1
  • Can you make the class an instance type instead of a static class? Commented Nov 16, 2016 at 14:37

1 Answer 1

1

A static constructor cannot be used for dependency injection.

Why don't you make a non-static constructor and register the EmailHelper as a service in the dependency injection?

This way you can inject and use the EmailHelper anywhere you need it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.