WCF

A Very Cool WCF Tool: WCFTestClient.exe

This an oldie, but a goodie. Not many people use this tool, much less know about it or where it exists.

By default when you install VS2008 (I’m not sure if its part of 2005 or not) you have a tool at your disposal in the Visual Studio install path:

My Path: c:\Program Files\Microsoft Visual Studio 9.0\Common7\ide\

In this path, you’ll find a tool called “WcfTestClient.exe”

It is located here: c:\Program Files\Microsoft Visual Studio 9.0\Common7\ide\WcfTestClient.exe

Fire this bad boy up and here’s the screen you get:

image

You can add a service endpoint by right clicking on “My Service Projects” and clicking Add Service

image 

Then paste in your service location (I’m using an open one I found on xmethods.net that generates prime numbers):

image

Click Ok, and the service contract, config, etc will be generated for you and you’ll have something that looks like this:

image

Double click on “GetPrimeNumbers” and you’ll see the following:

image

Open the body of the request (as it looks like this service uses message contracts) and where it says “(null)” select “GetPrimeNumbersRequestBody”

image

Then expand the body and the request what is the highest number the service should search for prime numbers (in english – give me all the prime numbers up to 50):

image

Click “Invoke”, wait a few seconds and your response will come back.

image

 

All Done!

 

You have now tested a WCF Service without creating an application, generating a proxy, running the app, etc. Super easy!

 

Other Uses

You could use this to test your own in house services or even to debug a service running on your machine during development (if you wanted to test full end to end). Simply paste in the address of your local service (http://localhost:someport/myservice.svc) and then start testing. This is a HUGE time saver to see if your service works or not. Otherwise you’d have to generate a client, perform a service reference or generate a service proxy, etc.

New Dime Cast: Migrating from .asmx web services to WCF web services

Woo hoo! I created another Dime Cast and its up and running today!

Learn how to migrate from your old asmx web services to new fancy WCF web services.

Check it out here

dynomite 

 

Intro to WCF Screen Cast Now Available

I have a new Screen cast available on  DimeCasts.net. It is entitled “Introduction to WCF: Creating your first Service“.

In this episode we will take a look at how to create your first WCF service. We will first walk you though the ABCs of what a WCF service is, we will then show you step by step how you can create a service.

Click here to watch.

How To: REST Services in WCF 3.5 Part 2 – The POST

This post is part of a series.

 

Implementing a POST HTTP Verb Call

In this post we’re going to cover what it takes to implement a PUT into our REST Service that we defined in Part 1 of this series.

First of all, what is the POST HTTP verb for? To understand this lets take a look at how a HTTP Get call is formed from an HTTP Header perspective. A GET request fetches data from a web server based solely on a URL value and a set of HTTP headers.

HTTP GET Header Example:

GET /index.html?userid=joe&password=guessme HTTP/1.1
Host: www.example.org
User-Agent: Mozilla/4.0

In this example the Get requests from a host (www.example.org) with the Mozilla/4.0 type browser and is asking for the index.html page with the query string values userid and password (and their corresponding values).

Now for the HTTP POST… POST request sends additional data to the web server, specified after the URL, the headers, and a blank line to indicate the end of the headers. An example:

POST /login.aspx HTTP/1.1
Host: www.example.org
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userid=joe&password=guessme

 

At a very simple level – A POST is normally done from a web form. When we fill out a form and send the data the form attribute “method” is set to “post”.

If you want the super formal explanation of POST check out the POST definition on the W3C site.

 

The implementation in WCF

In the previous example we used a simple Gas Price Service. This next service is something a little different. Lets assume that we have a comedy web site that allows users to get “insults” and “save insults”. We are going to use this example, and its called the “Insult Service”. Its a very simple REST service that insults you or your co-workers or anyone else for that matter. Kind of fun to mess around with. This version of the insult service has the following methods:

  • GET: Insult someone (an exact copy of the previous part 1, just in a different context)
  • POST: Add a new insult

To receive an insult we ask the insult service to insult someone via the GET (again, this is the same as the previous example).You would call the service like this: http://localhost:7000/insult/{personsNameToInsult}  .

NOTE: The insult service grabs a random insult from a list of insults and returns it to the user.

Here it is in action: Replace {personsNameToInsult} with “Donn” (minus the quotes)

image 

Here is the GET in code from the WCF Service Contract:

 

[OperationContract]
[WebGet(RequestFormat = WebMessageFormat.Xml,
	ResponseFormat = WebMessageFormat.Xml,
	BodyStyle = WebMessageBodyStyle.Bare,
	UriTemplate = "/insult/{personToInsult}")]
Insult GetInsult(string personToInsult);

 

Now, to implement the POST we need a way to tell the server that we want to POST data to a service. Here’s how we do it in WCF:

 

[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Xml,
	ResponseFormat = WebMessageFormat.Xml,
	BodyStyle = WebMessageBodyStyle.Bare,
	UriTemplate = "/insult/Add/{insultName}/{insultText}",
	Method = "POST")]
void AddInsult(string insultName, string insultText);

In this service contract we are telling WCF that the URITemplate is going to be as such:

/insult/Add/{insultName}/{insultText}

Where {insultName} and {insultText} map to the string values in the “AddInsult” method. To add an insult we’d have a POST request that looks like this:

http://localhost:7000/insult/Add/TestInsult/This+is+a+test+insult

However, if we type this into the Browser window we will get the following:

image

This is because the web browser by default performs a GET operation on the URI that is posted into the address bar.

So how do we POST? We can either build a web form to do it for us, or we can use a web debugging tool.

 

Adding a New Insult: The POST

Since we do not have a web front end for this service we need to construct a HTTP POST manually. We can do this with Fiddler. Fiddler is a Web Debugging tool that allows you to inspect HTTP Traffic and construct requests. We’re going to use it for constructing our POST call. (You could also create a simple web form to do this as well).

Constructing the POST:

  1. Install Fiddler
  2. Open Fiddler
  3. Open the request builder
  4. Select Post
  5. Build the request
  6. Submit the request

3. Open the request builder

image

 

4. Select POST

image

 

5. Build The Request

 image

Like this:

image

 

6. Submit the request

image

At this point our service has been called and the values have been written to the data repository (db or file, etc).

Now lets see if the insult shows up in our service. Lets hit refresh on our GET page a few times to see if it shows up. Which it should.

image

 

Conclusion

As you can see, its fairly simple to implement a POST action to add new values to a WCF REST service. This has been done utilizing the WebInvokeAttribute to handle our POST action.

Additional Reading:

 

Download Code

Part2.zip (~11kb)

SVCUtil & VS2008

After some research into a Service Reference problem I was having with Silverlight I came to find out (but not officially) that VS2008 DOES NOT use SvcUtil under the hood to get its service reference. It uses its own internal implementation of a proxy generation tool. I’ll have to confirm this conclusion with some authority/book/etc, but if this is the case, I made a statement in Dime Cast 67 (WCF Rest Part 1) that informed viewers that VS2008 uses SVCUtil under the hood.

I’ll see what I can find out and post it back here when I confirm my thoughts.