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.
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.
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.
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.