Attach to Process

dotnet

Fire and forget template for C#/.NET to run something that you don't need to wait on and don't care about the results.

Task.Run(() =>
{
	//code goes here
});

In ReactJS, if ESLint says you cannot use inline arrow functions to pass in props in JSX, a solution is to wrap them with the useCallback hook.

Reference: https://stackoverflow.com/a/36677798/5041911


Modals — can be used to add dialog or popup info boxes on your ReactJS app.


So using this library actually helped me solve my Azure Application Insights logging issue. The issue being, since I had a dateOfBirth object in my formData object, app insights would save it as an object, as opposed to something like dateOfBirth: 2024-04-16. The solution was to flatten the formData object so it will get logged properly in Azure App Insights.


Rendering the HTML string in React — short tutorial on how to render an HTML string in ReactJS/Gatsby.


Series: #DevNotes Tags: #CSharp #DotNet #Template #ReactJS #Azure #ApplicationInsights #Gatsby

Discuss... or leave a comment below.

Version 1.2.1 of WriteAs.Net has been released.

This very minor update adds an interface for the WriteAsClient class, which opens it up for use with dependency injection in ASP.NET Core. This allows you to create a single WriteAsClient instance and re-use it when needed, making the caching additions from Version 1.2.0 much more useful.

This also frees me up to make better use of it in the API I'm working on.

You can install it via nuget: Install-Package WriteAs.NET -Version 1.2.1

Or via the .NET Core command line interface: dotnet add package WriteAs.NET --version 1.2.1

If you find any bugs or issues with it, please let me know. Thanks and y'all have a good night.

Tags: #DotNet #WriteAs #WriteAsNet

Discuss... or leave a comment below.

Cache Implementations in C# .NET — good blog post on implementing caching with .NET.


You're using HttpClient wrong and it is destabilizing your software — great blog post that told me that we should avoid the use of the “using statement” when working with an HttpClient instance. And that's because disposing it after say a one time use, like doing one API call and then immediately disposing, will leave open/pending socket connections. Do this often enough and you'll accumulate a number of those open/pending socket connections and that will slow down your app. The better approach is to use a single static HttpClient instance in your app.


The always-recent guide to creating a development environment for Node and React (with Babel and Webpack) — good guide to setting up a full-stack JavaScript development environment on your local, with an eye toward ReactJS.

Read more...

How to check if array contains a specific item

To check if an object or item is contained in a JavaScript array, you can use the includes function, like so:

const customerIds = [1, 2, 3, 4, 5];
console.log('IsIncludedInArray', customerIds.includes(3));

To do the same thing in C#, you can use the Contains or Any LINQ method, like so:

var customerIds = new int[5] { 1, 2, 3, 4, 5};
Console.WriteLine("IsIncludedInArray " + customerIds.Contains(3));
Console.WriteLine("IsIncludedInArray " + customerIds.Any(n => n == 3));

How to filter out contents in an array

To filter out the contents of an array in JavaScript, you can use the filter function., like so:

const customerIds = [1, 2, 3, 4, 5];
const filteredCustomerIds = customerIds.filter(n => n <= 3);
console.log('Filtered CustomerIds', filteredCustomerIds);

To do the same thing in C#, you can use the Where LINQ method.

var customerIds = new int[5] { 1, 2, 3, 4, 5};
var filteredCustomerIds = customerIds.Where(n => n <= 3);

foreach (var n in filteredCustomerIds)
{
  Console.WriteLine(n);
}

Tags: #JavaScript #CSharp #DotNet

Discuss... or leave a comment below.

Version 1.2.0 of WriteAs.Net has been released.

This latest version now allows you to enter an API key when initializing a WriteAsClient instance. This API key will allow you to bypass the rate limiting checks on the Write.as API.

Some basic in-memory caching has also been added to the client. You can configure some of the cache settings when initializing a WriteAsClient instance. The new settings are described below:

  • cacheExpirationInSeconds determines how long data will stay in the cache before it expires. The default value for this setting is 300 seconds.
  • cacheSize determines how many objects it can store in the cache. Note that a collection of posts (List<Post>) and a single post each count as 1 item. The default value for this setting is 4.

You can install it via nuget: Install-Package WriteAs.NET -Version 1.2.0

Or via the .NET Core command line interface: dotnet add package WriteAs.NET --version 1.2.0

If you find any bugs or issues with it, please let me know. Thanks and y'all have a good weekend.

Tags: #DotNet #WriteAs #WriteAsNet

Discuss... or leave a comment below.

