Key layers of distributed applications

Every application that is going to be used by end users should be designed appropriately as users are expecting to process information from various data sources that might be geographically distributed. They are also expecting this information to be up-to-date and capable of being inflected very fast. Designing such applications is not an easy task and involves integration among different groups of components. Let’s review the layers that form a typical distributed application. The responsibilities in a distributed system can be divided into four layers: The data layer The business…

Read More

C# and HTML5: drag and Drop elements

HTML5 API includes Drag and Drop (DnD) native functionality. The event listener methods for all the drag and drop events accept Event object which has a readonly attribute called dataTransfer. The event.dataTransfer returns DataTransfer object associated with the event This is the list of events fired during the different stages: Event Description dragstart Fires when the user starts dragging of the object. dragenter Fired when the mouse is first moved over the target element while a drag is occuring. A listener for this event should indicate whether a drop is…

Read More

World’s First Autonomous Drone, The DJI Phantom 4, Revealed

Recently, the Irish Internet of Things chip maker Movidius, has won a multi-million dollar deal with Google, and it has been revealed as the brain behind the breakthrough autonomous drone, the DJI Phantom 4. This autonomous drone, which went on sale in Apple Stores across the US last week, features a new visual guidance facet, which sticks to and follows a subject, and can stay in a fixed position without the need of a GPS signal. Described as the first in the industry, the addition of Movidius chips technology and…

Read More

Integration with C# and ReactJS

The goal for this project is to show a list of books with ReactJS from a WebAPI written in C#. You can download this project from Github. New project Start by creating a new ASP.NET MVC 4 project: 1. File → New → Project 2. Select ".NET Framework 4" and Templates → Visual C# → Web → ASP.NET MVC 4 Web Application. Call it "CSReact" 3. In the "New ASP.NET MVC 5 (or 4) Project" dialog, select the MVC (or empty) template. Install ReactJS.NET We need to install ReactJS.NET to…

Read More

ReactJS: component Lifecycle and Rendering

These are the functions that will be, or can be, included in your specification object when you’re creating a component. Part of these specification functions are lifecycle functions, which when encountered, will show the details as to when they execute during the life of a component. Function invocation order during the initial render of a React component Component lifecycle that happens when the state changes on a component Lifecycle of a component when it has altered props render Every React component must have a render function. This render function will…

Read More

How to check if IsNumeric?

If you need to check if something is a number, in C# doesn’t exist a function for that. That I’ve created the following code. public bool IsNumeric(object Expression) { if (Expression == null || Expression is DateTime) return false; if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean) return true; try { if (Expression is string) Double.Parse(Expression as string); else Double.Parse(Expression.ToString()); return true; } catch { } // just dismiss errors…

Read More

Customize Json result in Web API

If you work with Visual Studio 2015 and WebAPI, this short post is for you! We have to make our Web API project easy to debug so, I’m going to remove the XML formatter. I’m doing that because I’m in a test project and I’d like to see the response in the browser. The easily way to force the response to Json for all Web API requests is to remove the XML. Obviously you shouldn’t do it in production. Global.asax var formatters = GlobalConfiguration.Configuration.Formatters; formatters.Remove(formatters.XmlFormatter); Now we can start change…

Read More

Creating a URL shortener using ASP.NET WepAPI and MVC: error handling

In the two previous post I discussed about the first step to creare this application and the implementation of the business logic. Now we can implement the error handling. We have two custom exception classes: ShorturlConflictException (when a segment already exists) and ShorturlNotFoundException (when a segment isn’t found in the database). There is also a third type: an unexpected exception, for example when there is no database connection. Normally, the user will see these errors. We have to build a mechanism where these exceptions are caught and a nice error…

Read More

Creating a URL shortener using ASP.NET WepAPI and MVC: implementing the business layer

