Basic WCF App


I like to provide very simple examples ("as simple as possible but no simpler" to quote someone who should know) to illustrate a feature, so my apologies to those who prefer something more complex or interesting. It is best practice to use svcutil for creating client proxies, but I have coded by hand for brevity and clarity.

It is important to stress that the code presented here was written and tested using a beta 2 build of WinFx. Unfortunately, features such as browser integration are not available before the February CTP (Community Technical Preview), so I have used an early version of that (the latest that was available to me at the time of writing). The upside for you is that this sample codeor something very similarshould work with all builds from the February CTP onward. The downside is that there is a chance that by some quirk of fate my code will not work on the final beta 2 build. If this is the case, please accept my abject apologies. I hope the difference is not so great as to impair your enjoyment of getting the code to work and seeing InfoCard in action.

So let's take a very basic WCF app and add InfoCard support. We're not going to use an STSin either this example or the browser example that followsit's just about showing how InfoCard works at a basic level.

The application consists of a servicewhich will be our relying partyand a client app that communicates with the service via WCF. Both are very simple console apps. You'll need to create a solution (InfocardHelloWorld, say) and two console application projects (InfocardHelloClient and InfocardHelloService, say).

Here are the code listings of the applications we're going to add InfoCard to. For each project you will need to add a reference to System.ServiceModel.

First the contract, contract.cs, a copy of which is in both the client and the service projects:

using System.ServiceModel; namespace HelloService {     [ServiceContract]     interface IHello     {         [OperationContract]         string Say();     } }


Now the service code, program.cs:

using System; using System.ServiceModel; namespace HelloService {     class Hello : IHello     {         public string Say()         {             return "Hello World";         }     }     class Program     {         static void Main(string[] args)         {             ServiceHost sh = new ServiceHost(typeof(Hello),               new Uri("http://localhost:4123/helloService"));             sh.Open();             Console.WriteLine("Listening....");             Console.ReadKey();             sh.Close();        // close the service         }     } }


And the service configuration file, app.config:

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <system.serviceModel>     <services>       <service name="HelloService.Hello">         <endpoint address="helloEndpoint"           contract="HelloService.IHello"           binding="wsHttpBinding"           bindingConfiguration="helloBinding">         </endpoint>       </service>     </services>     <bindings>       <wsHttpBinding>         <binding name="helloBinding">         </binding>       </wsHttpBinding>     </bindings>   </system.serviceModel> </configuration>


Now the client pieces. First, the client code, program.cs (note that this is the same filename as the server-side codeas with contract.cs and app.configbut it resides in a different project and directory):

using System; using System.ServiceModel; namespace HelloClient {     class Program     {         static void Main(string[] args)         {             ChannelFactory<HelloService.IHello> cnFactory =               new ChannelFactory<HelloService.IHello>("helloClient");             HelloService.IHello chn = cnFactory.CreateChannel();             Console.WriteLine(chn.Say());             // Clean up             cnFactory.Close(); // close the client's channel             Console.ReadKey();         }     } }


And finally the client configuration file, app.config:

<?xml version="1.0" encoding="utf-8" ?> <configuration>   <system.serviceModel>     <client>       <endpoint          name="helloClient"          address="http://localhost:4123/helloService/helloEndpoint"          contract="HelloService.IHello"          binding="wsHttpBinding"          bindingConfiguration="helloBinding">       </endpoint>     </client>     <bindings>       <wsHttpBinding>         <binding name="helloBinding">         </binding>       </wsHttpBinding>     </bindings>   </system.serviceModel> </configuration>


There, we're done. This should compile and successfully run a basic WCF "Hello World" application.




Presenting Microsoft Communication Foundation. Hands-on.
Microsoft Windows Communication Foundation: Hands-on
ISBN: 0672328771
EAN: 2147483647
Year: 2006
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net