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.

CHAPTER 4 ■ INSTALLING AND CREATING WCF SERVICES 121ramrameshreddy.blog.comIn addition to a sample implementation, the project references have been updated tomake it easier to work with WCF applications.Now in Solution Explorer, delete the generated Class1.cs file. Then right-click the project,and add a new item. Locate the .NET 3.0/WCF service item. Enter TradeService in the Namefield, and click Add.In the generated TradeService.cs file, replace the contents with Listing 4-8.Listing 4-8. TradeService.cs Implementationusing System;using System.ServiceModel;namespace ExchangeService{[ServiceContract(Namespace="http://PracticalWcf/Exchange/TradeService",Name="TradeService")]public interface ITradeService{[OperationContract]decimal TradeSecurity( string ticker, int quantity );}public class TradeService : ITradeService{const decimal IBM_Price = 80.50m;const decimal MSFT_Price = 30.25m;public decimal TradeSecurity( string ticker, int quantity ){if( quantity < 1 )throw new ArgumentException("Invalid quantity", "quantity" );switch( ticker.ToLower() ){case "ibm":return quantity * IBM_Price;case "msft":return quantity * MSFT_Price;default:throw new ArgumentException("SK security - only MSFT & IBM", "ticker" );}}}}ramrameshreddyramrameshreddyNotice that the top of the file contains a reference to the System.ServiceModel namespace.This namespace contains the necessary types that provide attribute support for the contractdeclaration.

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

Saved successfully!

Ooh no, something went wrong!