Home » C#

Category: C#

How to port from .net framework to .net standard

The last days I spent some time to port my NuGet packages (AzureBillingApi and FeedReader) to .net standard. As it was not so straight forward, I want to share what I did. The migration is certainly more complicated the larger the project is, but this post will hopefully give you some basic insights on how to migrate. The most time-consuming part will be to change the existing code so that it works with .net standard. In this blog post, I’ll focus on how to change the project to use .net standard and .net framework. In this case, I’ll port the AzureBillingApi to .net standard 1.4, but it will still support .net framework 4.5.2.

Prerequisites

Before migrating the project, you should at first check if the NuGet packages that you use in your project are already compatible to .net standard. There are different ways how to check it. You can create a new .net standard project and manually add all NuGet packages to it – then try to build it. This will immediately show you if these packages support .net standard or not.
Another way is to go to nuget.org or use the NuGet Package Explorer and check the dependencies (Dependencies section) for all your packages.

Read more

Return JSON from C# Azure function

In this blog post I’ll build a simple C# Azure function that returns an object as JSON. That’s useful if you want to build a simple “API” or if you just want to return some information in a structured format. Such a function could read data from an on-premise environment and provide this data to a logic app, because it’s much easier to connect an Azure function to on-premise than a logic app.

Create a C# Azure Function

First step is to create a new C# function. I’ll use the HttpTriggerWithParameters-CSharp template and I’ll use the authorization level ‘Anonymous’ (that’s okay for this demo):
Read more

Microsoft Cognitive Services – Translation API

If you want to programmatically translate text from one language to another, then the Translation service (translation api) is the right one for you. The Microsoft Translation Service just requires an authentication and then you can easily translate text from one language to another. It sounds simple…and it is simple! In this blog post, I’ll quickly describe how to create the translation service and how to use it with C#, Powershell and Node.js.

Create the translation service in Azure

The translation service is part of the cognitive services and can therefore be found as cognitive service in the Azure portal:

Read more

Microsoft Cognitive Services – Face API

The Face API which is part of the Microsoft Cognitive Services helps to identify and detect faces. It can also be used to find similar faces, to verify if two images contain the same person and you can also train the service to improve the identification of people. In this blog post, I’ll just use the detect service which detects faces and shows the age, gender, emotions and other data of the detected face.

Prerequisites: Create the Face API Service in Azure

As all Microsoft cognitive services, you can also create the face API service in Azure via the portal. It is part of the “Cognitive Services APIs”, so just search for it and create it. Select the Face API as the type of the cognitive service and check the pricing options:

Read more

Microsoft Cognitive Services – Computer Vision

The Computer Vision API which is part of the Microsoft Cognitive Services and helps to analyze images, retrieve information from images or even to modify them a bit. It currently supports the following cases:

  • Analyze an Image: Returns categories and tags (e.g. “mountain”, “outdoor”), detects celebrities on the picture, checks if it contains adult or racy content, gives a description of the image, detects faces (e.g. male, 20 years), analyzes colors and the image type.
  • Get a thumbnail: returns a thumbnail of the image.
  • OCR (optical character recognition): detects text on an image.
  • Analyze videos: does the same as analyze images, but for videos.

In this blog post, I’ll use C# to analyze images, to do an OCR and I’ll create a thumbnail.

Prerequisites: Create the Computer Vision service in Azure

The first step for using the computer vision API is to create the service. It is part of the “Cognitive Services APIs” and can be created in the Azure portal:

Read more

Send Mails with C# in Azure

The following blog post is about sending emails in/via Azure with C#. There are currently two services available that you can easily create in Azure – MailJet and SendGrid. You can also use them without Azure or you can use another service or your own Mail server, but in this post, I’ll focus on the services in Azure and how to use them. If you want to send mails from an Azure function, web job, web app or any other “application” in Azure, then the two services are good and easy to use. Both services, SendGrid and MailJet offer a free plan that allows to send up to 25k e-mails per month. Both of them work fine and are easy to configure, that’s why I’ll describe how to use the services.

SendGrid

Create and configure SendGrid service

