• About
  • Privacy Policy
  • Utilities
Darchuk.NET

C#

C# Setting Socket Keep-Alive

January 4, 2019 by Robert 4 Comments

TCP is a good choice for communication if you want to know that your message got to where it was going, as opposed to UDP where the communication is fire-and-forget. When a message is sent, the recipient sends an acknowledgement that the message arrived successfully.  If you don’t get an acknowledgement, you have a pretty … [Read more…]

Posted in: C# Tagged: C#, KeepAlive, Networking, Socket, TCP, TcpClient

CORS in ASP .NET Core 2.2

December 21, 2018 by Robert Leave a Comment

Recently my team upgraded to .NET Core 2.2.  We have an API that needs to be accessed from an Angular project living in a different domain, so we had CORS enabled this way: Disclaimer: this is an intranet application – so it is only accessible inside of a corporate firewall, making it more acceptable to … [Read more…]

Posted in: .NET Core, ASP.NET, C#, Security, Web API, Web Development Tagged: .NET Core, ASP.NET, C#, CORS, NETCORE, Security, WebAPI

Adding a CancellationToken to TcpListener.AcceptTcpClientAsync

November 9, 2018 by Robert 7 Comments

A TcpListener opens a socket using TCP to listen for incoming connection requests.  You can use AcceptTcpClientAsync to asynchronously get a TcpClient object, which you can then use to send and receive messages on the connection. AcceptTcpClientAsync Waits forever to get a connection.  This is pretty much what you want most of the time… after … [Read more…]

Posted in: .NET Core, Asynchronous, C# Tagged: async, await, C#, Cancel, CancellationToken, TcpListener

CancellationTokens

November 2, 2018 by Robert 1 Comment

In C# asynchronous programming, a CancellationToken allows you to stop a Task.  This is especially useful if you have many Tasks running and want to gracefully shut down the program, or if you want to add a timeout to a Task. via GIPHY Creation CancellationTokens are created from a CancellationTokenSource. via GIPHY Usage The token … [Read more…]

Posted in: .NET Core, Asynchronous, C#, Concurrency Tagged: async, await, C#, CancellationToken, CancellationTokenSource, Concurrency, OperationCanceledException, TaskCanceledException

C# and Mongo–FindOneAndUpdateAsync

August 31, 2018 by Robert Leave a Comment

Recently I’ve been working on adding SignalR to a web application, and that means broadcasting records to clients after they’ve been updated.  I could make different methods for each update type and react accordingly, but I’m pretty lazy so I decided to just make one “update” method that receives the entire record (and replaces it … [Read more…]

Posted in: .NET Core, Asynchronous, C#, Mongo Tagged: C#, Database, Document, FindOneAndUpdateAsync, Mongo, Record, UpdateOneAsync

C# Unit Testing object equality without implementing .Equals

August 24, 2018 by Robert Leave a Comment

Recently I was trying to write a unit test testing to see if one object was equal to another.  The objects themselves were simple, just a collection of built-in types, so it should be easy to test. .Equals However, since I didn’t implement .Equals, C# doesn’t natively know how to check if the objects are … [Read more…]

Posted in: C#, Reflection, Unit Testing Tagged: C#, Csharp, Equals, PixieDust, Reflection, Testing, UnitTesting

Throttling execution through BlockingCollection

July 20, 2018 by Robert Leave a Comment

BlockingCollection is essentially a queue that provides a thread-safe way to consume items.  You can put it into a loop and consume the items one at a time, throttling execution and potentially preventing your system from being overwhelmed. I use these all the time, but most recently I built it in to a metric collection … [Read more…]

Posted in: C#, Concurrency Tagged: Blocking, BlockingCollection, C#, Concurrency, Execution, Optimization, Process, Queue, Throttle

Benchmarking C# Mongo–ReplaceOne vs. AddToSetEach

July 13, 2018 by Robert Leave a Comment

Recently I was looking at a MongoDB service and wondered if doing ReplaceOne was really the right call. My document was pretty simple – some identifying information that doesn’t change, and an array containing informational state that grows (never updates) as my program runs.  I set up the documents to update to the database every … [Read more…]

Posted in: Benchmark, C#, Mongo Tagged: AddToSetEach, Benchmark, C#, Database, InsertOne, Mongo, Performance, Update

Adding projects to the C# Interactive Window

July 6, 2018 by Robert Leave a Comment

The C# Interactive window allows you to use a C# REPL that can be pretty useful for testing something out quickly.  I’ve used it a bunch of times for formatting a DateTime to a string, because I can never quite remember which combination of characters results in what I want. In Visual Studio, you can … [Read more…]

Posted in: .NET Core, C#, Unit Testing Tagged: C#, C# Interactive, REPL, Testing, Visual Studio

Thread-Safe Atomic Operations

June 29, 2018 by Robert Leave a Comment

There’s been more than a couple times where I’ve wanted to use the Parallel class to take a collection and rip through it with multiple threads, but I’ve struggled with how to log the progress (especially the count of objects processed) easily. Interlocked .NET actually provides the solution in a static class called Interlocked, located … [Read more…]

Posted in: .NET Core, ASP.NET, Asynchronous, C#, Concurrency Tagged: .NET, C#, Concurrency, Thread, Thread Safe, threading
« Previous 1 … 3 4 5 … 7 Next »

Recent Posts

  • Allowing trailing JSON commas in ASP.NET Core 3
  • YADNC3JSG–Yet Another .NET Core 3.0 JSON Serializer Gotcha
  • Logging in a .NET Core 3 SignalR client
  • .NET Core 3 unit testing project sdk
  • .NET Core 3.0 Upgrade–New JSON Serializer Gotchas

Recent Comments

  1. Robert on C# Setting Socket Keep-Alive
  2. Oliver Schramm on C# Setting Socket Keep-Alive
  3. John Anderson on Waiting for a keypress asynchronously in a C# console app
  4. Ronald Garlit on .NET Core 3 unit testing project sdk
  5. Ronald Garlit on .NET Core 3 unit testing project sdk

Copyright © 2025 Darchuk.NET.

Omega WordPress Theme by ThemeHall