title: C# WCF: Having a single app.config in a shared library that provides access to the Service
body: <p>I currently have a solution with a couple of projects and one of them is a WCF Service.  I created another projected with a static class that basically provides a gateway to an instance of the WCF client, like such :</p>
  
  <pre><code>public static class WSGateway
  {
      public static DBInteractionGatewayClient MR_WebService
      {
          get
          {
              return new DBInteractionGatewayClient();
          } 
      }
  }
  </code></pre>
  
  <p>This is so that (or so I thought) I could use a single <code>app.config</code> file that will be in that library only and then other projects can just just reference it and get a reference to that client from that property.</p>
  
  <p>But the problem is that when a project tries to access that property, an exception is thrown telling me that I need to <code>app.config</code> in the application, and when I copy the <code>app.config</code> the my gateway library to the application, it works.</p>
  
  <hr>
  
  <p>Is there a way to avoid having multiple <code>app.config</code> files in the application and having just one in maybe a single library?</p>
  
  <hr>
  
  <p><strong>[Update] Solution:</strong></p>
  
  <p>Following <a href="https://stackoverflow.com/questions/746107/c-wcf-having-a-single-app-config-in-a-shared-library-that-provides-access-to-th/746174#746174">Anderson Imes</a>' suggestions, for now I decided to hardcode the client reference configuration in the class and thus eliminated the need for multiple <code>app.config</code>s.  </p>
  
  <p>Thus, I translated my configuration from this (<code>app.config</code>):</p>
  
  <pre><code>&lt;configuration&gt;
      &lt;system.serviceModel&gt;
          &lt;bindings&gt;
              &lt;wsHttpBinding&gt;
                  &lt;binding name="WSHttpBinding_IDBInteractionGateway" closeTimeout="00:01:00"
                      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                      bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                      maxBufferPoolSize="524288" maxReceivedMessageSize="6000000"
                      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                      allowCookies="false"&gt;
                      &lt;security mode="None"/&gt;
                      &lt;readerQuotas maxDepth="6000000" maxStringContentLength="6000000" maxArrayLength="6000000"
                          maxBytesPerRead="6000000" maxNameTableCharCount="6000000" /&gt;
                      &lt;reliableSession ordered="true" inactivityTimeout="00:10:00"
                          enabled="false" /&gt;
                  &lt;/binding&gt;
              &lt;/wsHttpBinding&gt;
          &lt;/bindings&gt;
          &lt;client&gt;
              &lt;endpoint address="http://agnt666laptop:28666/DBInteractionGateway.svc"
                  binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IDBInteractionGateway"
                  contract="DBInteraction_Service.IDBInteractionGateway" name="WSHttpBinding_IDBInteractionGateway"&gt;
                  &lt;identity&gt;
                      &lt;dns value="localhost" /&gt;
                  &lt;/identity&gt;
              &lt;/endpoint&gt;
          &lt;/client&gt;
      &lt;/system.serviceModel&gt;
  &lt;/configuration&gt; 
  </code></pre>
  
  <p>To this (a <code>static class</code>):</p>
  
  <pre><code>public static class WSGateway
  {
      private static WSHttpBinding binding;
      private static EndpointAddress endpointAddress;
  
      static WSGateway()
      {
          var readerQuotas = new XmlDictionaryReaderQuotas()
          {
              MaxDepth = 6000000,
              MaxStringContentLength = 6000000,
              MaxArrayLength = 6000000,
              MaxBytesPerRead = 6000000,
              MaxNameTableCharCount = 6000000
          };
          binding = new WSHttpBinding(SecurityMode.None) {MaxReceivedMessageSize = 6000000, ReaderQuotas = readerQuotas};
  
          endpointAddress = new EndpointAddress("http://agnt666laptop:28666/DBInteractionGateway.svc"); 
      }
      public static DBInteractionGatewayClient MR_WebService
      {
          get
          { 
              return new DBInteractionGatewayClient(binding, endpointAddress);
          }
      }
      public static void ExecuteCommand(Action&lt;DBInteractionGatewayClient&gt; command)
      {
          var ws = MR_WebService;
          command.Invoke(ws);
          ws.Close();
      }
  }
  </code></pre>