Paging with ASP.NET Web API OData

A common requirement for web applications is to expose partial results from a larger set of data. Maybe you want your users to be able to browse through a collection of movies that they can stream. But you don’t want to display all of the thousands of movie titles you have available, just the first…


Writing tests for an ASP.NET Web API service

It’s important to test any service you write to make sure that it’s behaving the way you expect it to. In this blog post, I’ll go through the main ways of testing a Web API service, exploring the benefits and drawbacks of each option so that you can test your service effectively. Web API testing…


Building Real-time Web Apps with ASP.NET WebAPI and WebSockets

WebSocket is a recent technology that provides two-way communication over a TCP connection. This allows us to create real-time web apps where servers can push data to clients. In this blog post, I’ll demonstrate how this can be done by building a simple chat app using ASP.NET WebAPI and ASP.NET’s new support for WebSockets in…


Error Handling in ASP.NET WebAPI

WebAPI is a brand new framework that makes it easy to build HTTP services. As such, it provides several features that make it easy to send back useful and informative error messages in a variety of cases. In this post, I’ll go over some of these capabilities. Note: Not all of the features I mention…


Writing a Lightweight Web Service using WebApi Building Blocks

Don’t you wish you could write a web service this easily:   LiteWebServer server = new LiteWebServer(“http://localhost”); server.Get(“/Hello”, (r) => new HttpResponseMessage() { Content = new StringContent(“Hello World!”) }); server.Post(“/Echo”, (r) => new HttpResponseMessage() { Content = new StringContent(r.Content.ReadAsStringAsync().Result) }); server.Open();   Well, with last week’s release of the Web API beta bits, now you can. Web API is a brand new framework that makes it easy to build HTTP services. You can learn more about Web API here:….


Using the [ContractNamespace] attribute to customize the namespace for POCO types

In .NET 3.5 SP1, DataContractSerializer was enhanced to support POCO types that weren’t marked with any serialization attributes at all. You can find my previous post about serializing POCO types here. But in some cases, you may want to customize the XML namespace of your POCO types without necessarily having to fall back to the…


Loading WCF Client Configuration from Different Files with ConfigurationChannelFactory

One pain point with configuration that WCF developers have run into in previous versions of the framework is the limitation that WCF clients can only use the hosting executable’s configuration file. This can be problematic for clients that need to call multiple services because the configuration for all of these services would have to be…


Turning XML into XmlSerializer CLR Types with xsd.exe

Here’s a common dilemma .NET developers encounter when working with XML: I have some XML that I want to represent in a strongly typed way, but I don’t have any XSDs to represent that XML. How do I turn that XML into CLR types that I can serialize?   Of course, I could go about…


Using XML Schema Import and Export for XmlSerializer

In a previous post, I outlined how you could import and export the XML schema for a type that you’re serializing with DataContractSerializer. Here’s how to do the same thing if you’re serializing objects with XmlSerializer:   static void RoundTripXmlMetadata(Type type) {     XmlSchemas schemas = new XmlSchemas();     XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);  …


How to get minOccurs = 0 in the schema for XmlSerializer struct members

If you try exporting the following type into an XML schema:   public class Employee {     public DateTime BirthDate { get; set; } }   And you happen to be using XmlSerializer, you may be surprised to find that the schema gets exposed like this:   <xs:schema elementFormDefault=”qualified” xmlns:xs=””>   <xs:element name=”Employee” nillable=”true” type=”Employee”…