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 3 ■ EXPLORING THE WCF PROGRAMMING MODEL 71ramrameshreddy.blog.com}}}}Quote quote = tickers[ticker] as Quote;if (quote == null){// Quote doesn't exist.throw new Exception(string.Format("No quotes found for ticker '{0}'",ticker));}return quote;public void PublishQuote(Quote quote){lock (tickers){Quote storedQuote = tickers[quote.Ticker] as Quote;if (storedQuote == null){tickers.Add(quote.Ticker, quote);}else{tickers[quote.Ticker] = quote;}}}As you can see, the implementation of the service is simple. It has one member variable(of type Hashtable) that is responsible for keeping the internal state of the service with theprovided quotes in memory. In a more realistic scenario, this would of course be kept in somepermanent state system (a back end based on a database). To be able to call into the servicemultiple times, you have to make sure the behavior of the service is a Singleton. Therefore,the ServiceBehavior attribute is applied with the InstanceContextMode property set toInstanceContextMode.Single. You will learn more about behaviors in the “Applying Behaviors”section. The ReturnUnknownExceptionsAsFaults property is able to track back the exceptionthat can occur when a quote is requested for an unknown ticker to propagate to the client. Forobvious reasons, by default WCF doesn’t map .NET exceptions across the wire in SOAP faults.In the “Introducing the Programming Approach” section, you learned the approaches youcan take in programming WCF; you also learned that as soon as ServiceHost is there, anyattributes and configuration have already been applied. In other words, Listing 3-8 shows thesame thing in terms of the actual result, but the one with the configuration is much moremaintainable.ramrameshreddyramrameshreddy

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

Saved successfully!

Ooh no, something went wrong!