Contents tagged with C#
-
Distributed Cache with SQL Server in ASP.Net Core application
Performance is a key factor for any application, especially for web & mobile applications. Based on various research studies, it has been identified that high performing sites retain users than low performing sites. This means a poor performing site will have impact on the company’s business goals. So, Developers, be aware to make sure your applications are designed for faster response times.
One of the aspects that affect the performance of an application is Caching. With caching, you store the frequently accessed data and store in some temporary storage, As a developer, you should carefully consider the various caching mechanisms and implement them in your applications.
Caching in ASP.Net Application
ASP.Net supports both In-Memory Cache and Distributed Cache. Let me illustrate the basic differences.
In-Memory Cache
In-Memory Cache, your application uses Server memory to store the cached data. This works perfectly if you have only one server. In today’s world, where you use cloud services and automatically scale your applications, all your servers need to access the cached data from a single source, and In-Memory cache will not help you here,
Distributed Cache
In distributed cache, the cached data is stored and shared across all servers. ASP.Net supports several types of distributed cache implementations such as SQL Server, Redis, NCache etc.
In this article, I am going to demonstrate how to implement distributed cache with SQL Server. For the purpose of this demo, I created a .Net application using the template “ASP.Net Core Web App”. The screenshot of the app in solution explorer is given below.
-
Running background tasks in ASP.Net applications
While developing applications, you may come across various occasions where you create background services to achieve automation in your applications. Such services include sending notification, update a user’s subscription expiration or may be changing the state of a workflow activity and so on.
Developers use Windows Service, Cron jobs or Executables that works in a separate thread than your web applications that works based on a schedule. In .Net, for creating a background task, developers mainly use the Console Application or Windows Service template. While this will do the job, it adds some complexity. Few of the issues developers face with background services are below
- You may use configuration files in the web application such as connection string, logs file path etc, however for the console application, you need to redefine them under its application settings, something your operation team will not prefer, of course this adds operations complexity.
- Migrating the application may lead to troubles as I have seen several times, the migration happens without migrating the background service and then developers troubleshoot and identifies they forgot some of the services.
- Modifying the code in the application and database structure may impact the background service.
- And many more
Though it had limitations, developers used it a lot especially when they need to implement background services. Now with the ASP.Net framework has built in capability to run background tasks from the web application itself. Cool, for many of you, it must be music to your ears, now from the same project you can manage your pages, application logic and background services.
Background Service in Asp.Net application
In ASP.Net background tasks can be implemented as hosted services. In this article, I am going to demonstrate how you can develop a background service in ASP.Net
For the purpose of this demo, I used the following environment.
- ASP.Net 6
- Visual Studio 2022 Preview
- ASP.Net Web application
Also, I have a class in my application called Task that has a due date and Is Completed flag. I have a page that create a task by setting a due date. Now I am going to create a background service in ASP.Net application itself that set IsCompleted to true for tasks when it reaches the due date.
See the Task class below.
public class Task { public int Id { get; set; } public string Title { get; set; } public DateTime DueDate { get; set; } public bool IsCompleted { get; set; } public DateTime ActionDate { get; set; } }
-
Minimal APIs in ASP.Net 6 - A video walkthrough
On May 25, 2021, the .Net team released ASP.Net 6 Preview 4. You can see the announcement in the below URL.
-
Sending Push Notifications from ASP.Net Core using Google Firebase
Recently I was engaged in a project where by the customer wanted to push messages to the mobile application from their website. For e.g. when a page is published in the website, a new product added or price changed for a product, customer wanted to send push notifications to the mobile applications in these events.
-
The String Interpolation ($) Special Character
While developing applications, it is quite common that you are required to produce formatted output. This is a common scenario for any developer whether you develop console application, web application or Mobile application. With C# 6 onwards Microsoft introduced a special character called interpolation operator ($) to help developers easily manipulate the string literals.
In this article, I am briefly explaining the interpolation operator with the help of couple of examples.
Let us consider the example of adding two numbers and print their sum as the output. Very simple example. The below is the old (ugly) way of doing this in C# with ASP.Net.
int a = 2, b = 3, sum = a + b;
Response.Write("The sum of " + a + " and " + b + " is " + sum);
-
C# 7 - Return multiple values from methods
It was a long-awaited feature to return multiple values from a method. Developers used collection variables such as Array, ArrayList or Generic List to return multiple values from methods. Now with Visual Studio 2017 and C# 7, you can easily return multiple values with the help of Tuples.
In this article, I am going to explain how tuples can be used in C# 7 onwards to return multiple values.
Consider the following code from the console application. The method GetDivisionResults accepts two parameters namely number and divisor and returns two integers that are quotient and remainder.
static void Main(string[] args)
{
int number = 17;
int devisor = 5;
var result = GetDivisionResults(17, 5);
Console.WriteLine("Quotient is " + result.Item1);
Console.WriteLine("Remainder is " + result.Item2);
} -
Web API - Documentation & Testing
ASP.Net Web API is a framework for building HTTP Services. ASP.Net Web API makes it easy to write Restful applications on the .Net framework. Writing HTTP services is part of almost every developer’s job.
-
Send email using Office 365 account and C#
Microsoft Cloud offering “Office 365” is becoming popular day by day. One of the mostly used feature in Office 365 is the exchange online. Lots of customers are moving their emails to exchange online.
The developers now needs to send email notifications using Exchange online as their SMTP Server. Office 365 offers various integration options that allows your devices/applications to connect and send email. Refer the below technet article for more details.
https://technet.microsoft.com/en-us/library/dn554323(v=exchg.150).aspx
-
The Null Conditional Operator
-
Integrate Microsoft Translator into your ASP.Net application
In this article I am going to explain how easily you can integrate the Microsoft translator API to your ASP.Net application. Why we need a translation API? Once you published a website, you are opening a channel to the global audience. So making the web content available only in one language doesn’t cover all your audience. Especially when you are offering products/services it is important to provide contents in multiple languages. Users will be more comfortable when they see the content in their native language. How to achieve this, hiring translators and translate the content to all your user’s languages will cost you lot of money, and it is not a one time job, you need to translate the contents on the go. What is the alternative, we need to look for machine translation. Thankfully there are some translator engines available that gives you API level access, so that automatically you can translate the content and display to the user. Microsoft Translator API is an excellent set of web service APIs that allows developers to use the machine translation technology in their own applications.
-
Bundling and Minification in ASP.Net 4.5
Being a web developer in these days is challenging as well as interesting. With HTML 5 specifications in place, the browsers adding more client side capabilities. JQuery is playing a major role in today’s client side development. As a developer you cannot stay away from these things, you need to adapt these. At the same time you cannot develop (at least not practical as cost/time wise) all those client side scripts, you need to depend on third party JQuery plug-ins to bring attractive user friendly interfaces. There are lots of plug-ins available over internet for free that allows to use in your website. If you are developing sites, you must be using use several plug-ins in your site.
-
ASP.Net Web API in Visual Studio 2010
Recently for one of my project, it was necessary to create couple of services. In the past I was using WCF, since my Services are going to be utilized through HTTP, I was thinking of ASP.Net web API. So I decided to create a Web API project. Now the real issue is that ASP.Net Web API launched after Visual Studio 2010 and I had to use ASP.Net web API in VS 2010 itself. By default there is no template available for Web API in Visual Studio 2010.
-
Coexistence of projects between Visual Studio 2010 and 2012
Microsoft has released another version of Visual Studio named Visual Studio 2012. As you can see there are user interface (UI) changes in all/most of the Microsoft applications as Microsoft is moving towards Windows 8 and changing the UI scheme for all of the applications. Visual Studio 2012 is a move to adapt the new interface requirements that are in coherent with Windows 8. Not only this Visual Studio 2012 has lots of improvements in several areas and it supports .Net framework 4.5.
-
Change default language settings in Visual Studio 2012
The first thing you need to do after the installation of Visual Studio 2012 is to choose the IDE preferences. Once you select your preferred collection of settings, the IDE will always choose dialogs and other options according to your selection. Nowadays developer’s needs to work with different programming environments and due to this, developers might need to reset the default settings. In this article, I am going to demonstrate how you can change the default settings in Visual Studio 2012.
-
Extension methods in .Net framework
Recently one of my friend asked me about extension method. Though it was introduced in .Net framework 3.0, lots of people are not using this or not aware of the power of extension methods. So I decided to write a blog post about this to give an introduction to the extension methods.
-
Integrate Bing Search API into ASP.Net application
Couple of months back, I wrote an article about how to integrate Bing Search engine (API 2.0) with ASP.Net website. You can refer the article here
-
Integrating Flickr with ASP.Net application
Flickr is the popular photo management and sharing application offered by yahoo. The services from flicker allow you to store and share photos and videos online. Flicker offers strong API support for almost all services they provide. Using this API, developers can integrate photos to their public website. Since 2005, developers have collaborated on top of Flickr's APIs to build fun, creative, and gorgeous experiences around photos that extend beyond Flickr.
-
Getting started with LINQ to SharePoint
One of the major advantages of SharePoint 2010 is the support for LINQ. If you are new to LINQ, I would recommend you to read the below article from MSDN.
-
Integrate BING API for Search inside ASP.Net web application
Update: Since Bing has moved Search API to Windows Azure Market place, I wrote a new article about the new Bing Search API. You can read this from the below link
-
SharePoint 2010 : Sharing data between Visual Web Parts
Web Parts are essential parts of SharePoint 2010 that helps developers and site owners to customize the SharePoint portal. There are lots of out of the box web parts available in SharePoint 2010. When you are building business portals definitely you will be in need to create your own web parts corresponding to your business requirements. SharePoint 2010 supports Visual web parts. Visual web parts allow developers to easily design the user interface as they are doing in conventional ASP.Net applications.