The SendGrid service is available in the Azure marketplace, so you can simply add it via the portal. Just search for “SendGrid” and you’ll find the service “SendGrid Email Delivery”. If you create it, then you can also see the pricing:

When the service is provisioned, we have to create an API key that we use to send the mails. Open the SendGrid service in the Azure portal and go to manage:

Read more

Return HTML or file content from C# Azure function

I’m currently implementing different Azure functions and these days I wanted to return a simple HTML document via an Azure function. So I did it at first for a simple (“hello world”) Azure function and used the code later in my real function.

I developed the sample online (in the Azure portal), but you can also use Visual Studio for it. Just add a new “HttpTrigger – C#” function and set the authorization level (I used anonymous – so no key is required):

After that, I changed the code of the function:

using System.Net;
using System.Net.Http.Headers;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");

    string html = @"<html>
<head><title>Hello world!</title></head>
<body>
<h1>Hello World!</h1>
<p>from my <strong>Azure Function</strong></p>
</body>
</html>";
    
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StringContent(html);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("text/html");
    return response;
}

Read more

Creating NuGet packages for C# libraries

A few weeks ago I published my first C# NuGet package. The package is a RSS and ATOM FeedReader library that I developed, because existing libraries had issues with encodings, different languages or others. In this blog post, I will quickly describe how to create a NuGet package for a C# library.

The first thing to do is to install nuget.exe. This can be done by downloading the nuget.exe from nuget.org or by using chocolatey. I already blogged about how to use chocolatey (Chocolatey – Package management for Windows) and we will reuse that for the nuget install.

Read more

Export Azure Usage data to CSV with C# and Billing API

The first important thing to mention is, that there are two types of the Azure billing API. One is the billing API for EA (enterprise agreement) customers. This API is easier to use, because it returns the costs in a separate field (extendedCosts). If you have an enterprise agreement, I recommend to check the following blog post: http://www.redbaronofazure.com/?p=631.

The other one is the billing API for non EA users (see Azure Billing REST API Reference. This API has two services, the RateCard API which returns costs for your resources (e.g. S1 DocumentDB costs per hour in region West Europe). I already blogged about how to export the RateCard data to CSV. In this post, I’ll focus on the second one which returns the usage data. This service returns the usage of your resources – e.g. “Standard IO – File Write Operation Units (in 10,000s), quantity: 3.182, …”. Unfortunately, the usage API does not return the costs per resource. If you want to know the costs, then you need to combine the RateCard data with the Usage data.

Update February 7, 2017: I built a .net library and published it as NuGet package which you can use to get data from the usage api and the ratecard api. The library also combines the usage and ratecard data and calculates the costs. The code is published on GitHub and I blogged about the usage and the configuration of it:

This blog post is still valid, it shows the how to configure and build an application that uses the Azure Billing Usage REST API.

Read more

Export Azure RateCard data to CSV with C# and Billing API

The Azure Billing API allows to programmatically read Azure costs. On the one hand, there are is the usage of your resources (how much [unit] you already used for [resource]), on the other hand there are the basic costs for resources – so e.g. the costs for a storage account or a specific virtual machine in a specific region. Each of these two use cases has its own API: the RateCard API which returns the potential costs of the resources (“basic costs”) and the Resource Usage API which returns the usage of your resource of a subscription.

In this blog post, I’ll focus on the RateCard API. There is a sample available on GitHub, but when I used it, I ran into some issues. So I decided to developed a simple C# console application and blog about my experiences. The application reads data from the RateCard API and creates a CSV file out of it. The CSV file can be opened in Excel and should already help to do some calculations.

The application/this post is mostly inspired by: https://github.com/Azure-Samples/billing-dotnet-ratecard-api.

Update February 7, 2017: I built a .net library and published it as NuGet package which you can use to get data from the usage api and the ratecard api. The library also combines the usage and ratecard data and calculates the costs. The code is published on GitHub and I blogged about the usage and the configuration of it:

This blog post is still valid, it shows the how to configure and build an application that uses the Azure Billing RateCard REST API.

Read more