C#: Favorite Features through the Years

C#: Favorite Features through the Years

Abstract: Each newer version of C# is packed full of powerful and impactful features. In this tutorial I walk through the various versions of C# and share my favorite features from each release.  516    59    68 Anytime I get the chance to write about C#, I’m eager to do so. This time was no System.Exception! As of this writing, C# has been around for over 17 years now, and it is safe to say it’s not going anywhere. The…

Read More Read More

Guidelines for Collections

Guidelines for Collections

Source Any type designed specifically to manipulate a group of objects having some common characteristic can be considered a collection. It is almost always appropriate for such types to implement IEnumerable or IEnumerable<T>, so in this section we only consider types implementing one or both of those interfaces to be collections. X DO NOT use weakly typed collections in public APIs. The type of all return values and parameters representing collection items should be the exact item type, not any of its base types…

Read More Read More

Cleaning up your codebase with a clean architecture

Cleaning up your codebase with a clean architecture

Source Let’s talk software architecture. Most of us know MVC, it’s the foundation for pretty much every web framework. As your product grows though, problems with MVC will start to appear. Even a relatively simple product can end up with a bloated and messy codebase. MVC is where we start, but what do you do when you need to evolve past it? Before we go further, let’s examine why we have so much trouble explaining the answer. Here’s a common conversation…

Read More Read More

Returning binary file from controller in ASP.NET Web API

Returning binary file from controller in ASP.NET Web API

Source Try using a simple HttpResponseMessage with its Content property set to a StreamContent: // using System.IO; // using System.Net.Http; // using System.Net.Http.Headers; public HttpResponseMessage Post(string version, string environment, string filetype) { var path = @”C:\Temp\test.exe”; HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK); var stream = new FileStream(path, FileMode.Open, FileAccess.Read); result.Content = new StreamContent(stream); result.Content.Headers.ContentType = new MediaTypeHeaderValue(“application/octet-stream”); return result; } A few things to note about the stream used: You must not call stream.Dispose(), since Web API still needs to be able to access it when it processes the…

Read More Read More

How to make IIS7 play nice with Office Interop

How to make IIS7 play nice with Office Interop

Source I’ve just started working on a new project for which we had to dig out one of our older libraries. We wrote it to retrieve data from MS Excel workbooks using the Office interop assemblies, and thus far it has never given us issues. A slight change this time round was that we were using our library in an ASP.NET site rather than a desktop application. As a result I was faced with two errors that we’d never seen…

Read More Read More

Setting up MVC 5.2.3.0 site with .Net 4.6 on IIS 7.5 on 2008R2SP1

Setting up MVC 5.2.3.0 site with .Net 4.6 on IIS 7.5 on 2008R2SP1

Thank you for that. After a bit of Googling I documented my setup roughly like this: – Install .Net 3.5.1 Features via Server Manager -> Add features wizard – Install .Net 4.6: – Install IIS 7.5: — Make sure you include the following features in IIS: IIS management console, IIS Management Script and Tools, Management Service, Static content, Directory Browsing, HTTP Errors, HTTP Redirection, ASP, ASP .Net, ISAPI Extensions, ISAPI Filters, .Net Extensibility, Default Document, HTTP Logging, Logging Tools, Basic…

Read More Read More

ASP.NET MVC – Set custom IIdentity or IPrincipal

ASP.NET MVC – Set custom IIdentity or IPrincipal

Source I decided to use IPrincipal instead of IIdentity because it means I don’t have to implement both IIdentity and IPrincipal. Create the interface interface ICustomPrincipal : IPrincipal { int Id { get; set; } string FirstName { get; set; } string LastName { get; set; } } CustomPrincipal public class CustomPrincipal : ICustomPrincipal { public IIdentity Identity { get; private set; } public bool IsInRole(string role) { return false; } public CustomPrincipal(string email) { this.Identity = new GenericIdentity(email); }…

Read More Read More

WEB API 2 EXPLORING PARAMETER BINDING

WEB API 2 EXPLORING PARAMETER BINDING

Source This article demonstrates how to call or map Web API methods with different types of parameters using XML, Json and Urlencoded formats. It shows how simple parameters, objects parameters and array parameters can be sent either in the body of the Http request or in the Url itself. This all works per default in Web API and if that’s not enough, you can customize it yourself. code: https://github.com/damienbod/WebApiParameters Simple Parameters Example 1: Sending a simple parameter in the Url 1 2…

Read More Read More

Send string array to web api

Send string array to web api

You can send it in the body or the URL URL: // http://localhost:49407/api/values/example6?paramsObject=2,paramsObject=4,paramsObject=9 [Route(“example6”)] [HttpGet] public string GetListFromUri([FromUri] List<int> paramsObject) { if (paramsObject != null) { return “recieved a list with length:” + paramsObject.Count; } return “NOTHING RECIEVED…”; } or in the body: [Route(“example8”)] [HttpPost] public string GetListFromBody([FromBody] List<ParamsObject> paramsList) { if (paramsList != null) { return “recieved a list with length:” + paramsList.Count; } return “NOTHING RECIEVED…”; } User-Agent: Fiddler Content-Type: application/json Host: localhost:49407 Content-Length: 91 [{“Id1″:3,”Id2″:76,”Id3”:19},{“Id1″:56,”Id2″:87,”Id3”:94},{“Id1″:976,”Id2″:345,”Id3”:7554}] Here’s example…

Read More Read More

Don’t Block on Async Code

Don’t Block on Async Code

Source This is a problem that is brought up repeatedly on the forums and Stack Overflow. I think it’s the most-asked question by async newcomers once they’ve learned the basics. UI Example Consider the example below. A button click will initiate a REST call and display the results in a text box (this sample is for Windows Forms, but the same principles apply to any UI application). // My “library” method. public static async Task<JObject> GetJsonAsync(Uri uri) { using (var client =…

Read More Read More