Saturday, April 18, 2020

Upgrade to .NET Core 3.1 - Could not load file or assembly Microsoft.IntelliTrace.Core

The last error I came across was this one, "Could not load file or assembly Microsoft.IntelliTrace.Core". This happened while debugging a test.

The initial fix was upgrading the referenced nuget packages to their latest version. However, after this, I still got the error. The solution was to update Visual Studio 2019 to it's latest version. Not very intuitive, but it worked.

Friday, April 17, 2020

Upgrade to .NET Core 3.1 - Using StructureMap in .NET 3.1

In short, you should stop using StructureMap. It's no longer supported. Instead, use Lamar, and only have to make minimal changes. It's written by the same guy who wrote StructureMap.

In your Program.cs:

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseLamar()
                .ConfigureWebHostDefaults(webBuilder =>
                {

In your Startup.cs:

        public void ConfigureContainer(ServiceRegistry services)
        {
             ...
services.Scan(scan =>
             {
...

Wednesday, April 15, 2020

Upgrade to .NET Core 3.1 - Could not find an IRouter associated with the ActionContext. If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection and use it to create a UrlHelper

The error itself exposes the solution. You need to stop using: 

            return new UrlHelper(helper.ViewContext).DisplayLink(action, linkText, returnLink);

And instead use (notice I'm not using DI, but you should use DI to retrieve IUrlHelperFactory):

            var urlHelperFactory = ServiceLocator.Current.GetInstance<IUrlHelperFactory>();
            return urlHelperFactory.GetUrlHelper(helper.ViewContext).DisplayLink(action, linkText, returnLink);

Tuesday, April 14, 2020

Upgrade to .NET Core 3.1 - Migration warning CS0618 'RazorViewAttribute' is obsolete

This error is simple, you can even find the answer is SO. It's caused by one of your nugets, you only need to update it to the latest version. Finding the offending nuget is another story. Hopefully you don't have too many nugets to update.

Sunday, April 12, 2020

Upgrade to .NET Core 3.1

Last week I had the task of upgrading a project written in .NET Core 2.1 to .NET Core 3.1. I'll be posting solutions to the errors I came across. But first, I'll show important sections of my Startup.cs and Program.cs:

I'll start with Program.cs:


        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();
            host.Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseLamar()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                }).ConfigureLogging(builder =>
                {                    
                    ...

                    builder.AddApplicationInsights();

                });

On to the file Startup.cs

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureContainer(ServiceRegistry services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
...
            });

            services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
                .AddAzureAD(options => Con

            services.AddMediatR();
            services.AddAutoMapper();
            services.AddSession();

...
            services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();

                options.Filters.Add(typeof(AzureAuthorizationFilter));
...
                options.UseModelBinding();
            });
            services.AddRazorPages().SetCompatibilityVersion(CompatibilityVersion.Latest).AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
                .AddTypedRouting()
                .AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
            services.AddAuthorization();
            services.Configure<RazorViewEngineOptions>(options => {      
...
            });

            services.AddSignalR();

            services.Scan(scan =>
            {
                scan.TheCallingAssembly();
...
                scan.WithDefaultConventions();
            });
            services.For<IPrincipal>().Use(x => Thread.CurrentPrincipal);
...
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();
            app.UseSession();
            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "default",
                    "{controller=Home}/{action=Index}/{id?}");

                endpoints.MapHub<NotificationHub>("/pushHub");
            });
        }

    }