• Simplify working with parallel tasks in C# (updated)


      No doubts that async/await pattern has significantly simplified working with asynchronous operations in C#. However, this simplification relates only to the situation when asynchronous operations are executed consequently. If we need to execute several asynchronous operations simultaneously (e.g. we need to call several micro-services) then we do not have many built-in capabilities and most probably Task.WhenAll will be used:

      Task<SomeType1> someAsyncOp1 = SomeAsyncOperation1();
      Task<SomeType2> someAsyncOp2 = SomeAsyncOperation2();
      Task<SomeType3> someAsyncOp3 = SomeAsyncOperation3();
      Task<SomeType4> someAsyncOp4 = SomeAsyncOperation4();
      await Task.WhenAll(someAsyncOp1, someAsyncOp2, someAsyncOp4);
      var result = new SomeContainer(
           someAsyncOp1.Result,someAsyncOp2.Result,someAsyncOp3.Result, someAsyncOp4.Result);

      This is a working solution, but it is quite verbose and not very reliable (you can forget to add a new task to “WhenAll”). I would prefer something like that instead:

      var result =  await 
          from r1 in SomeAsyncOperation1()
          from r2 in SomeAsyncOperation2()
          from r3 in SomeAsyncOperation3()
          from r4 in SomeAsyncOperation4()
          select new SomeContainer(r1, r2, r3, r4);

      Further I will tell you what is necessary for this construction to work...

      Read more →
      • +11
      • 15.8k
      • 4
    • Dynamically generating robots.txt for ASP.NET Core sites based on environment

        I'm putting part of older WebForms portions of my site that still run on bare metal to ASP.NET Core and Azure App Services, and while I'm doing that I realized that I want to make sure my staging sites don't get indexed by Google/Bing.

        I already have a robots.txt, but I want one that's specific to production and others that are specific to development or staging. I thought about a number of ways to solve this. I could have a static robots.txt and another robots-staging.txt and conditionally copy one over the other during my Azure DevOps CI/CD pipeline.

        Then I realized the simplest possible thing would be to just make robots.txt be dynamic. I thought about writing custom middleware but that sounded like a hassle and more code that needed. I wanted to see just how simple this could be.

        Read more →
      • SQL Index Manager – a long story about SQL Server, grave digging and index maintenance

          Every now and then we create our own problems with our own hands… with our vision of the world… with our inaction… with our laziness… and with our fears. As a result, it seems to become very convenient to swim in the public flow of sewage patterns… because it is warm and fun, and the rest does not matter – we can smell round. But after a fail comes the realization of the simple truth – instead of generating an endless stream of causes, self-pity and self-justification, it is enough just to do what you consider the most important for yourself. This will be the starting point for your new reality.

          For me, the written below is just such a starting point. The way is expected to be lingering…
          Let's go?
        • Announcing .NET Core 3.0 Preview 6

            Today, we are announcing .NET Core 3.0 Preview 6. It includes updates for compiling assemblies for improved startup, optimizing applications for size with linker and EventPipe improvements. We’ve also released new Docker images for Alpine on ARM64.

            Read more →
          • .NET: Tools for working with multi-threading and asynchrony – Part 1

              I have originally posted this article in CodingSight blog
              The second part of the article is available here

              The need to do things in an asynchronous way – that is, dividing big tasks between multiple working units – was present long before the appearance of computers. However, when they did appear, this need became even more obvious. It is now 2019, and I’m writing this article on a laptop powered by an 8-core Intel Core CPU which, in addition to this, is simultaneously working on hundreds of processes, with the number of threads being even larger. Next to me, there lies a slightly outdated smartphone which I bought a couple of years ago – and it also houses an 8-core processor. Specialized web resources contain a wide variety of articles praising this year’s flagship smartphones equipped with 16-core CPUs. For less then $20 per hour, MS Azure can give you access to a 128-core virtual machine with 2 TB RAM. But, unfortunately, you cannot get the most out of this power unless you know how to control interaction between threads.
              Read more →
            • Porting desktop apps to .NET Core

                Since I’ve been working with the community on porting desktop applications from .NET Framework to .NET Core, I’ve noticed that there are two camps of folks: some want a very simple and short list of instructions to get their apps ported to .NET Core while others prefer a more principled approach with more background information. Instead of writing up a “Swiss Army knife”-document, we are going to publish two blog posts, one for each camp:

                • This post is the simple case. It’s focused on simple instructions and smaller applications and is the easiest way to move your app to .NET Core.
                • We will publish another post for more complicated cases. This post will focus more on non-trivial applications, such WPF application with dependencies on WCF and third-party UI packages.

                If you prefer watching videos instead of reading, here is the video where I do everything that is described below.

                Read more →
              • Ads
                AdBlock has stolen the banner, but banners are not teeth — they will be back

              • Nullable Reference types in C# 8.0 and static analysis

                  Picture 9

                  It's not a secret that Microsoft has been working on the 8-th version of C# language for quite a while. The new language version (C# 8.0) is already available in the recent release of Visual Studio 2019, but it's still in beta. This new version is going to have a few features implemented in a somewhat non-obvious, or rather unexpected, way. Nullable Reference types are one of them. This feature is announced as a means to fight Null Reference Exceptions (NRE).
                  Read more →
                • The architecture of an exceptional situation: pt.2 of 4

                    I guess one of the most important issues in this topic is building an exception handling architecture in your application. This is interesting for many reasons. And the main reason, I think, is an apparent simplicity, which you don’t always know what to do with. All the basic constructs such as IEnumerable, IDisposable, IObservable, etc. have this property and use it everywhere. On the one hand, their simplicity tempts to use these constructs in different situations. On the other hand, they are full of traps which you might not get out. It is possible that looking at the amount of information we will cover you’ve got a question: what is so special about exceptional situations?

                    However, to make conclusions about building the architecture of exception classes we should learn some details about their classification. Because before building a system of types that would be clear for the user of code, a programmer should determine when to choose the type of error and when to catch or skip exceptions. So, let’s classify the exceptional situations (not the types of exceptions) based on various features.

                    Read more →
                  • Let's help QueryProvider deal with interpolated strings

                    • Translation

                    Specifics of QueryProvider

                    QueryProvider can’t deal with this:

                    var result = _context.Humans
                                          .Select(x => $"Name: {x.Name}  Age: {x.Age}")
                                          .Where(x => x != "")

                    It can’t deal with any sentence using an interpolated string, but it’ll easily deal with this:

                    var result = _context.Humans
                                          .Select(x => "Name " +  x.Name + " Age " + x.Age)
                                          .Where(x => x != "")

                    The most painful thing is to fix bugs after turning on ClientEvaluation (exception for client-side calculation), since all Automapper profiles should be strictly analyzed for interpolation. Let’s find out what’s what and propose our solution to the problem.

                    Read more →
                  • Support of Visual Studio 2019 in PVS-Studio

                      Support of Visual Studio 2019 in PVS-Studio affected a number of components: the plugin itself, the command-line analyzer, the cores of the C++ and C# analyzers, and a few utilities. In this article, I will briefly explain what problems we encountered when implementing support of the IDE and how we addressed them.
                      Read more →
                    • Exceptional situations: part 1 of 4


                        It’s time to talk about exceptions or, rather, exceptional situations. Before we start, let’s look at the definition. What is an exceptional situation?

                        This is a situation that makes the execution of current or subsequent code incorrect. I mean different from how it was designed or intended. Such a situation compromises the integrity of an application or its part, e.g. an object. It brings the application into an extraordinary or exceptional state.

                        But why do we need to define this terminology? Because it will keep us in some boundaries. If we don’t follow the terminology, we can get too far from a designed concept which may result in many ambiguous situations. Let’s see some practical examples:

                         struct Number
                             public static Number Parse(string source)
                                 // ...
                                     throw new ParsingException();
                                 // ...
                             public static bool TryParse(string source, out Number result)
                                // ..
                                return parsed;

                        This example seems a little strange, and it is for a reason. I made this code slightly artificial to show the importance of problems appearing in it. First, let’s look at the Parse method. Why should it throw an exception?

                        Read more →
                      • Should array length be stored into a local variable in C#?

                        • Translation
                        I notice that people often use construction like this:

                        var length = array.Length;
                        for (int i = 0; i < length; i++) {
                            //do smth

                        They think that having a call to the Array.Length on each iteration will make CLR to take more time to execute the code. To avoid it they store the length value in a local variable.
                        Let’s find out (once and for all !) if this is a viable thing or using a temporary variable is a waste of time.
                        Read more →
                      • DynamicData: Dynamic Collections, the MVVM Architecture, and Reactive Extensions

                        • Translation

                        February 2019 marked the release of ReactiveUI 9 — the cross-platform framework for building GUI applications on the Microsoft .NET platform. ReactiveUI is a tool for tight integration of reactive extensions with the MVVM design pattern. You could familiarize yourself with the framework via a series of videos or the welcome page of the documentation. The ReactiveUI 9 update includes numerous fixes and improvements, but probably the most crucial and interesting one is integration with the DynamicData framework, allowing you to work with dynamic collections in Reactive fashion. Let’s find out what we can use DynamicData for and how this powerful reactive framework works under the hood!

                        Read more →
                      • Build Visual Studio templates with tags, for efficient user search and grouping

                          Visual Studio’s project templates enable you, the developer, to create multiple similar projects more efficiently by defining a common set of starter files. The project templates can be fully customized to meet the needs of a development team, or a group, and can be published to the Visual Studio Marketplace for others to download and use too! Once published, developers can install and access the template through Visual Studio’s New Project Dialog.

                          The newly designed New Project Dialog for Visual Studio 2019 was built to help developers get to their code faster. Using a search and filter focused experience, we are aiming to provide better discoverability for specific templates to start your application development


                          Read more →
                        • Blazor now in official preview

                            With this newest Blazor release we’re pleased to announce that Blazor is now in official preview! Blazor is no longer experimental and we are committing to ship it as a supported web UI framework including support for running client-side in the browser on WebAssembly.

                            A little over a year ago we started the Blazor experimental project with the goal of building a client web UI framework based on .NET and WebAssembly. At the time Blazor was little more than a prototype and there were lots of open questions about the viability of running .NET in the browser. Since then we’ve shipped nine experimental Blazor releases addressing a variety of concerns including component model, data binding, event handling, routing, layouts, app size, hosting models, debugging, and tooling. We’re now at the point where we think Blazor is ready to take its next step.

                            Blazor icon
                            Read more →