0 Comments
  • Posted in:
  • C#

With the launch of Visual Studio 2017 I thought it might be good to give a quick introduction to some of the new language features we now have in C#. One of the ones I was most looking forward to is tuples, so I made a quick video showing how this feature which I really didn’t like before has been improved in C# 7 to be something genuinely useful.

For more examples of how to use C# 7 tuples as well as the other new features, check out this article.

0 Comments

I recently created an Azure Functions backend implementation for the Todo-Backend project, using functions written in F#, JavaScript and C# and storing the data in Azure Table Storage. One of the really cool things about Todo-Backend is that you can run an automated test suite against your API by visiting their online test runner and pointing it at your Azure Functions app.

Now this is great for running against a deployed Azure Functions app, but what if you wanted to test against the local host that comes with the Azure Functions CLI? Although the Todo backend unit test suite runs locally in the browser, if we start our local host in the usual way with func host start, we’ll find that we can’t connect to localhost due to CORS restrictions.

I was sure there ought to be a way to enable CORS for the local host, and sure enough thanks to ayls on StackOverflow, I discovered how we can do this. You simply need to pass the --cors flag followed by the host you want to allow (* is usually sufficient, although you can provide a list).

What this means is that you can clone my func-todo-backend git repo, point it at a table storage account, and then you can run it locally with func host start --cors * and try the test runner against it by visiting http://todobackend.com/specs/index.html?http://localhost:7071/api/todos.

And sure enough, you can run the test suite locally:

image

Want to learn more about how easy it is to get up and running with Azure Functions? Be sure to check out my Pluralsight course Azure Functions Fundamentals.

0 Comments

If you’ve ever needed to get hold of the duration of a media file such as an MP4 or MP3 file in C#, you’ve probably discovered there are a whole bunch of possible techniques, and its hard to know which one to pick. None seem ideal, and all either involve referencing other libraries or using a bit of PInvoke.

My preference is to get the value from the Windows Shell, and the the helpful Microsoft.WindowsAPICodePack-Shell NuGet package contains wrappers for the relevant APIs. With this  NuGet package installed, we can very simply ask for the video duration as a TimeSpan like this:

private static TimeSpan GetVideoDuration(string filePath)
{
    using (var shell = ShellObject.FromParsingName(filePath))
    {
        IShellProperty prop = shell.Properties.System.Media.Duration;
        var t = (ulong)prop.ValueAsObject;
        return TimeSpan.FromTicks((long)t);
    }
}

And that’s all there is to it. Has worked well for me so far.