Today lets create a client application for our simple Flight Service using C# code. Writing clients for ServiceStack services using C# is super easy! If you have worked with older web service related technologies, you may know by experience how difficult it is to create xml requests, serialise data, de-serialise data and etc. At the end of this example you’ll realise how easy it is writing C# client code to consume a service developed using ServiceStack.
First we need to add a consumer project into the solution. For this demonstration, I am going to use a Windows Console Application as my client and I name it “ServiceConsumer”.
Next, we should add a reference to ServiceStack.Client namespace within our client project. To get this done, right-click on the project and select Manage NuGet Packages… from the context menu.
Then search for ServiceStack.Client and click Install.
This will add the ServiceStack.Client and other supporting ServiceStack references into our client project.
Next, we should add the reference to our service project (see below).
Now, we are ready to go ahead and write our client code.
If you could remember, our FlightRequest class included two properties, Departure and Arrival. Basically, I am going to write a console application that creates an instance of this request class, populates its Departure and Arrival properties with values passed into the application and pass that request into the service. Finally, I will be writing some code to display the content of the response returned from the service. The following piece of code does it all…
The most important lines in the above code are line 16 and 17. In line 16, I create a new JSON client. Here, I create a new JsonServiceClient object by passing in the base URI of the service. The best way to get the base URI is to launch the service and copy the base address from the service URL (see below).
You may create other types of clients that could be used with request types such as XML, CSV, SOAP and etc. too. I will show how to create such clients later in this post.
In line 17, I create a new FlightRequest object, assign its two properties with values passed into the application and then pass it to the Post() method of the client. The other parameter that I pass into the Post() method is the desired route to use. If you remember, I had two routes defined for FlightRequest class so, I specify which route that I need to use. Also, note that I specify the response type in the method call in order to receive a strongly typed response back from the service.
Finally, I have written some code (line 22 to 31) to display the content of response I’ve got back. now, if you press F5 it will start debugging the service. To get the client application kicked off follow, right-click on the client project -> Debug -> Start new instance.
The following screenshot shows the output response for a sample run of the client application with two input parameters supplied.
In the above example, we used a JsonServiceClient class to initiate a JSON request to the service. As I mentioned before, there are other client classes available to us as well. You may replace line 16 with the following line of code to initiate an XML request. The client application should still function as before.
var client = new XmlServiceClient(“http://localhost:63808”);
The following code lines could be used to create JSV, SOAP 1.1 and SOAP 1.2 clients respectively.
var client = new JsvServiceClient(“http://localhost:63808”);
var client = new Soap11ServiceClient(“http://localhost:63808”);
var client = new Soap12ServiceClient(“http://localhost:63808”);
The client code that I’ve written worked fine but, we can further simplify this code to make it look more simple and sleek. We had to specify the response type in the Post() method but, with a small tweak in the FlightRequest class, we can get rid of this from our client code. All we have to do is to slightly change the FlightRequest class as shown below.
Here, we declare the expected response type related to our request type. Simply, we say that our FlightService returns an object of type FlightResponse when we post a FlightRequest object into the service.
We may even remove the route name parameter from the Post() method call as ServiceStack is intelligent enough to inspect the routes associate with the our request type and sort out the appropriate route to use! So, our simplified client code looks like the following.
And, our client program still runs with no troubles!











































