0

History: I have a scenario where I have several project(let's say 4) in solution where I have a WCF service, a Website and a shared class library. I have to send email to users on demand and also schedule the email for WCF service. As the email configuration is same, I was thinking to move them to shared library project so that I can consume the service from both web and webservice project when needed.

The problem: the problem is, when i moved the configuration to app.config of the shared project, it is not behaving as expected. I know that if i move this code to web.config it will work. :

<system.net>
<mailSettings>
  <smtp from="noreply@webcruiter.com">
    <network host="smtp.sendgrid.net" userName="candidatedev" defaultCredentials="false" password="foxed-kqStTg1" port="587" enableSsl="true" />
  </smtp>
</mailSettings>

but I want the service to consume the configuration from class library instead of web.config.

this is my email service which is now in shared library:

public class EmailService : IEmailService
{
    public void SendEmail(MailMessage message)
    {
        SmtpClient smtpClient = new SmtpClient();
        smtpClient.Send(message);
        message.Dispose();
    }
}

the suggested answer for this in comment is not answering my query. I want to centralize the email setting as it is same for both project. I need ideas or suggestion about how to go around and make it work the way i want.

  • In my experience, you should either configure it from the consuming application, or roll your own configuration file system (perhaps a JSON or XML file) that you set to be included with your class library. – mason Sep 11 '17 at 14:38
  • Settings are always read from the config of the startup project. You could move them to a separate file? – stuartd Sep 11 '17 at 14:38
  • 2
    class libraries don't have their own config. (Unfortunately, some VS tooling will automatically add one). At best, the `app.config` for a class library *suggests* what config you need to include in the `web.config` or the *exe*'s `app.config`. – Damien_The_Unbeliever Sep 11 '17 at 14:38
  • Possible duplicate of [Can a class library have an App.config file?](https://stackoverflow.com/questions/4817051/can-a-class-library-have-an-app-config-file) – maccettura Sep 11 '17 at 14:41
  • so, is there no custom way to read configuration from app.config instead of read it from startup project? i was trying to centralize the config for that email setting. – Md. Tazbir Ur Rahman Bhuiyan Sep 11 '17 at 14:41
  • 1
    If you read through some of the answers from the duplicate question, you will see the question asked: "Why would you want to do this?". Class libraries are intended to be portable, including a config with the library would counter that design. – maccettura Sep 11 '17 at 14:44
  • maccettura, i understand your view, but how can i centralize the email settings then? i want it to be in one place as it is same for both web and wcf project. When changes will need to make, I want to make changes to library only to make a swift delivery for both wcf and web project. – Md. Tazbir Ur Rahman Bhuiyan Sep 11 '17 at 14:48
  • 1
    if you're set on doing this just make your own xml file in the class library and have it read from that. To eliminate duplicate code just put a static method in the class library that that reads the xml file and returns the settings, then have your service and site call the static method – chris-crush-code Sep 11 '17 at 14:49
  • You could cut out the shared class library and have both projects read from the same xml file – chris-crush-code Sep 11 '17 at 14:50
  • Thats a very bad idea. Configs are cached, XML parsing has high overhead. – maccettura Sep 11 '17 at 14:50
  • @maccettura I agree but if they want to avoid have the email settings duplicated then they pay the cost with performance – chris-crush-code Sep 11 '17 at 14:52
  • 1
    @maccettura You could read the configuration once the first time it's run and then cache it in memory. Best of both worlds. – mason Sep 11 '17 at 14:56
  • It just seems silly to me. Email settings are unlikely to change often (or else they wouldnt be stored in a config to begin with). Why not just change in both places when you need to? If this is an environment issue, just use web config transforms. – maccettura Sep 11 '17 at 14:57
  • If you're dead set on keeping them in the shared library then you could put them into your code directly. See http://csharp.net-informations.com/communications/csharp-smtp-mail.htm – JConstantine Sep 11 '17 at 15:51
  • I think I will wait for the design decision from my team. for now I will keep separately in projects. keeping configuration in C# has it's own cost. I will have to compile it if any changes is needed. thanks for the suggestions by the way.. :) – Md. Tazbir Ur Rahman Bhuiyan Sep 12 '17 at 06:08

0 Answers0