Miguel A. Castro's Blog

# Monday, May 24, 2010

WCF 4.0 incorporates the features previously only obtainable via the REST Starter kit for .NET 3.5.  Among the features included is the ability to cache REST requests using a similar technique to caching ASP.NET pages.

The 4.0 version of the System.ServiceModel.Web assembly includes an attribute called AspNetCacheProfile which takes a string as an argument.  The attribute is to be used on a service contract operation as follows:

[OperationContract]
[WebGet(UriTemplate = "/{name}/getGreeting.xml",
        BodyStyle = WebMessageBodyStyle.Bare,
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml)]
[AspNetCacheProfile("CacheFor10Seconds")]
string GetGreetingXml(string name);

 

The string points to a cache profile to be defined in the config section as follows:


    
        
            
        
    

 

As you can see, this is pretty much identical to setting up cache profiles for ASPX pages.  The effect is pretty much what you would expect, the caching of a REST request.  This can be proven and tested by implementing the above contract as such:

public string GetGreetingXml(string name)
{
    return string.Format("Hello {0}.  The time is {1}", name, DateTime.Now.ToLongTimeString());
}

 

Now you can refresh the request and you’ll see the same time for 10 seconds before it changes; once again, the same effect as in caching an ASPX page.

So what did I learn the hard way you ask?  Well, common sense dictates that the request go through ASP.NET in order to use its caching capability.  This not only means that you have to host in IIS or WAS, but also that you need to turn on ASP.NET compatibility mode.  This shifts the processing of the REST request over to an HTTP handler that also sets an HTTP Context for you, something not normally done with conventional IIS hosting for REST services.  To turn this on, you must enable it in the section under the section using the aspNetCompatibilityEnabled attribute.  Not only that, but you have to “allow” it on the service using the AspNetCompatibilityRequirements attribute.

This part was not a surprise for me since I was already familiar with this technique and the added scope your service receives because it.  I’m referring to the entire HTTP Context including session, application, cache, etc.  Keep in mind that this is not for everything and should be used in very specific scenarios as it ties the service to the hosting style quite tightly.

What drove me crazy is something I’m surprised I did not notice in the past and that is the fact that your REST requests will not work while using the Visual Studio development server.  Meaning you need to create an application in IIS and run it that way.  No big deal but it drove me a little crazy since the meta data exposure worked fine, and so did the new HTML Help pages (another new feature in WCF 4.0 For REST).  But the second I tried to request one of my URLs, it bombed out.  Once I set up my application under IIS, everything was fine.

I have to give credit where it’s due and thank Jon Flanders.  I found the answer in his book Restful .NET from O’Reilly press.

Until next time…

Monday, May 24, 2010 4:50:14 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter
Dev Stuff | WCF
# Tuesday, May 18, 2010

WCFFirestarterNYC

Stop starting new projects with ASMX Web Services or .NET Remoting!  OK, now that we have your attention, let’s get serious.  These two

technologies are so 2002!  You need to start learning Windows Communication Foundation (WCF) if you haven't already.  This is the platform for connected applications on the Windows platform going forward, and you know what?  It's easier to use than you may think.

Even if you've been tinkering with writing basic services in WCF for a while but haven't dived into anything more advanced, you should come in for the NYC WCF Firestarter.

Join us for a full day of nothing but WCF sessions.  Ranging from an introductory so you can hit the ground running, and with best practices, continuing with topics involving the most important features of WCF, and ending with some cutting edge material on REST & Microsoft's new service bus and Azure technology in .NET 4.0.

We’ve got a great line up of speakers!  Presenting at the NYC WCF Firestarter will be Miguel Castro, Don Demsak, and myself (Peter Laudati).

Microsoft NYC Office - 1290 Ave of the Americas - 6th Floor
Microsoft NYC Office - 1290 Ave of the Americas - 6th Floor

Event Details

Event Date: Saturday June 19th, 2010

Event Location:

Microsoft NYC Offices
1290 Ave of the Americas 6th Floor
New York, NY 10104

REGISTER HERE!

Event Agenda

Doors open 8:30am. Sessions run from 9:00am – 5:00pm. Lunch will be served.

  • Session 1: Keynote – Intro to SOA & WCF
  • Session 2: Most Common WCF Usage Scenarios
  • LUNCH
  • Session 3: REST Programming with WCF
  • Session 4: WCF Made Easy – Data & RIA Services
  • Session 5: What’s New With WCF 4.0
Session Details

Intro to SOA & WCF

Jump right in to understanding what service orientation is and how WCF is the preferred technology for this architecture.  You'll see how services get written in WCF from scratch and using best practices from the beginning.

Most Common WCF Usage Configurations

WCF has many characteristics, each with several options.  There are however some optimal settings for the most common scenarios.  In this session you'll learn the whys and hows for configuring WCF services to work optimally in the areas of instantiation, concurrency, transactions, security, and bindings.

REST Programming With WCF

With .NET 3.5 came the addition of the System.Service.Web assembly and the addition of REST capabilities to WCF.  REST is an HTTP-based messaging protocol that is common in web applications today and even more common in non-.NET services that expose API (think Twitter & Google).  In this session, you'll learn how to expose your services using REST and also how to consume non-.NET REST APIs using WCF.

