Archive for the ‘C#’ Category
WCF KnownType attribute example
In this article I will present an easy to understand example when to use Data Contract Known Types, e.g. the KnownTypeAttribute class, to make a WCF Service serialize and a service consumer deserialize types when using inheritance. The MSDN has the complete documentation on this, take a look here: KnownTypeAttribute Class.
In this example I will use a hierarchy of different types of vehicles, e.g. a base vehicle type, a car type, a bike type and a kid’s bike type. I will use WCF 4 (.NET 4), Visual Studio 2010 and the WcfTestClient to test the service.
The great thing with KnownTypeAttribute is that WCF can expose one method supporting all types in a class in a hierarchy.
Read the rest of this entry »
Announcement and discovery in WCF 4
WCF 4 comes with a bunch of new features where I find the service announcement and service discovery really cool. In this article I will implement a couple of services using these new features.
A couple of key words and classes for the scenario are SeviceDiscoveryBehavior, UdpAnnouncemenEndpoint, AnnouncementService. I will describe those classes along the way, but on MSDN you can find more information here: WCF Discovery Overview.
In my previous article I linked to a great article written by Aaron Skonnard, don’t miss it: A Developer’s Introduction to Windows Communication Foundation 4. It covers most of the new features in WCF 4, the simplified configuration, Workflow Services and a bunch of other stuff.
I will describe the scenario below along with a couple of code snippets.
Read the rest of this entry »
Sharing DataContracts between WCF Services
In a previous article, Multiple web service references sharing types, I wrote about sharing types between referenced web services. That article used the old ASP.NET Web Service technique, but nowadays WCF is the technology to use. In this article I will use much the same scenario as in the previous article, Visual Studio 2010 and of course WCF (.NET 4).
I will describe the scenario below, describe what may be an upcoming problem and what can be done to avoid it. Please read the previous article as well, it is much the same solution that is to be presented here.
If you are not familiar with WCF and DataContracts you can read David Chappell’s article here: Introducing Windows Communication Foundation in .NET Framework 4. Aaron Skonnard has written a great article about WCF 4, read it here: A Developer’s Introduction to Windows Communication Foundation 4.
The importance of the order of routes in ASP.NET MVC
Last week I had the pleasure of listening to a presentation from Scott Guthrie here in Stockholm, http://weblogs.asp.net/scottgu/archive/2009/12/06/my-presentations-in-europe-december-2009.aspx. One of the most interesting parts of the all day event was his talk about ASP.NET MVC 2. Yesterday I had some time off and started to try things out using Visual Studio 2010 Beta 2 and the ASP.NET MVC 2 template. It was running smoothly for a while until I bumped into a problem I recognized from earlier. I didn’t remember the solution right away, but soon I figured it out what was the key point in making the routes, registered in Global.asax, work properly.
The order when adding the routes using the MapRoute method on a RouteCollection object is important to avoid getting a 404 page when browsing to pages that are supposed to work. I will present the problem and the solution for it below.
Update Views through events between their ViewModels
Lately I’ve been developing standalone WPF applications using Visual Basic. To make a WPF application somewhat easy to maintain and extend it is extremely important to use some design pattern. The most popular design pattern around when developing WPF applications is the MVVM pattern (Model-View-ViewModel).
Developing a single view application is really really straight forward using the MVVM pattern, but when it comes to multiple views application there is no simple solution to update one or more views from another view and still make the Views and ViewModels behind them loosely coupled. When I say update I mean making some property change in the ViewModels behind the Views to be affected by the change of something in another View or ViewModel.
In this post I will show how I’ve solved the problem using events and delegates. Before I describe my solution, please read about the basics in the MVVM design pattern. There are a lot of good publications around, e.g. THE MODEL-VIEW-VIEWMODEL (MVVM) DESIGN PATTERN FOR WPF and Model View ViewModel (Wikipedia).
To make it easier to understand this post and to make it easier for me to write it, please read the following article if you’re not familiar with WPF data binding and the usage of INotifyPropertyChanged: http://coredotnet.blogspot.com/2006/05/wpf-data-binding-tutorial.html.
Using interfaces when implementing web services
The other day I wrote about sharing types between web services to simplify implementation of consumers of these services. You can read about it here. I mentioned a nicer way to accomplish almost the same thing and I will present it here, by giving an example.
The biggest difference is that you have to define an interface, let all web service producers implement this interface and you as a consumer will be left with a minimal amount of work to consume all these web services as long as their end points are well known.
Multiple web service references sharing types
In this article I would like to point out a useful way to accomplish something that would be really easy, but still have been annoying me for the last couple of months. Imagine that you would like to get some data of some type from one web service and pass it on to another. The data types on both server sides are the same, but the compiler won’t let you pass the type from the first service on to the next one. Casting won’t work either, not without implementing some kind of home made casting method.
This is really really annoying, since the scenario stated above is really common in the web service world. I will present a solution to this, using the wsdl command line tool when adding web references instead of the built-in tool in Visual Studio.
Using the Enumerable Class methods and lambda expressions
This article presents nothing new at all, but sometimes it is nice to freshen your memory when it comes to smooth stuff available in the .NET Framework. I will give a couple of examples to select, count and look up elements in a collection of objects, both using regular functions and by using lambda expressions. The methods I will use are available when using .NET Framework 3.5.
The complete documentation is available on MSDN, in System.Collection.Generic Namespace documentation.
Nice coding standard documents for C# (and VB.NET)
Coming back from a little break during Christmas I looked around on the ASP.NET web site (http://www.asp.net) and found a couple of interesting newly added things. Amongst stuff I took a look at the coding standards added by Clint Edmonson (http://www.notsotrivial.net) a couple of days before Christmas. They look really nice and may be a good start when implementing some kind of standards in an organization.
Service interfaces using XSD (cont’d MVC Web Service part 1)
The other day I got a question from a colleague of mine, regarding the XML schema usage when defining a web service interface, like the one defined in series. The question was something like this: “How will the consumer of the web service react if the, from the XSD file, auto-generated class used as in and return parameters is updated?” I couldn’t give a straight answer, other than that I assumed it would be transparent to the consumer as long as no nodes were changed or removed and only new nodes were added to the schema. I decided to give it a try, using a really simple XSD and a couple of really simple web methods. I will give a schematic picture of the scenario that I will test out later on.