Search code examples
c#.netwcf

Self Hosting WCF service using app.Config files


I have self hosting WCF service that contains it's own app.Config to expose endpoints required for the service contracts. If the service is started in the programs.cs main method it all works just fine and the metadata is exposed via the browser. However, I created a HostService class based on the ServiceBase class which in the same host library and is instantiated within the program.cs file. The HostService class starts the service and has a timer method to ping other client web services for information.

My question is, when I created the HostService : ServiceBase class and instantiate it from the main(), I have to put a duplicate app.Config file in the Service Library in order for the endpoints to properly exposed and return the metadata/wsdl. I don't want to maintain 2 duplicate app.config files if possible. Currently the host library and service library both require one. Is there a way to only have just one w/ the host that could be used for both? Sorry for the dumb question, but I'm new to WCF =)

Program.cs

static void Main(string[] args){
  var service = new HostService(); 
  service.StartHostService(args);   
}

HostService.cs

public partial class HostService : ServiceBase
{
    internal void StartHostService(string[] args)
    {
        this.OnStart(args);
        Console.ReadLine();
        this.OnStop();
    }

    ....
}

Solution

  • Short answer is no. There must be two configs, one for the client that consumes the WCF and one for the server that exposes that communication methods with the WCF.

    In order for your client to work, your config must be set with Client Configuration

    <?xml version="1.0" encoding="utf-8"?>  
    <configuration>  
      <system.serviceModel>  
            <client>  
              <endpoint  
                name="endpoint1"  
                address="http://localhost/ServiceModelSamples/service.svc"  
                binding="wsHttpBinding"  
                bindingConfiguration="WSHttpBinding_IHello"  
                behaviorConfiguration="IHello_Behavior"  
                contract="IHello" >  
    
                <metadata>  
                  <wsdlImporters>  
                    <extension  
                      type="Microsoft.ServiceModel.Samples.WsdlDocumentationImporter, WsdlDocumentation"/>  
                  </wsdlImporters>  
                </metadata>  
    
                <identity>  
                  <servicePrincipalName value="host/localhost" />  
                </identity>  
              </endpoint>  
    // Add another endpoint by adding another <endpoint> element.  
              <endpoint  
                name="endpoint2">  
               //Configure another endpoint here.  
              </endpoint>  
            </client>  
    
    //The bindings section references by the bindingConfiguration endpoint attribute.  
        <bindings>  
          <wsHttpBinding>  
            <binding name="WSHttpBinding_IHello"   
                     bypassProxyOnLocal="false"   
                     hostNameComparisonMode="StrongWildcard">  
              <readerQuotas maxDepth="32"/>  
              <reliableSession ordered="true"   
                               enabled="false" />  
              <security mode="Message">  
               //Security settings go here.  
              </security>  
            </binding>  
            <binding name="Another Binding"  
            //Configure this binding here.  
            </binding>  
              </wsHttpBinding>  
            </bindings>  
    
    //The behavior section references by the behaviorConfiguration endpoint attribute.  
            <behaviors>  
                <endpointBehaviors>  
                    <behavior name=" IHello_Behavior ">  
                        <clientVia />  
                    </behavior>  
                </endpointBehaviors>  
            </behaviors>  
    
        </system.serviceModel>  
    </configuration> 
    

    notice the <client> tag specifying how the client must call the WCF.

    and with Server Config:

    <?xml version="1.0" encoding="utf-8"?>  
    <configuration>  
     <system.serviceModel>  
      <bindings>  
        <basicHttpBinding>  
         <binding name="myBindingConfiguration1" closeTimeout="00:01:00" />  
         <binding name="myBindingConfiguration2" closeTimeout="00:02:00" />  
         <binding closeTimeout="00:03:00" />  <!—- Default binding for basicHttpBinding -->  
        </basicHttpBinding>  
         </bindings>  
         <services>  
          <service name="MyNamespace.myServiceType">  
           <endpoint   
              address="myAddress" binding="basicHttpBinding"   
              bindingConfiguration="myBindingConfiguration1"  
              contract="MyContract"  />  
           <endpoint   
              address="myAddress2" binding="basicHttpBinding"   
              bindingConfiguration="myBindingConfiguration2"  
              contract="MyContract" />  
           <endpoint   
              address="myAddress3" binding="basicHttpBinding"   
              contract="MyContract" />  
           </service>  
          </services>  
        </system.serviceModel>  
    </configuration>  
    

    Notice there is no <client> tag here.