Skip to main content

Posts

[Code refactoring] HttpClient status code

Last night I had some time to look over a project that is on CodePlex. There I found the following code: using (var httpClient = new HttpClient()) { httpClient.BaseAddress = fooUrl; FooMessage message = new FooMessage() { ... } Task<HttpResponseMessage> response = httpClient.PostAsJsonAsync("api/foo", message); response.Wait(); if (response.Result.StatusCode != HttpStatusCode.OK) { throw new Exception("..."); } } What do you see strange in this code? Well, there is a check if the return code was 200 (OK) and if not, throw an exception. This is not the wrong way to go, but you should know that HttpClient already contains a method that can check if the return code was 200 (OK). This method will throw an exception automatically if the return code is not the expected one. The above code could be written in the following way: using (var httpClient = new HttpClient()) { httpClient.BaseAd...

GET call of REST API that contains '/'-slash character in the value of a parameter

Let’s assume that we have the following scenario: I have a public HTTP endpoint and I need to post some content using GET command. One of the parameters contains special characters like “\” and “/”. If the endpoint is an ApiController than you may have problems if you encode the parameter using the http encoder. using (var httpClient = new HttpClient()) { httpClient.BaseAddress = baseUrl; Task<HttpResponseMessage> response = httpClient.GetAsync(string.Format("api/foo/{0}", "qwert/qwerqwer"))); response.Wait(); response.Result.EnsureSuccessStatusCode(); } One possible solution would be to encode the query parameter using UrlTokenEncode method of HttpServerUtility class and GetBytes method ofUTF8. In this way you would get the array of bytes of the parameter and encode them as a url token. The following code show to you how you could write the encode and decode methods. ...

[Event] Global Windows Azure Bootcamp in Cluj-Napoca, March 29

Registration link: https://codecamp-cluj-azurebootcamp.eventbrite.com One year ago Codecamp Romania added Cluj-Napoca on the map when we organized Global Windows Azure BootCamp , we here part of an event organized in more than 90 location worldwide. This year, we are doing the same thing. In March 29 we will organize in Cluj-Napoca a full day of workshops related to Windows Azure. This event is part of Global Windows Azure Bootcamp, which is organized in more 140 locations registered in 56 countries. Let’s see what will happen in Cluj-Napoca. In March 29, there will be workshops of 90 minutes, where all the attendees will have the opportunity to learn how to use different services that are available on Windows Azure. Because of this you should bring your own laptop with you and have Visual Studio 2013 or 2012, Windows Azure SDK and a free account for Windows Azure set. The event will start at 9:00 AM with a little networking. The real coding will start at 9:30 AM with a w...

[Post Event] Cluj City Apps Hackaton 14-16 March 2014

This weekend in Cluj-Napoca we had the first hackathon based on public data offered by Cluj-Napoca City Hal l. There were a lot of public data that could be used to create different application. For example people had access to data related to parking slots, how European funds are used and so on. This event was organized by Cluster Cluj IT in collaboration with Microsoft Romania , Cluj-Napoca City Hall and hosted by City Hub . At this event I was invited as mentor and to be part of the jury. I saw a lot of great ideas that could be used in everyday life to improve our life. There were a lot of application around maps and putting different data on the map. A lot of teams tried to add content to the map and on different graphical reports. BEGIN OFFTOPIC A small hint for all teams for the next time when they have an idea related to this. Try to look on the internet and find out what kind of frameworks you can use in your application. THE internet is full with free stacks and appli...

Tips and Tricks to configure Sonar Qube for .NET projects (C#)

Last week’s I had the opportunity to configure Sonar for a pretty big project. The application has over 3M lines of code and use different languages and technologies, from C# and Silverlight to JavaScript, C++ and embedded applications. The main scope was to see if we can configure Sonar for this project. We decided at this step to exclude the embedded part of the application for Sonar. This was decided because of the time limitations. I’m almost sure that we can configure Sonar for embedded part without any kind of problems. In the next part of the post I will present different issues that I encountered during Sonar configuration. Where to store custom configuration? Sonar gives you the possibility to specify different custom configuration at server level or on each project that is analyzed. Personally I prefer to have all the configuration (tools path location, unit tests projects pattern and so on) at project level – in sonar-project.properties file. For me is more easily to ...

[Code refactoring] ThreadStatic and NULL checks

ThreadStatic is used when you have content that needs to be static per thread. For example we can have a field that needs to be static per thread and not per application. In the example below we can see that _message field is decorated with ThreadStatic attribute. Each thread will have a different instance of this field. public class Foo { [ThreadStatic] private static string _messageBuffer; } Great, until now we are good. But scrolling some lines below we find the following code: public class Foo { [ThreadStatic] private static string _messageBuffer; public void DoSomeAction(string input) { if ( _messageBuffer == null ) { _messageBuffer = string.Empty; } _messageBuffer += input; } public void DoMoreAction(string input) { if ( _messageBuffer == null ) { _messageBuffer = string.Empty; } _messageBuffer += " " + input; } ... } This is not good. Why would you like to check at each call if the field is ...

[Code refactoring] From Method to Property

Looking over a project I found the following code: public class AzureLocalStorageTraceListener : XmlWriterTraceListener { public AzureLocalStorageTraceListener() : base(Path.Combine(AzureLocalStorageTraceListener.GetLogDirectory().Path, "WebService.svclog")) { } public static DirectoryConfiguration GetLogDirectory() { DirectoryConfiguration directory = new DirectoryConfiguration(); directory.Container = "wad-tracefiles"; directory.DirectoryQuotaInMB = 10; directory.Path = RoleEnvironment.GetLocalResource("WebService.svclog").RootPath; return directory; } } My attention was attracted by two things. First, was that we had a method called GetLogDirectory that returns the directory configuration for the logs. This method don’t has any custom logic there, only read a configuration from a specific location and retrieves an object populated with specific information. In this case we could have a property called LogDirectoryC...