11.07.2015 Views

y - Net Developer

y - Net Developer

y - Net Developer

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

amrameshreddy.blog.com76 CHAPTER 3 ■ EXPLORING THE WCF PROGRAMMING MODELramrameshreddy.blog.com}}}host.Open();Console.WriteLine("Service started: Press Return to exit");Console.ReadLine();public class CustomServiceHost : ServiceHost{public CustomServiceHost(Type serviceType, params Uri[] baseAddresses): base(serviceType, baseAddresses){}}protected override void OnInitialize(){BasicHttpBinding binding = new BasicHttpBinding();AddServiceEndpoint(typeof(ITradeService), binding, "Exchange");}Although this scenario is not really something you would do in a real application becauseyou are hard-coding the configuration again, you can imagine the benefits of this scenario.An example is setting up the description of your service based on a configuration stored in adatabase.Channel FactoryJust like ServiceHost, you instantiate ChannelFactory based on a specific service. There’s a difference,though. The client knows only about the exposed contract of the service, not about itsimplementation. Therefore, in this case, the generic that is passed to ChannelFactory is the interfaceof the contract. In Listing 3-11, we have written a client that instantiates a ChannelFactory toopen a channel to the service defined in the previous section about ServiceHost. Listing 3-12shows the associated configuration files for use on the client side. To handle the third tenet(share the schema and not the class), it is best if you define the contract of the service separatelyand not create a separate assembly that you use on both the client side and the service side. Thisway, the service side can evolve without impacting the client side. Of course, the code uses theconfiguration best practice instead of the imperative code.ramrameshreddyramrameshreddyListing 3-11. The Client Code Using ChannelFactoryusing System;using System.ServiceModel;using System.ServiceModel.Channels;using System.Runtime.Serialization;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!