WCF Made Easy - Data & RIA Services

You have seen all the various ways you can leverage WCF, but what if all you want to do is expose your data via HTTP?  In that case, you can simplify your development, by using WCF Data Services or WCF RIA Services.  In this session we will learn how to easily expose your data using Entity Framework in a RESTful fashion using WCF Data Services, how to consume WCF Data Services, and then dive into WCF RIA Services.

What's New With WCF 4.0

No Day-Of-WCF would be complete without letting you know what's new with .NET 4.0 and Visual Studio 2010.  This session will give you a heads up on several new and powerful features introduced in WCF 4.0.  Among these are "Configuration-less Hosting", "Discovery", "Routing", and an intro to the new Azure Service Bus.

Swag & Prizes

We hope you come to attend the NYC WCF Firestarter for the unique content, providing a FREE opportunity to advance your skills by learning a new technology. However, like many events, we’ll have some fun stuff to giveaway at the end.  If you stick around, you will have the chance TO BE THE LUCKY WINNER OF A MICROSOFT ZUNE!  YOU MUST BE PRESENT TO WIN.

Tuesday, May 18, 2010 4:25:36 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter
Speaking Events | WCF
# Friday, April 30, 2010

This is certainly worth blogging about.  DevExpress, whom we all know are famous for their component suites and of course CodeRush, is working with Habitat for Humanity in New Orleans.  Rather than regurgitate their description on this posting, go here to read all about it.

Way to go guys !

Friday, April 30, 2010 12:55:31 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] - - Follow me on Twitter
Speaking Events
# Friday, April 16, 2010

The user group in New York City last night was great.  The turn out was around 85 people and the response, attentiveness, and questions were very good.  This talk is a hell of a memory dump since I cover so many things.

The name of the session was “Integrating Search: An Adventure in Dependency Injection” and I cover not only the APIs of the three major search engines, Bing, Google, and Yahoo (soon to use Bing), but also some provider design, dependency injection, and custom controls (both windows and web).

I also showed off my new FluentSearch DSL which I’ll have on this site very soon.  I talked about this session and the FluentSearch DSL in a previous posting regarding the Philly Code Camp, so you can go there to read more.

I want to cover an answer to one of the questions last night since I wasn’t able to answer it off the cuff and gave the attendee an honest, “I’m not sure”.  The question was regarding the WebGet attribute that I used to decorate the operations in my service contracts in order to consume the REST APIs of both Google and Bing.  The ResponseFormat argument of this attribute had the optional settings of either XML or JSON.  This determined the format for the data that the search engine API would return and instruct WCF how to decode it.  The exact question was regarding the RequestFormat argument of the attribute and what it would be used for in this scenario.  The answer is that it wouldn’t be used so its value wouldn’t matter.  The RequestFormat argument determines the format used when the operation is used to expose a service, not to consume it.  In all my usage for this talk, I was using this service contract for consumption of the external API so ResponseFormat is all that was important.  If I was actually writing a service to implement my service contract and would provide my own implementation of that operation, then the RequestFormat would determine what format of data I would expose through that operation.  But once again, I was only using my service contract to map to a REST URL that was exposed by another API so it was only in the context of a proxy-based consumption.

My sincere apologies for not having the answer to this question off the top of my head.  This talk covered so much that when the question was asked, I guess my head was elsewhere.  Thank you to whoever it was that asked it because it is definitely information that I should have covered in more detail.

As always, kudos to Andrew Brust for running a great group and inviting me to once again be a part of it.

You can get the session material from the Downloads section of this site.

Until next time…

Friday, April 16, 2010 6:14:34 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [1] - - Follow me on Twitter
Speaking Events
# Tuesday, April 13, 2010

The Philly Code Camp this last weekend was awesome as always.  The talk I did was “Integrating Search: Adventure In Dependency Injection”, and I was extremely pleased by the turnout.  I was a bit nervous about this one because I gave it in DevConnections last year and had a very small turnout.  Though the people that did show up loved it, I set out to make some modifications here and there and tried again in Philly.  I think there were over 75 people in the room and the response and feedback was awesome.

The material for the session can be found in the Downloads section of this site.

I’m looking forward to doing this talk again this Thursday in the NYC .NET User Group.  The New York group is run by Andrew Brust, Stephen Forte, and William Zack and has been around for a very long time.  It’s at this group where I first met my friend Carl Franklin (host of .NET Rocks!), and the group was also the second .NET user group in which I ever spoke.  Though I’ve remained an active attendee at this group, I’m looking forward to returning this Thursday as a speaker once again.

If you want to see a brief on the upcoming talk, see my posting prior to the Philly Code Camp.

Until next time…

Tuesday, April 13, 2010 12:25:30 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [1] - - Follow me on Twitter
Speaking Events
Search
Me & My Flair

Read all about me here.
Download my Resume here.

Check out where I am here.
 
Click on logos above for profiles.
Archive
<May 2010>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
Statistics
Total Posts: 40
This Year: 0
This Month: 0
This Week: 0
Comments: 93