3

I have a WebAPI project containing services to my project, and an integration project that handles things such as sending emails.

I have originally had my mail settings in the web.config of the API project and instantiating the SmtpClient quite happily and sending emails with no problem.

Having refactored and moved the mail sending to the integration project, including moving the mail settings to the app.config of the integration project, I now find that my app can't locate the mail settings in the app.config.

I could always move the mail settings into the web.config of the calling project but it seems to me that the settings and the code actually doing the sending should live in the same project.

Is there a way to have the code in the integration project pick up the settings from the app.config of the integration project rather than the web.config of the calling project?

serlingpa
  • 12,024
  • 24
  • 80
  • 130
  • you could set up the whole thing in your integration project, in a static class may be. – Amit Kumar Ghosh Aug 10 '15 at 11:55
  • http://stackoverflow.com/questions/6150644/change-default-app-config-at-runtime – user1666620 Aug 10 '15 at 11:56
  • As for me this `it seems to me that the settings and the code actually doing the sending should live in the same project` is not correct. There are a lot of material (first google result is [here](http://stackoverflow.com/questions/594298/c-sharp-dll-config-file)) where described why we should not add own configuration to dll, but should provide it from consuming project. **P.S.** I supposed that your email sending code was in separate dll – Disappointed Aug 10 '15 at 12:10
  • @AmitKumarGhosh Often it's not acceptable approach because you will not be able to change setting during runtime. Only change code --> Rebuild-->Republish – Disappointed Aug 10 '15 at 12:13

1 Answers1

0

If your final outcome (deployment package to production) is a website or hosted Web Api then the Smtp settings has to be in the web.config file. On publish the integration project will be converted to DLLs and it can read settings from web.config file.

TejSoft
  • 3,213
  • 6
  • 34
  • 58
  • Ok that makes a lot of sense. Is the app.settings not available in production? – serlingpa Aug 10 '15 at 12:36
  • The final package will have only one web.config or app.config depending on the project type. Web projects will have web.config whereas exe or desktop applications will have app.config. – TejSoft Aug 11 '15 at 00:12