First time here? You are looking at the most recent posts. You may also want to check out older archives or the tag cloud. Please leave a comment, ask a question and consider subscribing to the latest posts via RSS. Thank you for visiting! (hide this)

C#

Using web api client in synchronous methods and some good async/await resources

Lately I’ve been trying to include a call to a REST service using the WebAPI client to a library already used from inside both an ASP.NET page and a WCF service. The problem with mixing sync and async Of course I started with an async/await code: HttpResponseMessage response = await client.PostAsJsonAsync(GetUpdateUri(), payload); Unfortunately both consumers were synchronous, so I had to wait for the async to complete before going on with the execution: so I did something that proved to be wrong (the following methods calls the line...

Why you should never use a boolean field (use an Enum instead)

Over the last few months I worked, on my spare time, on a new web development project: the site for the Web.NET Conference. It was a refreshing experience, going back working on custom development on ASP.NET MVC 4 and all the latest bits of technology. That gave me quite a few ideas for posts, so over the next weeks I'm going to blog about some of the bits and pieces of code that I think are worth sharing with the community, like ActionResults, HtmlHelpers together with my first real-coding experiences with RavenDb. As first post I’m not focusing...

The evolution of asynchronous controllers in ASP.NET MVC

Asynchronous operations in ASP.NET MVC have always been left a bit behind. They appeared in ASP.NET MVC 2, remained untouched in v3, but now in MVC 4 (especially  in combination with C# 5 and async/await) they reached the same easiness of use of the standard synchronous controller. Now (or better, in a few months with the release of ASP.NET MVC 4, .NET 4.5 and C# 5) you can write public async Task<ViewResult> Stuff() { return View(await DoStuff("Some stuff")); } In this post I’m going to show how the code...

How to convert dates from Google Reader and Facebook API with Json.NET

Lately I’m working together with Daniela on a news feed reader for WP7 and one of its features is Google Reader synchronization. (It is called FeedTso, and if you are interested in knowing when it will be released I recommend you go and subscribe to the newsletter on the FeedTso site.) I’ll talk about the Google Reader API in a future post (I’ll opensource the API when FeedTso is on the MarketPlace) but for the moment I want to discuss how I deserialize the JSON Date that I receive from the Google Reader API (and from what I’ve been...

C# vs Ruby

This is not another humor post, but just a comparison between the searches on the terms C# and Ruby using Google Trends. Apart from the number of searches on the term C# which are double the ones on the term Ruby, it’s funny to notice the trend in the various days of the weeks: Ruby stays constant throughout all the week, while C# drops to half the volume during weekends. There might be a few explanations for this: Ruby is used by people that don’t have a personal life and spend...

Data Structure and Algorithms eBook

After dark years, seems like good things are coming out from the Italian .NET community lately: the latest one is the free eBook on Data Structures and Algorithms wrote by Granville Barnett and the Italian UGIdotNET member, Luca Del Tongo. I remember studying such things at the university, and the last time I helped a friend with his studies the books were still the same, and still based on "old" languages like C. Finally now a book covers the same important topics but with samples written in a modern object oriented language. All the algorithms of the book are available in form...

String to Enum

This might seems a pretty trivial problem, but every time I've to get an Enum given its name I forgot how to do it, and I've to look for it on Google. So, is there a better way to remember something you always forget than writing a post about it? Back to the problem: going from an Enum to its numeric value or its string representation is quite easy: /* Enum to string */ string stringValue = enumVal.ToString(); /* Enum to numeric value */ int numericVal = (int) enumVal; And going from a numeric value to the Enum is quite as easy: /* Numeric value...

How to add a required validator to a CheckBoxList

If you add a normal RequiredFieldValidator and you want to validate a CheckBoxList, you will get a runtime exception informing you that the CheckBoxList cannot be validated. Sometimes you want to make sure the that user has selected at least one of the checkbox in the checkbox list, but as aforementioned, if you user a normal RequiredFieldValidator you will get an exception. You could use a CustomValidator control, but a nicer and more reusable approach is to build a custom web control that extends the BaseValidator. I found a 2001 article on how to do that (Building a CheckBoxList Validator Control) but...

SetData to clipboard failing on VirtualPC

After a nice 4 days trek on the Marlborough Sounds (pictures and short report to come soon), I'm back at work. We are close to the release of the next version of our main product, so we are setting up some VirtualPCs to test it. And we found a problem on a portion of application that never caused problems: a simple Clipboard.SetDataObject. Looking at the documentation on MSDN I found that NET 2.0 added a new overload to the SetDataObject method: public static void SetDataObject ( Object data, bool copy, int...

Going dinner with NET 3.0

The Microsoft Platform Evangelism team released a very nice technology demo product to show all the new cool technologies Microsoft just released or is going to release during that year. The demo utilizes several technologies including: IIS7, ASP.NET Ajax Extensions, Linq, Windows Communication Foundation, Windows Workflow Foundation, Windows Presentation Foundation, Windows Powershell, and the .NET Compact Framework. They are developing the demo on CodePlex, so you can download the code directly from there. The also build a web site DinnerNow.net, that will contain a working demo, and will host videos showing the features of the "product". At the moment...

Fine tune your properties with accessors

How many times did you need a property to be read-only for the users of your API, but also you needed that property to be populated by your own data access layer? It happened to me many times, and I usually solved that problem accessing directly the underlying field changing its visibility from private to internal or protected. But what if you want to access everything as a property and don't want to hook directly to the field?You need to be able to specify for the setter a different visibility for the getter. Here is how: ...

NHibernate in 8 minutes

Yesterday afternoon I delivered my first speech in English: NHibernate in 8 minutes during the Lightining session at the Wellington .NET user group meeting. I hope my English was not too bad, and that I managed in making a brief overview of the power of NHibernate. If you attended my presentation, please write a comment about it, and also if you would like to listen to a more detailed presentation of NHibernate. For the moment you can download the slides and the demo of the Lightning presentation.

Your encryption algorithm may fail moving to .NET 2.0

In my first week of work in Wellington at Calcium, I'm starting to migrate the application from .NET 1.1 to .NET 2.0. The wizard did correctly almost all of the job, and the only thing I needed to change to make it compile without too many warnings was changing the old (and obsolete) Parameters.Add with the new Parameters.AddWithValue (I think I did it 1000 times). Once it compiled we had a very strange problem: we are sending username and password, encrypting them using TripleDES, then serializing as string and then sending over the wire with remoting. And of course decrypting them...