I am totally new to WCF and I am still learning the basics. What I've learned so far was that I can create a service and configure its endpoints and behaviors in the service's config file. And when I run my service through visual studio, a default application will be created and the service will be hosted in IIS successfully and everything works great.

Now when I create a host application for my service, I figured out that I should add the service endpoints (and behaviors) for my service again in the code as following:

ServiceHost host = new ServiceHost(typeof(HelloService));
host.AddServiceEndpoint(typeof(IHelloWorld), 
                        new WSHttpContextBinding(), 
                        "http://localhost:8873/helloworld/ws");

host.Open();

foreach (var se in host.Description.Endpoints)
{
    Console.WriteLine(se.Address);
}

host.Close();
Console.Read();

or I can do it in the host application's config file

So here are my question :

  1. what is the point of defining endpoints in service's own config file when it is not even useful in a host application?

  2. Or is it that service's config file only applies to IIS and managed hosts only?

  3. and finally is there a way to have the service's own configurations in the host application (not defining the endpoints and behaviors in the host application again) or the two mentioned configurations are completely different?

EDIT

my ultimate question is that how can I use the configurations defined in service's config file in the host application?(Without using host application's own config file or creating additional code to define new endpoints and behaviors )

1

There are 1 best solutions below

6
On

Now when I create a host application for my service, I figured out that I should add the service endpoints (and behaviors) for my service again in the code

This is incorrect. You don't have to define your service endpoint in code at all. That is what the config file is for.

Simply pass in the name of your service (as defined in your config file) as a type into your servicehost contructor:

var host = new ServiceHost(typeof(MyNamespace.MyService));

With the config defined as:

<system.serviceModel>
    <services>
      <service name="MyNamespace.MyService" behaviorConfiguration="MyServiceBehavior">
        <endpoint address="MyService"
                  binding="basicHttpBinding"
                  contract="MyNamespace.IMyService" />

        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8456/"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

WCF will work out that you want to use the config file to define the service to run.