WCF Part 3 : Hosting the service

Finally .NET Framework 3.0 RTM’d, so I don’t have to run in my VMWare anymore. A good time to see if everything works and create the host for the service contract we’ve created last time. We’ll do this using a C# console application, as those just rock in simplicity.

We need a ServiceHost object to host our service. It implements IDisposable, so we’ll use the using statement to create one.

using (ServiceHost host = new ServiceHost(xxx))
{
}

You’ll hopefully immediately notice the three red x characters. That’s where we have to insert a parameter, our servicetype. For us this is our Hello contract. We also need to open the host we created, before it’s actually brought alive by WCF.

Type type = typeof(Hello);
 
using (ServiceHost host = new ServiceHost(type))
{
  host.Open();
 
  Console.WriteLine("The service is available. Press any key to continue...");
  Console.ReadKey();
 
  host.Close();
}

In the above final sample I first declare a type variable, assign our Hello service contract type to it and pass it into the constructor of our ServiceHost instance. I then open the service, show this in the console window and ask the user to press a key to close the service. Else the service would close immediately.

WCF however needs some information from configuration. It can all be done through code, but of course we like our app.config much better. We need to specify our WCF ABC. We’ve already created our contract, but we have to let WCF know what contract belongs to what binding on which address. First, compile your application. Then, in your project you can add the app.config, right-click it and select ‘Edit WCF Configuration…’ and you’ll see the Service Configuration Editor. Select ‘Create a New Service…” and you’ll be presented with a nice wizard.

The first step we need to tell it what service we’ll want to use, where the implementation of the service is. Select to browse and lookup your console application it’s executable and select it. You’ll see your Hello class, which you’ll have to select. The next page of the wizard will now just know that IHello is the contract we’ve used. Now we’ll have to select the first part of our binding, the protocol we’ll start using. Leave it at default. In the next step we’ll also choose the default setting. The difference between these two can roughly be described as standard ASP.NET ASMX webservices, where the second means WSE 3.0 extension.

Last thing of the WCF ABC is to select the address. You might want to choose http://localhost:8080/HelloService/. We’ll then get an overview of our selections and you can press the finish button. Be sure to check out what the wizard has changed inside the Service Configuration Editor. You’ll only have to research the “Services” folder as we did not yet change the others. After saving and looking at our app.config, we should find the following.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
    <system.serviceModel>
        <services>
            <service name="Classa.Wcf.Samples.Hello">
                <endpoint
                  address="http://localhost:8080/HelloService/"
                  binding="basicHttpBinding"                  
                  contract="Classa.Wcf.Samples.IHello"
                  bindingConfiguration="" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

Notice that we have one service. In the above configuration you can see my namespace Classa.Wcf.Samples. which I added extra. In the attachment I’ve included you can see where exactly this comes from. Also the bindingConfiguration attribute isn’t really necessary right now. What is important that we see our WCF ABC return again. Address, binding and contract!

You can run this and your service will run and be live, but noone can tell how to communicate with it. For this, we need metadata! But that’s for the next post.

[Go to the WCF series article index]

You may also like...

6 Responses

  1. Jakob Bjerre Jensen says:

    In the example you reference the class ‘Hello’ twice. Both in code (Type type = typeof(Hello)) and in the app.config ().

    I was wondering, if that is necessary? Why does it have to be in the code, if it is in the config file and vice versa?

    And another issue: What is the behaviour if you change it to some other contract implementation in the app.config file?

    Best regards,
    Jakob Bjerre Jensen

  2. Dennis van der Stelt says:

    Hi Jakob,

    The typeof(Hello) is because you have to tell the servicehost that you’re hosting a service and that this class holds your implementation. The ServiceHost also needs some configurtion though. You _can_ do this all in code, but config is preferred, because you can change it later. It can find the config for that implementation by going to name=”Classa.Wcf.Samples.Hello”.

    It needs to be the fully qualified name (Classa.Wcf.Samples.Hello) in the config. It also needs the fully qualified name in the code (the typeof(Hello)) but it can infer the fully qualified name itself.

    So it needs to be in both so the ServiceHost can find the corresponding configuration. You can actually start multiple ServiceHost objects all with their own service implementation and all with their own configuration.

    If you change the contract in the configuration, it will fail to start the service.

    Don’t hesitate to ask more questions!
    And take a look for the ServiceHost without configuration in my post “WCF simple example”: http://bloggingabout.net/blogs/dennis/archive/2007/04/20/wcf-simple-example.aspx

    Regards,
    Dennis

  3. Javier Rodríguez J says:

    Hi, normally in the step “in your project you can add the app.config, right-click it and select ‘Edit WCF Configuration…'” in some visual studio installations are mising the “Edit WCF Configuration..” so to activate this option go to the Visual Studio Menu->tools->WCF Service Configuration… open this option and closed, and there is the option active in the app.config file! i hope help us!

  4. Kelsri says:

    I am pretty new to WCF. How did you access Hello class in Host class?
    Type type = typeof(Hello);

    I guess you have added the reference to Hello class within Host. Right?

  5. Dennis van der Stelt says:

    @Kelsri: If the Hello class is not in the same assembly (in Visual Studio an assembly will always be a project, on your file system it’s most likely a .dll or a .exe) you would surely add a reference to the assembly and a using statement to the namespace at the top of the class which initializes the ServiceHost.

    But perhaps my other article makes everything much clearer:
    http://bloggingabout.net/blogs/dennis/archive/2010/06/16/wcf-simple-example-in-visual-studio-2010.aspx

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.