I remember when (Accelerated Mobile Pages) first came out, and it was very restrictive and weird. I think this ultimately hurt the AMP Brand Beyond this, several companies have built AMP experiences which haven't always been the best experience. I do however think AMP pages always load extremely fast. A lot of that is just the constraints of AMP. Last night I put my blog posts on AMP for a laugh, and it was much easier than I thought it would be.
Hosting dotnet core on Heroku
I've been getting back into building scrappy little web apps for my friends. On top of this, I recently joined a startup and getting away from Enterprise class software has made me make a huge mind-shift. In the recent past when I wanted to build apps I was thinking Kubernetes, Helm Charts, etc. However, in small app, and startup land reducing the barriers to ship is very important.
Making alexa skills in .net
Ok so I've been in the alexa skills market recently, and obviously amazon wants you to use AWS Lambda for your skills. If you are like me, you have a ton of stuff in azure app service (the PaaS of azure). Azure app service supports nodejs, java, python, and of course .net. The two sdk's amazon ships (node, java) do not tie in with a web stack, and are obviously thought of as being used with Lambda.
Making a minimal webapp with dotnet core
Recently I wanted to make myself a short url host. Really, I made this not to make short urls, but to make memorable urls for myself.
StatsN a modern statsd client for dotnet core, and dotnet 4.5
tl;dr click here
When we talk about capturing metrics in applications. One server/service that constantly is in all conversations monitoring, is statsd. Incase you have never heard of it, statsd is a udp/tcp server that you send your in-code metrics to. These metrics get aggregated by statsd, and are forwarded to various backends. Some backends are services like librato or sumologic. Other times you are sending metrics to time series databases such as graphite or god forbid influxdb.
This boils down to in code you can say "log whenever this block of code is hit" or say "measure how long this function takes to execute". These stories come together to form pretty graphs, and rich alerts. All of this enabled by statsd.
Parsing cli arguments in dotnet core Console App
tl;dr view this gist
So its 2016, and we are still making console apps/cli's. In fact I would say there has been a surge in popularity of these types of tools. I think we have come to the realization that buttons on forms are not automatable, and that the command line doesn't have to be scary.
I recently started writing an app in dotnet core, which is the new runtime for dotnet. In the past I have often used command line parser, but as of this writing it does not support core.
I was really lost trying to find an arguments parsing library when I realized the dotnet cli was open sourced.
After much struggle, failing to bingle. I started ripping through the Entity Framework, and dotnet cli's code hoping to find a gem. Thats when I stumbled across a diamond. You see many dotnet projects use Microsft.Extension.CommandLineUtils to do cli parsing.
Announcing gulp-nuget-restore
So recently I have thought about build tools. We have many tools including cake, sake, albacore, and even MSBUILD. Most of these tools work well, infact they work flawlessly. I am a web developer, and I work on a team of web developers. Most of our work is in JavaScript land, with tools like React, backbone, etc. We love ES6, and we want to use things like babel. This ultimately causes us to have 2 build engines. The first being a proprietary version of albacore, and the second being gulp.
Commiting a new file to git, through the github api
Recently I have been working on an application that basically has a github bot (aka user) fork a repo, commit some files, and submit a PR against someone's repo. When it came down to actually making a new git commit through the github API, I had quite a hard time. I figured it out with some help from a ruby tutorial, and now I'm going to show you how to do it.
Wiring up client side logs into c#/node.js logging frameworks
Around a year ago I joined a new team where I work, and this team was starting to undertake a full rewrite of their code. We were going from a full c#/mvc app to a tiny c# api, and a very big SPA.
Early one one of the huge things to do was to make sure that our JavaScript error logs could land in our Log4Net infrastructure. I started to write something to do just that, and as I was coding I quickly realized this was less trivial that it sounded. We had something internal we could use, but it was tied to a lot of other code that we didn't want to pull in.
I started Bingling around and I stumbled across jsnlog. JSN log lets you quickly wire up your client side logs to your server. I have been able to get PR's into the code base and the guy behind it has been very friendly to me when I have had questions.
How .ToLookup() Saved me 4 hours a week, and got me some high praise from my boss
I recently created a small utility that is ran in jenkins to create indicies in ElasticSearch.
The first versions took around 5 hours to index our massive data into elasticsearch. This was still better than the 9 hours, our old solution took, so no one was complaining.
One of the major slowdowns was a .Where()
on a List<T>
. When I wrote the tool this TODO was written
//TODO: use some kind of key lookup here, but we need non-unique keys and Dictionaries are unique only
Drastically altering view behaviors using custom DisplayFor templates C# MVC
One feature that I think is highly under-documented in the C# MVC framework is custom DisplayFor
and EditorFor
Templates.
By simply making folders in your views folder called DisplayTemplates
and EditorTemplates
you can use the DisplayFor(a=>a.TypeHere)
and EditFor(a=>a.TypeHere)
with any custom types you so choose.
Avoid the Godclass
I joined a team earlier this year, who own a core set of pages on our website. This part of the site makes us buckets of money, and was written by people whom are clearly smarter than me. However every platform is not without its quirks.
Most of the code is C# MVC but a lot of the problems with the platform are more historic architecture, and less .NET
specific.
Razor Websites, lightweight C# web coding
I was exploring around github, and I stumbled upon an interesting project called Miniblog which was a lightweight blog engine written in c#. The thing that immediately stood out to me was the lack of a .csproj
file.
As I dug around the code I realized this was not a Web App, which most of us were familiar with, but a websites project. I then suddenly realized that the whole thing only used razor!
I am a huge fan of Nancyfx because its much more lightweight than the MVC framework created at Microsoft. To say the least I am a massive fan of small tools, and micro frameworks. So when I realized this whole thing was powered by razor only I was immediately impressed.
I decided to dig around on the internet to see if anyone else was talking about this. I found out quickly that it has been possible for some time, but I didn't find many references about it.
The one thing that bummed me out about the Miniblog example was that it was not a web app. You can use nuget packages will websites, but you cannot make references to other projects in the solution. This was a problem for me, and unlike websites, web app's are precompiled which reduces application startup time.
Hosting NancyFx with OWIN on IIS
So I was quite confused about hosting Nancyfx on OWIN under IIS. Parts of the Nancy wiki led me slightly astray.
Here is the simple guide.
Make sure you Install the following nuget packages (if you havn't already).
Working with Entity framework (Code First)
Entity Framework is the ORM that has been pushed by the MSFT giant over the last few years to the .NET community.
Binding SSL Certs on Windows Installer XML (WiX) deployed Web Applications
This tutorial is about using SSL certs with WiX for IIS websites. For those of you whom didn't know, WiX is an MSI generator. You can even deploy IIS applications with WiX's MSI's.
Using Action<>, Func<> to hide using statements
Ok so to give you all some background. I always write my data access with a repository pattern in c#. Now I often use dapper, however I'd guess this problem would also apply with Entity framework.
Anti-Forgery Tokens in NancyFX with Razor
Getting started with anti-forgery tokens in NancyFX with razor views is pretty simple.
To start you need to enable csrf in application startup.
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
Csrf.Enable(pipelines);
base.ApplicationStartup(container, pipelines);
}
Now you need to create a token on the get request that returns the form
Get["/"] = x =>
{
this.CreateNewCsrfToken();
return View["Index"];
};
<!-- more -->
Now in your view you need to render the token
<form method="POST">
Username <input type="text" name="Username" />
<br />
Password <input name="Password" type="password" />
<br />
<input type="submit" value="Login" />
@Html.AntiForgeryToken()
</form>
Finally you need to authenticate the token on the post request
Post["/"] = x =>
{
try
{
this.ValidateCsrfToken();
}
catch (CsrfValidationException)
{
return Response.AsText("Csrf Token not valid.").WithStatusCode(403);
}
//do something
};
Custom error pages in Nancyfx (C# Web Framework)
To do custom error pages in Nancy you must implement an IStatusCodeHandler. This class must provide 2 methods. HandlesStatusCode is a bool that basically should tell Nancy if this class will handle the status code. If this returns true then this class will be responsible for handling the request.
Admob with Xamarin Android Part 2: InterstitialAd
Interested in Interstital ads, but not banner? Thats ok, but I recommend your read my first post about banner ads. The first steps, installing Google Play Services, altering your permissions, adding to your manifests files, and reviewing my github demo are located in that tutorial.
Admob with Xamarin Android Part 1: BannerAd
This will be a brief overview on how to get admob working with Xamarin.
Disclaimer
I highly suggest you run this on a real phone. I'm not sure if the virtual phones can load content on the internet. I always develop on a real phone.
Sample code located in a repo at github
Abstracting Xamarin Android SharedPreferences
The standard way to get/set SharedPreferences in Xamarin is with the following code.
Get Preference:
var shared = con.GetSharedPreferences(_preferenceName, FileCreationMode.WorldReadable);
var value = shared.All.Where(x => x.Key == key).FirstOrDefault().Value;
Set Preference:
var shared = con.GetSharedPreferences("PreferenceName", FileCreationMode.WorldWriteable);
var edit = shared.Edit();
edit.PutString(key, val);
edit.Commit();
The main issue I have/had with this is you often have to know what will be returned, and what type you need to save as. Usually this isn't difficult, but it adds an un-needed level of complexity.
The other major issues I have with this, is that it is quite verbose, and unnecessary. The code duplication here can be quite high.
Xamarin For Android The Good: (Part 1 of 4)
Introduction
- Part One
- Part Two
- Part Three
- Part Four
This will be a series of blog entries where I discuss the Xamarin platform for Android.
I really enjoy C# programming language (JavaScript second)....Linq, Generics, anonymous methods, and Visual Studio are just some of the reasons I like it. Xamarin is a platform that gives you the ability you to write Android applications in c#.
When I heard about Xamarin I naturally, wanted to give it a shot. Having tried Eclipse, and Android Studio for android development I was no idiot when it came to the platform. So I got a license, and did nothing with it for six months, until a few weeks ago. After only 3 days I created Ultimate Gravatar Sync. An app that sync's your contacts gravatar images to their picture in your phone.
C# with no compromise
The Xamarin platform uses mono, and some kind of voodoo bindings to the Java libraries to make it work. I wont go in depth, but the native features of the C# language are there to use. I never felt like my hands had been tied, that all of a sudden I couldn't use a library that is normally part of the GAC (Global Assembly Cache). When I needed multi-threading, System.Threading was there, and when I needed to use C# Generics I had no issues implementing them.
Manage Android Manifest files
One of the things that blew me away about the platform, was that I never had to add anything to my manifest file. For those of you whom don't know, Android requires an XML config detailing the permissions you require, and the classes you have in your application.
Simple decoration such as:
[Activity(Label = "Label", MainLauncher = true, Icon = "@drawable/Icon")]
Will Generate in your manifest file as:
<pre>
<activity
android:label="Label"
android:name=".logoActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</pre>
Adding permissions is also easy:
[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
Using Java Libraries
Xamarin provides some kind of crazy visual studio project, that will essentially provide c# bindings to Java libraries you require. To bind Simply create a Java Binding project, adding the .Jar files, and then build. Watch the magic happen. They do note that you sometimes need to do some configuration for certain libraries, however I had no issues with the one I tried. On top of that if you really needed to, you could access the Java Native Interface for even more power.