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…
Month: January 2016
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…
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.
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!
Calculate the execution time of a method
I found a simple code to know the execution time of a method. First of all you have to import the following namespace: using System.Diagnostics; Then you use Stopwatch to know the execution time. Stopwatch sw = Stopwatch.StartNew(); DoSomeWork(); sw.Stop(); Console.WriteLine("Time taken: {0}ms", sw.Elapsed.TotalMilliseconds); Happy coding!
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…
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][…
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…
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…
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…
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…
Microsoft cuts off Windows 8’s security updates on January 12
Windows 8 is about to get a lot less secure. After January 12, Microsoft will stop offering security patches for the three-year-old operating system. Users will have to upgrade to either Windows 8.1 or Windows 10 to keep receiving updates. As Ed Bott notes Windows 8 is an exception to Microsoft’s typical support lifecycle policy, which provides 10 years of security fixes after the initial launch date. That’s because Microsoft considers Windows 8.1—a meaty update released nearly one year after Windows 8—to be a service pack, rather than an entirely…
Support Ending for the .NET Framework 4.0, 4.5 and 4.5.1 on Tuesday
In less than a week Microsoft will formally end support for versions 4.0, 4.5, and 4.5.1 of the .NET Framework. Users should upgrade to a later version such as the slightly incompatible .NET 4.5.2. Before we move on, it should be noted that this only affects the 4.x series. The much older .NET 3.5 SP 1 will continue to be supported. In this context, support means having access to technical support, security updates, and hotfixes. Compatibility When upgrading to .NET 4.5.2, ASP.NET developers may see a compatibility issue. Though considered…
Microsoft is pulling the plug on Internet Explorer 8, 9, and 10 next Tuesday
Microsoft is ending support for Internet Explorer 8, 9, and 10 next week on January 12th, releasing a final patch encouraging users to upgrade to one of the company’s more recent browsers. The end of support means that these older versions of Internet Explorer will no longer receive security updates or technical support, making anyone who uses them much more vulnerable to hackers. A recently-announced patch will deliver the last few bug fixes, as well as an "End of Life" notification telling users to upgrade to IE 11 or Microsoft…
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. ///…
Microsoft CEO Satya Nadella explains why it doesn’t matter if only a handful of people have a Windows phone
Microsoft CEO Satya Nadella has explained in an interview with BuzzFeed why the company is not concerned about the market share of its mobile operating system, which is about 1.7% globally. He said the company would be doing a “disservice” to itself by measuring its success solely by market share. “If you think of this more like a graph,” he said of how people use products, “these [devices] are all nodes. Sometimes the user will use all of these devices … sometimes they’ll use only one or two of our…