Tag: c#
-
Mocking FunctionContext GetLogger using Durable Functions Isolated Worker
Although you can inject your ILogger<T> into the constructor of your function, sometimes you may want to use the FunctionContext from your trigger method. Mocking this for testing can be a bit tricky. Consider the following function: We want to write tests for our class. For this to be testable, we’ll need to somehow mock…
-
NSwag build error – Project file does not exist / Unable to retrieve project metadata
While working on a project that used NSwag, when the solution was built, Visual Studio (and dotnet build from the command line) reported the following error: View the code on Gist. Pretty frustrating, as it wasn’t clear what the error meant. This project used NSwag.MSBuild to generate the client on build. As per their documentation,…
-
Better assertions when verifying Moq’d methods have been called
Moq, according to its GitHub page, is “The most popular and friendly mocking framework for .NET”. Mocking frameworks allow you to create a ‘fake’ version of a dependency (typically an interface, or abstract class). With this fake, you can then instruct the methods return values, or invoke behaviour. Alternatively you can assert the methods were called,…
-
Azure function not receiving json messages using Topic Service Bus Trigger
While developing an Azure Function application, using this tutorial, I encountered a problem. Ultimately, using `func new` generated my function (the run.csx file) which looked like this: public static void Run(string mySbMsg, TraceWriter log) { log.Info($”C# ServiceBus topic trigger function processed message: {mySbMsg}”); } Side note: the `mySbMsg` is important – it’s defined in the…
-
Out Of Memory exception when deserializing with JSON.net – Use Streams instead
Calling an api and deserializing the returned json into a type is something I have to do quite often. I used to use the following: //assume client is an instance of HttpClient – this part isn’t important var response = await client.PostAsync(url, content); //read the response as a string var responseString = await response.Content.ReadAsStringAsync(); //use…
-
Mocking calls to ApplicationContext.Current.Services
My current contract involves working on a project based on Umbraco. Unit testing Umbraco, can be a bit tricky, especially given the existence of the static ApplicationContext.Current.Services class, which contains handy references to the Umbraco services – provided by a static type! We can’t mock this. So, I created a little wrapper around this that…
-
Entity Framework migrations – names are case sensitive
In a rush, I’d called my migration “vendorServiceKEy” Which had resulted in my migration being named 201512102031458_vendorServiceKEy I then ran migrations with update-database. Now, when I renamed the class manually, to sort out my OCD, the next time my application ran, I got There is already an object named xxx in the database My _MigrationHistory table contained…
-
Could not load file or assemebly ‘ceTe.DynamicPDF.Rasterizer.40.x86.dll’
I ran into this issue when deploying an application that uses the DynamicPDF Rasterizer. After emailing support, who got back to me within the promised 24 hours, they offered some good suggestions. The solution for me was to use the installer provided on their download page, which in turn installs the pre-requisites (Visual C++ distributable)…
-
Dynamic routes with ASP.net MVC – Category/Location mapping to controller
I’ve recently been looking in to building a directory type application with Asp.Net MVC One of the requirements would be to have SEO friendly URLs such as: http://www.mysite.com/restaurants/Camberley—Surrey /restaurants being the “Category” /Camberley—Surrey being the “Location” So, I created a ‘CategoryController’ like this public class CategoryController : Controller { public ActionResult Index(string category, string location)…
-
Managing multiple MongoDB C# class maps – Part 1
I’ve used MongoDB with several net projects over the last few years, and one thing that has in the past frustrated me is the way BsonMapping is set up. This once per app setup can get pretty long, complex and hard to follow. As per the documentation this, on the face of it, it’s fairly easy:…
-
Unit tests failing when run all together with FakeItEasy
While running my tests, I noticed that if I ran my test suite all together, some would fail, giving the exception: API restriction: The assembly has already loaded from a different location This issue was happening both in ReSharper test runner, and using the NUnit test runner Debugging, the exception was being thrown on my…
-
Joe Blogs Wrapper V2 – Help required
I’m on the verge of being able to release a second, much improved version of the Joe Blogs WordPress & MetaWeblog API Wrapper. Some very brief details below, and a cry for help (or several cries!) New Features / Improvements Friendlier API Instead of having to deal with the XML-RPC interfaces directly, I’ve added a…
-
C# – String.Concat vs String.Join
I had a quick Google search for a comparison between string.concat and string.join, but I couldn’t find anything. Say you want to construct the sentence “The quick brown fox jumps over the lazy dog” This is comprised of 9 words, 8 spaces. Using string.concat: public void CreateSentanceUsingStringConcat() { string space = ” “; string the…
-
Quartz.net trigger not firing
I’ve used Quartz.net for a little while – more specifically, around 6 months. I started working with it around the end of October 2009. Irrelevant, you may think, but the important thing here is the time. During winter months, the UK runs on GMT (or UTC+0) During these months, (up until 28th March 2010) my…
-
Implementing DataCash 3D Secure with ASP.net
This is an article I’ve been meaning to write for a while now… Mainly, to help others out, who are struggling with the near non-existent documentation provided by Datacash, when trying to plug 3D Secure into my ASP.net application. I’m sure you’re already familiar with what 3D Secure is, so I won’t go in to…
-
Creating a new post in WordPress using the JoeBlogs library
A few people have recently been asking how the NewPost method works within JoeBlogs First, you need to create an instance of Post. Then, set the following properties: dateCreated Fairly self explanatory, but you should set this to today’s date (or whatever date you wish the post to be set as published) title The title…
-
NVelocity template with decimal to two decimal places
I wanted to be able to output a decimal value from an object in my NVelocity template. For example, the value of the decimal was: 3.40000 to represent 3.4. The end result: Total Order Value: 3.40 GBP I needed to display this as currency format. Sure, I could of used this String.Format method, but that…
-
C# Ternary conditional operator – Question mark in an if statement
I’m all for shortening the amount of code we write, if it makes it more readable. One enhancement I make use of in C# is the ternary conditional operator. Consider the following block of code: string input = “hi”; bool saidHello; if (input = “hello”) { saidHello = true; } else { saidHello = false;…
-
JoeBlogs – Typo in dummy URL fixed
I’ve recently had a few comments that the JoeBlogs wrapper wasn’t working – and they were getting an “invalid response” or more specifically “Response XML not valid XML-RPC – missing methodResponse element.” It seems that I had a typo in my comments in JoeBlogs.TestHarness/Program.cs on line 15: //typically http://www.yourdomain.com/xmlprc.php (if your wordpress blog is installed…
-
Reverse GeoCoding with Google API and C#
I needed to write a function to get a set of co-ordinates from an address, supplied as a string. Using the Google API, I came up with this: First, I created a struct, to represent the lat and lon co-ordinates: public struct Coordinate { private double _latitude; private double _longitude; public Coordinate(double latitude, double longitude)…
-
Using JoeBlogs -metaWeblog API Wrapper
Step 1 Download the latest release of JoeBlogs from Codeplex (Click the Downloads tab, and select the latest download) Step 2 Unzip the contents of the downloaded zip file. (I usually copy DLLs I am going to use into a “lib” folder at the root of my solution) Step 3 Add a reference to BOTH…
-
Why Are Interfaces Useful?
Interfaces, usually prefixed by “I” are useful in software engineering, for a number of reasons. Primarily, they allow you to create “pluggable” code. By this, I mean that your code is easier to manage, easier to maintain, easier to change the way certain parts of your application work, without changing the entire way it works.…
-
Specify XML-RPC Endpoints at Run Time With CookComputing XMLRPC.net
A common question when using Cook Computing XML RPC.net to talk to blogs etc… is how to specify a blog / endpoint at runtime? Most of the examples seem to specify the details in an attribute… not much use if you’re trying to develop a wrapper. To enable me to create JoeBlogs, I made use…
-
C# WordPress Wrapper
Ok, I’ve barely finished this as a stable release, but I thought I’d post this up any way…. A little open source project I’m working on – Joe Blogs. In a nutshell, it allows easy communication to your WordPress (or other blog) via an xml-rpc interface. Big thanks to the work by Charles Cook at…
-
String.EndsWithAny and StartsWithAny Extension Method
It frustrated me that while String provides the StartsWith and EndWith method, it doesn’t provide an EndWithAny. So, if I wanted to see if a particular string started or ended with any specified string, I would have to create my own method to return a true or false. Whilst this is fine, it’s a little…