I'm working on an update to the WriteAs.Net client/wrapper library. In a previous post I talked about adding caching to it before I release a new version. I ran into some road-blocks that derailed me. I ended up pushing it off to the side to focus my time somewhere else.

The two issues that I ran into were: figuring out what the cache key was going to be for the cached object and removing the oldest object in the cache. I now have solutions for those issues.

For the cache keys, I figured I could use the method name plus the parameter values.

For clearing out the oldest cached object, I decided to make use of a generic Queue collection that could accept the cache key values. Then I could just pop-off the oldest value from the Queue and use that to remove the associated object in the cache.

And so anyway, I should have the updated version of the client/wrapper library out soon. I just need to do some more testing on it.

Tags: #DotNet #WriteAs #WriteAsNet

Discuss... or leave a comment below.

When working with a Windows Forms TextBox, yes I was working with a Windows Forms TextBox 😀, and you want to display updates periodically while a long task is running, you can make use of the Application.DoEvents() method.

I think a better solution is to use a BackgroundWorker class for this. But, if you're working on an unimportant utility tool or a throwaway app, using Application.DoEvents() should be good enough.

Tags: #DotNet #WindowsForms

Discuss... or leave a comment below.

When you just want to do a simple WCF service test, you don't need to download third party tools. You can use the WCF Test Client app that usually comes as part of a Visual Studio installation. I keep forgetting where to find it, so I'm writing it down on here to remind myself.

I had Visual Studio 2017 installed on my PC. Here is where I found the WCF Test Client app (WcfTestClient.exe):

C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE

Tags: #WCF #DotNet #VisualStudio

Discuss... or leave a comment below.

A week ago I was running a test and kept running into a WCF BindingConfiguration Error. I would not allow myself to check-in my code unless my tests passed. So, I battled with this error for over an hour.

The binding at system.serviceModel/bindings/basicHttpBinding does not have a configured binding named ‘BasicHttpBinding_IXXXXXX’. This is an invalid value for bindingConfiguration.

It's your basic, run of the mill, WCF BasicHttpBinding configuration error.

I double-checked my test project's app.config file and the relevant client.config files – everything looked right. I know it was a configuration issue. The error message itself made it obvious that it was a configuration issue. But everything looked right. I didn't see any issues with the config files I was looking at.

It must be noted that it was already close to midnight at this point. I was working late that day and decided to push myself by resolving to check-in my changes before the night ends. I have no doubt that being tired and sleepy didn't help.

Anyway, after awhile I realized that the service method that I was trying to test, was running inside another service. So, in addition to the test project’s app.config file, I also needed to double-check that other service’s Web.config file. When I ran into this error earlier that night, I actually updated that other Web.config file. I added the entry it needed just to cover my bases. I didn't know then that it was the Web.config file that was causing the error.

For some reason, possibly due to fatigue and needing sleep, I didn't think about checking that Web.config file again. Out of frustration, I decided to take a break and headed to the kitchen to drink a glass of water. After hanging out in the kitchen for a bit, then listening to some good music, I finally had the bright idea to check this other Web.config file again. And there it was, a typo on the BasicHttpBinding entry I added. Can't believe I didn't check on it sooner.

Lesson learned here is to take a break whenever you're stuck with a problem. Give your mind time to rest. Chances are, your subconscious will kick in and tell you what to try next. And if the problem points to a configuration issue, with WCF, it most likely is. So, check all the config files, again.

Tags: #WCF #CSharp #DotNet

Discuss... or leave a comment below.

I was playing around with Visual Studio 2019 and the open source ASP.NET Core blog engine Miniblog.Core. I cloned it to my local, opened it in Visual Studio 2019 and tried to build the solution. I immediately ran into the error below:

Error CS8630 Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version '8.0' or greater.

So, I looked up the error message and every Stack Overflow page I ended up on, says that I have to set the Language Version in Visual Studio. Okay, so how do I do that? I eventually found this answer, which gives you the steps to get to the Advanced Build Settings dialog box for the project.

  • Right-click YourProject, click Properties
  • Click Build if it's not already selected
  • Change Configuration to All Configurations
  • Click Advanced...
  • Change the language version

When the dialog box showed up however, it didn't give me an option to set the Language Version.

Visual Studio 2019 - Advanced Build Settings

Solution

The solution I ended up with involved manually editing the project file and adding an entry for the Language Version. So, I opened up the Miniblog.Core.csproj file and added <LangVersion>preview</LangVersion> under the <PropertyGroup> settings. It looks like this:

Setting the Language Version manually

Tags: #DotNet #VisualStudio #MiniblogCore

Discuss... or leave a comment below.