freddes.se

…because I'm a nerd

Archive for the ‘C#’ Category

WCF KnownType attribute example

with one comment

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 »

Written by Fredde

May 19th, 2010 at 11:37 pm

Posted in .NET,C#,WCF

Tagged with , , ,

Announcement and discovery in WCF 4

without comments

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 »

Written by Fredde

May 16th, 2010 at 3:30 pm

Sharing DataContracts between WCF Services

without comments

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.

Read the rest of this entry »

Written by Fredde

May 2nd, 2010 at 5:21 pm

Posted in .NET,C#,WCF

Tagged with , , , ,

The importance of the order of routes in ASP.NET MVC

with 3 comments

mvc-logo-landing-pageLast 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.

Read the rest of this entry »

Written by Fredde

December 9th, 2009 at 1:49 pm

Update Views through events between their ViewModels

with 2 comments

eventLately 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.

Read the rest of this entry »

Written by Fredde

December 4th, 2009 at 9:11 am

Posted in .NET,C#,MVVM,Patterns,WPF

Tagged with , , , ,

Using interfaces when implementing web services

with one comment

interfaceThe 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.

Read the rest of this entry »

Written by Fredde

February 24th, 2009 at 11:46 pm

Multiple web service references sharing types

with one comment

play_btnIn 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.

Read the rest of this entry »

Written by Fredde

February 16th, 2009 at 11:14 pm

Using the Enumerable Class methods and lambda expressions

without comments

lambdathumbnailThis 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.

Read the rest of this entry »

Written by Fredde

January 28th, 2009 at 12:17 am

Posted in .NET,C#,Tips & tricks

Tagged with , ,

Nice coding standard documents for C# (and VB.NET)

with 2 comments

coding_stdComing 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.

Read the rest of this entry »

Written by Fredde

December 30th, 2008 at 7:38 pm

Posted in .NET,C#,Visual Studio

Service interfaces using XSD (cont’d MVC Web Service part 1)

with one comment

visualstudiologoThe 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.

Read the rest of this entry »

Written by Fredde

December 29th, 2008 at 11:09 pm