0

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 )

roostaamir
  • 1,928
  • 5
  • 24
  • 51

1 Answers1

1

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.

tom redfern
  • 30,562
  • 14
  • 91
  • 126
  • that is true and as I said in my question, we can define the configurations in the HOST APPlLCATION's config file. how can I use the service's configurations defined in SERVICE"s config file? – roostaamir Sep 04 '15 at 15:34
  • @user2078785 - The host application config IS THE SAME THING AS the service config. You say you're new to WCF - are you also new to .Net? In .Net you have a single config file per process. – tom redfern Sep 04 '15 at 16:08
  • when I create a service library project, the project has a app.config file.What is the use of this config file when I am going to always use the host application's app.config file? – roostaamir Sep 04 '15 at 16:43
  • @user2078785 - It has no use - when your host application runs it will only find it's local config file. But, you can copy the relevant bits out of the WCF project config into your host application configuration file. In your case those relevant bits are everything inside the `` – tom redfern Sep 04 '15 at 18:50
  • so how the default hosting application can use this config file(the one that is defined in the wcf service library application)?(sorry that I ask so many questions) – roostaamir Sep 05 '15 at 06:55
  • It can't use that config file. It can only use it's own one. So you need to copy the configuration from one file and paste it into the other. – tom redfern Sep 05 '15 at 13:14