none
Connection Strings in WCF Service

    Question

  • My current design has a web project which contains only the .svc file and a reference to a library project which contains the code for the service.  Based on all the examples I viewed prior to starting this project, that design was the most common.

    Now I am faced with a dilema, My boss wanted me to have a separate database for each company that the service is supposed to do work for.  Currently I am passing in the company name and using that in a lookup to get the connection string and the lookup opens a file is a static location on the server.  The file is also encrypted which adds additional overhead for my service calls as it must be decrypted each time.  This was the design that was imposed. 

    I have convinced the powers that be that this is not a good design (although it is a pretty secure design as the decrypted connection string lives in memory only a short time).  What I want to do is use the ConnectionString storage mechanism in IIS7.  However, since the web side of my project only consists of the .svc file I have no idea how to access these strings from a library that is running on the web server.  I also want to prevent a Service to Service call as well as this has its own overhead that I would like to avoid.

    Will I have to move my implementation and interface files on the webproject to accomplish this (so that ConfigurationManager works) or is there a way for me to access the webservers connection strings right within my library code?
    --------------------------------------------------------------------------------------------- No warranties are expressed or implied with any code being posted/emailed.
    Tuesday, May 19, 2009 10:23 PM

Answers

  • Just move the connection strings to your service's web config file (connectionStrings section) and access it using the ConfigurationManager.ConnectionStrings property.

    Is this what you were after?

    Regards,
    John
    • Marked as answer by Wizengamot Wednesday, May 20, 2009 9:01 PM
    Tuesday, May 19, 2009 10:35 PM
  • I think I understand what you're wanting here. What Slatkovi is saying is when you are running the WCF service under IIS (as SVC) and you are accessing the ConfigurationManager anywhere in an Assembly loaded by the IIS project, it goes all the way up to the Web.Config. The hierarchy is Machine.Config up to Web.Config.

    You can also add a custom XML Config file to any assembly by naming the XML file the same name as the Assembly and adding .config as the extension. In short you should be able to reference the Assembly from the other project you are talking about and when it is loaded in your WCF service you should have no problem with it "seeing" Web.Config values. Then you can encrypt the Web.Config file for security.

    The same would be true for say any WPF or Console app by putting values in its App.Config. All Assemblies loaded into its process should have access to its App.Config values. Hope this helps.
    • Marked as answer by Wizengamot Wednesday, May 20, 2009 9:01 PM
    Wednesday, May 20, 2009 12:59 AM

All replies

  • Just move the connection strings to your service's web config file (connectionStrings section) and access it using the ConfigurationManager.ConnectionStrings property.

    Is this what you were after?

    Regards,
    John
    • Marked as answer by Wizengamot Wednesday, May 20, 2009 9:01 PM
    Tuesday, May 19, 2009 10:35 PM
  • I appreciate your answer John, but I am not sure if that is the answer.  I know that I cannot access the connection strings from the web project within my library, already tried that.  Although I reserve the right to have made a mistake with that test.
    From what I understand, code running in the web project can access the connectionstrings property(because the configuration manager is accessing the web.config of the local project), but code running as a library cannot.  The structure of my code is not the same as creating a WCF service from the template provided by microsoft (which places the codebehind and the interface file in the same project).  My code references another project so that the code running within the web (where the .svc file is located) is already compiled. 
    The whole contents of the .svc file are as follows:
    <%@ ServiceHost Language="VB" Debug="true" Service="Hyduke.Services.CustomerManagementImplementation" %>
    Note the lack of a CodeBehind property.  That is because the code is accessed by reference.  The only "code" that is contained within the files that reside in the IIS folder is either compiled or is the XML as listed above.  This is by design.

    This lack of code in the web project means that I cannot access the connection strings in web.config.  I was hoping there would be a way for me to access the IIS global connection strings within the compiled libraries, but so far no luck.

    Cheers,
    Brent


    --------------------------------------------------------------------------------------------- No warranties are expressed or implied with any code being posted/emailed.
    Tuesday, May 19, 2009 11:31 PM
  • I think I understand what you're wanting here. What Slatkovi is saying is when you are running the WCF service under IIS (as SVC) and you are accessing the ConfigurationManager anywhere in an Assembly loaded by the IIS project, it goes all the way up to the Web.Config. The hierarchy is Machine.Config up to Web.Config.

    You can also add a custom XML Config file to any assembly by naming the XML file the same name as the Assembly and adding .config as the extension. In short you should be able to reference the Assembly from the other project you are talking about and when it is loaded in your WCF service you should have no problem with it "seeing" Web.Config values. Then you can encrypt the Web.Config file for security.

    The same would be true for say any WPF or Console app by putting values in its App.Config. All Assemblies loaded into its process should have access to its App.Config values. Hope this helps.
    • Marked as answer by Wizengamot Wednesday, May 20, 2009 9:01 PM
    Wednesday, May 20, 2009 12:59 AM
  • Thanks to both of you.  I have this working now.  I had attempted to access those strings from that object before, but I had a bug in my code that was making me think that it was not working.  Since you both told me that it MUST work as I thought it did, I found the bug upon further review and now this is working like a charm.  I can access the connection strings defined in IIS, which is what I wanted. 

    Cheers,
    B

    --------------------------------------------------------------------------------------------- No warranties are expressed or implied with any code being posted/emailed.
    Wednesday, May 20, 2009 9:04 PM