Today, Microsoft announced .NET 6 Preview 1. It wasn’t a surprise, because they’ve been developing in the open for a long time as we saw in 2020, during the .NET 5 rollout. They’ve announced a new site, Themes of .NET, that anyone can visit to learn what others are thinking and what the priority and direction is for .NET.
What I’m most excited about are cross-platform UI and Apple Silicon (M1) support. The “Apple Silicon” they refer to is the new MacBook Pro with the new Apple M1 chip. I bought one recently and am currently doing 95% of my work on it. The only reason I’m not 100% is that I’ve left a few items on a Windows machine for unforeseen use cases that I might encounter. So far, so good, and the M1 is a very nice machine to work on.
In addition to M1 support, I’m interested in cross-platform UI features. This was something that wasn’t ready in the .NET 5 timeframe. They’ll do this via a project codenamed “Maui”, which is an evolution of the Xamarin Framework. In addition to Android and iOS apps, we’ll be able to build desktop UI for Mac, Linux, and Windows. From what I gather, they’re looking at WinUI 3 support too, which is the next generation of desktop UI that’s evolving under a separate, but coordinated, project.
So, I’m going to get brave and install .NET 6 Preview 1 with Visual Studio for Mac (VS). As I implied earlier, I have cloud backups, Git, and a Windows machine standing by if I get in trouble.
After opening VS, select Visual Studio | About Visual Studio to verify the version:
The announcement say’s 8.9, and this is a .1 version below that. To fix this, select Visual Studio | Check for Updates to see the Visual Studio Update window:
Select Preview, in the Update Channel, and observe that the proper version of Visual Studio for Mac 188.8.131.523 appears. By the time you read this, the version might have incremented because the .NET team is doing fast releases.
Click Switch Channel and wait for updates to download. After everything downloads, the Close button will change to Restart and Install Updates, which you should click. After that, you’ll be ready to use VS with .NET 6 Preview 1. You can verify by selecting Visual Studio | About Visual Studio and observe that the version is now 8.9.
Next, I installed the .NET 6 Preview 1 SDK. There’s a link to click for downloading for macOS installer:
No surprises here, just run the installer and you’ll have support for the .NET, ASP.NET Core, and .NET Desktop runtimes.
After installing new components, extensions, etc., I have a habit of closing and re-opening VS to avoid any strangeness and let it pick up the newly installed bits. After VS opens, click New on the opening dialog:
BTW, I’m using VS for the new book I’m writing, C# Cookbook, which you can visit now to see early release chapters (at least until publication).
You’ll see the New Project window, where you can navigate to Web and Console | App, select Console Application, and click Next:
This will bring you to the Configure page. Although the dropdown might default to .NET Core 3.1, you can select it and observe that .NET 6.0 is now an option. Select .NET 6.0 and click Next:
The next window is the typical project setup, where you provide a project name, location, and other details. I used HelloTwitter, because it’s using LINQ to Twitter to tweet a message. I also checked Use git for version control because I checked it into GitHub as blog-hellotwitter so you can download and try yourself. After configuring, click the Create button:
That creates an initial project shell. Next, go to the Solution window, select Dependencies, open the context menu, and select Manage NuGet Packages:
In the Manage NuGet Packages window, search for “linqtotwitter”, make sure you’re getting the latest version, and click Add Package:
Once you add the package, you’ll be able to write code using LINQ to Twitter. However, you’ll need to Apply for a Twitter API Developer account and create a Twitter App, via the Twitter Developer Portal first. The process isn’t difficult and they just ask you what you want the app to do. Once you’ve done that, replace the default VS code with the following:
Remember to replace the ConsumerKey and ConsumerSecret values with your Twitter App keys (once you have an approved app).
Most of this code is setting up the authorization, PinAuthorizer, in GetTwitterContext.
- CredentialStore holds your keys
- GoToTwitterAuthorization navigates to the Twitter authorization page
- After authorizing, Twitter gives you a PIN number that you can copy and paste in response to the prompt, specified in GetPin.
Calling AuthorizeAsync sets off this process. Afterwards, if you debug and examine auth.CredentialStore, you’ll see additional tokens that LINQ to Twitter can use every time it interacts with the Twitter API. There’s also a Screen Name and User ID of the Twitter user (yourself in this case) who performed the authorization. Once you have a TwitterContext, initialized with the authorizer, you can use LINQ to Twitter queries and commands to communicate with Twitter. The example uses TweetAsync, in Main, to er… tweet.
Now, you’ve set up .NET 6 Preview 1 via both Visual Studio for Mac and installed the .NET 6 Preview 1 SDK. You created a new Console application with a reference to LINQ to Twitter. Finally, you wrote some LINQ to Twitter code that tweets a message referencing this blog post. This shows that the .NET 6 Preview 1 is working (as far as a beta/preview could in this case) and that LINQ to Twitter works with .NET 6 Preview 1.