Skip to main content

Posts

Showing posts from March, 2014

[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

[Code refactoring] Class name and namespace with the same name (deadly sin)

Looking over some ‘old code’ (3-4 years old) I discovered a class nested under a namespace with the same name. For example we have a class called Foo and the namespace above is also called Foo App.Foo.Foo In that moment I told to myself something is not okay and we need to do something to change this. The good part was that this class was in internal class, used by other subcomponents – no public API there. I don’t know how I missed this problem until now. The fix was very simple, renaming the namespace to a more suitable name. Let’s see why we shouldn’t have a class and a namespace with the same name. First of all you will have problem referring the namespace and the class. You will have to use the full namespace for the class or an alias. This can create confusion to developers, especially because they will never know when the class is referring or what the alias is. You can have different standards to name an alias but the scope is not to hack the system. Namespace are used to

Internal Static IP on VM in a Virtual Network from Windows Azure

In general I don’t discuss about IT PRO features from Windows Azure, but some of them are very important. Why: because in the end we design solution to run somewhere. In today post we will talk about how static IP on VM from Windows Azure. I assume that this is the first time when you hear about this subject. Let’s start with the begging. On Azure we have a Virtual Network where we can add our VM. Of course each machine from this network will have an IP based on the Virtual Network configuration (subnet range, available IPs in our Virtual Network and so on). When we restart of machine (OS upgrades, application upgrades, …) the IP allocated to the machine will remain the same. If we deallocate one of VM machines from Virtual Network, the IP will be automatically available to other new VM that will be added. This means that if we have a machine with 192.168.1.2 (internal IP) and we deallocate it, the next VM that will be added to the network will have this IP, even if there will be o

AOP using RealProxy (custom implementation) - Part II - Register to a IoC container

In the last post of this series we saw how we can use RealProxy to implement our custom AOP stack in .NET that can measure how long a method lasts. In this post we will see what is the mechanism that can be used to register our instance to a IoC container with or without a duration profiler (with or without a proxy). The solution is pretty simple and can be used with any kind of IoC stack. Basically, when we register our instance to the container we will detect if we need or not to measure how long the methods takes place from our specific class. Based on this information we can register to the container an instance of DurationProfilingDynamicProxy (our custom RealProxy) or not. In the next example, using a flag read from configuration file we create a specific instance of our Foo objects. private static IFoo CreateFooInstance(bool isFooProfillingActive) { IFoo fooOriginalInstance = new Foo(); IFoo fooContainerInstance = null; if (isFooPr