title: WCF : Is it possible to move all service settings outside the config file, and in the code?
body: <p>I have a WCF that is in use now for over a year, coded by someone else and i'm trying to refactor it but the way the folder structure is organized and the svc.cs file containing the methods is not what I am used to from WCF samples I <a href="http://msdn.microsoft.com/en-us/library/dd936243.aspx" rel="nofollow noreferrer">completed</a>.</p>
  
  <p>Here is what I'm trying to achieve. Since none of the settings changed over the last year I am asked to hardcode all the service's settings in code. The service is hosted within big and existing web application(and I have to leave it in there), the svc file contains the method getting called by the client, and this svc.cs file is not located at the root of the virtual directory but deeper, lets say <code>&lt;virtualDirectory&gt;/WebServices/PotatoWebService/PotatoWebService.svc</code>, does having it not in the virtual directory root cause any problem?</p>
  
  <p>Since I am moving the configuration out of the config file, I am now creating the service, host, endpoint, binding within the web application's global.axax.cs file, at <code>ApplicationStart()</code>, is that the right place to start this service within a web application?</p>
  
  <p>Is it possible to move everything in code, not just the endPoint but also the entire content of the behavior, service and system.ServiceModel found in the web.config file, in order to 'clean' the config file, and stop scaring support and config managers with settings they don't need?</p>
  
  <p>If it is, I have been trying for 2 days to get this to work without success. What I have now according to netstat is a service up and running but when I open my svc file in the browser an exception pops saying the service dosent have any endpoints defined, although according to nstat I have a service running:</p>
  
  <pre><code>netstat -a | find LISTENING
  
  ...
    TCP    machineName:8097      elamontagne.potato.com:0  LISTENING
  ...
  </code></pre>
  
  <p>So I have a service running but a svc file that is not aware of it and can't use it's endpoints, a client trying to contact the service and can't find it because service has no endpoint, any tips to narrow down the problem, tips for trouble shooting this situation?</p>
  
  <pre><code>     //Address
      string hostingAddress = "PotatoVirtualDirectory/PotatoService/PotatoService.svc";   
      // Binding
      BasicHttpBinding PotatoServiceBinding = ConfigurePotatoServiceBinding(serviceSecurityMode);
      // Contract
      Type PotatoContract = typeof(IPotatoService);
      // Host
      this.PotatoServiceHost = new ServiceHost(typeof(PotatoService), new Uri("http://localhost:8097"));
      // Endpoint
      this.PotatoServiceHost.AddServiceEndpoint(PotatoContract, PotatoBinding, hostingAddress);
      // Behavior
      ConfigurePotatoServiceBehavior(this.PotatoServiceHost, PotatoServiceBinding);
      // Name
      this.PotatoServiceHost.Description.Name = "PotatoNamespace.PotatoService";
      //Start!
      this.PotatoServiceHost.Open();
  </code></pre>
  
  <p>This compiles and run, it gives me an running service in opened state. The client can't find it at it's usual location, and the svc file complains it has nohting in the web.config and wants an endpoint ServiceHasZeroAppEndpoints: "Service has zero application (non-infrastructure) endpoints"</p>
  
  <p>I've step through the code in debug and all looks fine, service is openend, what I am missing, any tips to narrow down this problem?</p>
  
  <p><strong>Edit</strong>: Deriving from the service host factory worked well, thanks!</p>