In my previsious post I discussed the first implementation of this application. In this post I’m explained how to implement the business layer. First of all you make sure the Business project references the Data, Entities and Exceptions projects. In the Exceptions project you add two new classes: ShorturlConflictException using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PSC.Shorturl.Web.Exceptions { public class ShorturlConflictException : Exception { } } ShorturlNotFoundException using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PSC.Shorturl.Web.Exceptions { public class ShorturlNotFoundException : Exception {…

Read More

Agile approaches

Agile Wallpaper

There are a number of specific Agile approaches, as well as a generic agile style of working. Extreme Programming is a software development methodology, containing mainly programming practices such as Test Driven Development, Pair programming and Continuous Integration, but little management. Lean, which came from the manufacturing environment, is all about efficient processes. The focus is on eliminating waste from the production line and thereby reducing cost. Scrum is a very simple agile process, designed for delivering software in small chunks, taken from a backlog of work to be done.…

Read More

Davos: Smart machines set to transform society

Artificial intelligence will spur economic growth and create new wealth. Machines that “think” like humans will help solve huge problems, from curing cancer to climate change. Yet millions of human workers will need to retrain, as robots make their existing jobs redundant. These are the contrasting messages provided by the world’s leading technologists during the World Economic Forum in Davos this week, as political and business leaders ponder how best to respond to the rise of smart machines. Sebastian Thrun, the inventor of Google’s self-driving cars and an honorary professor…

Read More

Creating a URL shortener using ASP.NET WepAPI and MVC

In this tutorial, I use several techniques and tools. I use Microsoft Visual Studio 2015 and the latest version of all components. ASP.NET MVC: Microsoft’s modern web application framework. As the name says, it pushes you to use the MVC (model view controller) software design principle. ASP.NET Web API: Web API and MVC are used together in many applications. With MVC, the HTML of the web pages are rendered on the server, and with Web API you can, like the name says, create an API. Web API also uses the…

Read More

Throw vs Throw e

Some people asked me the different between those two following code: Code 1 try { … } catch (Exception ex) { throw ex; } Code 2 try { … } catch (Exception ex) { throw; } I don’t think there is ever a good reason to write catch (Exception e) { throw e; }. This loses the original stacktrace. When you use throw; the original stacktrace is preserved. This is good because it means that the cause of the error is easier to find.

Read More

C# Faster way to truncate a string

Very often I found this problem and now I got a solution! I’ve compared some methods and I was decided which one. But I verified time (if you want to know how, read this post) of them and the final solutions are: static string TruncateLongString(string str, int maxLength) { return str.Substring(0, Math.Min(str.Length, maxLength)); } or string TruncateString(string str, int maxLength) { return new string(str.Take(maxLength).ToArray()); } The best solution than even is the second one!

Read More

String to Hex – Hex to String Convert

You can convert a string to hex or vice-versa with this methods. Example for real world; You want set / get some data from URL, but if your data has some special chars (like ^$ %) it’ll be problem… In this case, you can use this methods and convert your data to hex. public string ConvertStringToHex(string asciiString) { string hex = ""; foreach (char c in asciiString) { int tmp = c; hex += String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString())); } return hex; } public string ConvertHexToString(string HexValue) { string StrValue = ""; while…

Read More

Regular expression for UK postcode

I’m looking for an answer to my regular expresssion problem in c#. I’m looking for a match on a specific postcode format and have run into problems. Here you can find my solution with the regex pattern. C# public bool IsPostCode (string postcode) { return ( Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][A-HJKS-UWa-hjks-uw][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) || Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][A-HK-Ya-hk-y][0-9][A-Za-z][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z{2}$)”) || Regex.IsMatch(postcode, “(^[Gg][Ii][Rr][]*0[Aa][Aa]$)”) ); } Visual Basic Public Function IsPostCode(postcode As String) As Boolean Return (Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][ ]*[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}$)”) OrElse Regex.IsMatch(postcode, “(^[A-PR-UWYZa-pr-uwyz][0-9][0-9][…

Read More

OWIN and Facebook: the developers of this app have not set up this app properly for Facebook Login?

Did you received this error when you try to login in your Owin app with Facebook? App Not Set Up: This app is still in development mode, and you don’t have access to it. Switch to a registered test user or ask an app admin for permissions. Solution Go to https://developers.facebook.com/ Click on the My Apps menu on the top bar and select your appThe circle next to your app name is not fully green. When you hover mouse on it, you’ll see a popup saying, "Not available to all…

Read More

C# ASP.NET MVC OWIN and Twitter authentication error

We have an MVC project using OWIN Framework to allow our users to authenticate using Twitter. However starting today, we have been getting this exception when trying to authenticate: System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure. Thanks to the power of open source we can see that the thumbprints for the twitter certificates have been coded in the Katana Project. Microsoft.Owin.Security.Twitter.TwitterAuthenticationOptions Recently some certificates must have changed and now the thumbprints no longer match. Please add a new thumb print for the "VeriSign Class 3 Public…

Read More

ASP.NET MVC Return image dynamically drawn in controller

If you have problem returning dynamically drawn image from controller below informations may help. The task looks easy and should be achieved with this code: public ActionResult Image(string text) { //Create new image Image img = new Bitmap(100, 50); Graphics g = Graphics.FromImage(img); //Do some drawing Font font = new Font("Arial", 24); PointF drawingPoint = new PointF(10, 10); g.DrawString(text, font, Brushes.Black, drawingPoint); //Return Image MemoryStream ms = new MemoryStream(); img.Save(ms, ImageFormat.Png); return new FileStreamResult(ms, "image/png"); }   Unfortunately this won’t work. At least not in .NET 4.0. Perceptive person will…

Read More

ASP.NET MVC OWIN and Microsoft account

Register an app in the Microsot Account Developer Center Go to the Microsoft Account Developer Center and create a new application. After you have registered the application take note of the App ID and App Secret: Install the Nuget Package Install the Nuget Package which contains the Microsoft OAuth provider. Install-Package Microsoft.Owin.Security.MicrosoftAccount Register Provider Locate the file in your project called \App_Start\Startup.Auth.cs. Ensure that you have imported the Owin namespace: using Owin; In the ConfigureAuth method add the following lines of code: app.UseMicrosoftAccountAuthentication( clientId: "Your client ID", clientSecret: "Your client…

Read More

The current type, is an interface and cannot be constructed. Are you missing a type mapping?

You might have missed to register your Interface and class (which implements that inteface) registration in your code. e.g if the error is "The current type, xyznamespace. Imyinterfacename, is an interface and cannot be constructed. Are you missing a type mapping?" Then you must register the class which implements the Imyinterfacename in the UnityConfig class in the Register method. Using the following code as an example using System; using Microsoft.Practices.Unity; using Microsoft.Practices.Unity.Configuration; using PSC.Shorturl.Web.Business; using PSC.Shorturl.Web.Business.Implementations; namespace PSC.Shorturl.Web.App_Start { /// /// Specifies the Unity configuration for the main container. ///…

Read More