c#

Using Qdrant for Embeddings Search with C#

I recently wrote a .NET client for the Qdrant vector database and ended up contributing it to the official Qdrant .NET SDK. A question came up from a user asking how to adapt an OpenAI cookbook article written in Python using the qdrant python client, to C# using the .NET SDK. This is the topic of today's post.

Read more...

Generate X.509 certificates with C#

A short and sweet post for today. I've often had the need to generate X.509 certificates in code, in both PEM and PKCS#12 format. It's pretty straightforward to do so with the excellent BouncyCastle.Cryptography library.

Read more...

Favouring exact matches in Elasticsearch

Ice cream!

I recently came across a question on Stack Overflow asking about Boosting elasticsearch results with NEST when a secondary field is a specific value. I thought the question was interesting enough to warrant a blog post, the first I've written in a while!

Read more...

Beware mixing Tasks with async/await

I recently came across an issue within a pipeline of asynchronous method calls where an attempt to deserialize a response from json to a known type may throw an exception, usually when the response is not json. For example, this may be the case when using something like nginx as a proxy for authentication purposes and failed authentication returns a HTML page. A simple reproducible example would be

void Main()
{
    MainAsync().Wait();
}

async Task MainAsync()
{
    var result = await TryDeserializeAsync().ConfigureAwait(false);    
    Console.WriteLine(result);
}

Task<double?> TryDeserializeAsync()
{
    try { 
        return DeserializeAsync(); 
    }
    catch {
        // swallow any exceptions thrown.
    }
    return null;
}

async Task<double?> DeserializeAsync()
{
    await Task.Delay(100);
    throw new Exception("Serialization error");
}

Here we await TryDeserializeAsync() which in turn calls the actual DeserializeAsync() implementation, swallowing any exception that may occur in deserialization and simple returning null. That's the plan anyway.

Read more...

Optimistic Concurrency with Elasticsearch and NEST

In my last post I gave an example of how to use the bulk api to index many documents at a time into Elasticsearch. Once all of the documents have been indexed, it is very likely that we'll want to update individual documents within the index; In this post, we'll look at how to do this and some of the features available to manage multiple concurrent update requests to a given document.

Read more...

Geospatial search with Elasticsearch and NEST

'Australia in 1897, etc' from the British Library - https://www.flickr.com/photos/britishlibrary/11243694643

In this blog post I’m going to show you how to get started with geospatial search with Elasticsearch, using the official and fantastic .NET client for Elasticsearch, NEST.  An example like this is best served with real data, so given this post was written from Australia, we’ll use the State Suburbs (SSC) from 2006 provided by the Australian Bureau of Statistics as the data of interest; it's provided in ESRI Shapefile format and contains a collection of all the Australian Suburbs, each with a name, code and geometry; We’ll need to extract each suburb from the Shapefile and serialize them to a format that can be persisted to Elasticsearch and so that we can query them.

TL;DR

I’ve put together a demo application to illustrate geospatial search using Elasticsearch and NEST..

Read more...

ReSharper + SemanticMerge = Streamlined Development

I wanted to share a setup that has been working well for me for some time in keeping the layout of C# code consistent, easier to maintain and smoother when it comes to merging changes into git. It leverages ReSharper and SemanticMerge, two really awesome tools that allow you to remain focused on delivering features with minimum fuss.

With this setup, a simple keyboard shortcut restructures a C# code file according to defined settings, performing such actions as reordering members according to type, accessibility and name, removing regions, sorting Using directives and updating file headers. When it comes to merging these changes into source control, we can merge with confidence as SemanticMerge parses the C# file to determine changes at the class structural level, meaning we can happily reorder members ‘til our hearts content and let SemanticMerge deal with the fallout of determining the real changes. Enough of the hyperbole, let’s get set up!

Read more...

Running MassTransit within a Topshelf Windows Service

Whenever I have a need to write a Windows Service, Topshelf is pretty much the first nuget package that I install to aid in the task. In a nutshell, Topshelf is a framework that makes writing Windows Services easier; you write a standard console application, add Topshelf to the mix and you now have an application that can be run as a console application whilst developing and debugging, and installed as a Windows Service when the need to deploy arises. It's a pretty frictionless experience, but there can be some a gotcha when using Topshelf in conjunction with MassTransit and Dependency Injection. I've experienced this a few times so thought I'd write it down here for next time!

Read more...

Nuget Package for Managing Scripts in ASP.NET MVC 4

As per the comments on my previous post about Managing Scripts for Razor Partial Views and Templates in ASP.NET MVC, I've created a Nuget package for the HtmlHelpers used. Go take a look and leave a comment if you find them useful; I plan to add additional helpers to the package over time for general functionality that is useful in any ASP.NET MVC application.

Read more...

Web API – Binding Complex types from the URI by default for HTTP GET requests

The model binding implementation in ASP.NET Web Api shares some similarities to the model binding you find in ASP.NET MVC and at the same time has some striking differences. For a more detailed discussion on web api parameter binding, check out Mike Stall’s excellent post on the topic. The topic of today’s post is how we define our own convention for parameter binding complex types when the request is a HTTP GET request.

Read more...