Peter
Peter

Reputation: 119

.NET Framework to .NET 6

the code below is from random source and is in .Net Framework and I am trying to convert it to .Net 6. As I am new to .Net 6, I'm unable to figure out which statements I should reuse while migration. I tried using .Net 6 style for getting my settings key and values but however I get objt ref is required for non-static exception.

 public abstract class ServiceBase : IDisposable
    {
        //newly added
        //start
        private readonly PrintSettings _PrintSettings;
        public ServiceBase(IOptions<PrintSettings> options)
        {
            _PrintSettings = options.Value;
        }
        //end


       // Problem is here, object ref required for non static.
        public ServiceBase() : this(_PrintSettings.DBConnection()) { }

        protected ServiceBase(string instance)
        {
            this.proxy = new Proxy(instance);
        }

        public void BeginTran()
        {
            this.proxy.GetContext().BeginTran();
        }

Upvotes: 1

Views: 823

Answers (1)

Nikolai
Nikolai

Reputation: 1173

All modern C# platforms implement .Net Standard specification. To understand if you can just copy-paste code from one platform to another you should check .Net Standard versions supported by these platforms. .Net Faramework implements it since version 4.5. .Net 6 supports all .Net standard versions. It means, if .Net Framework version is higher then 4.5 you can easely copy-paste code from its project to .Net 6 project.

You can read more about it here https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-1-0

But keep in mind, that if platforms don't have common .Net Standard implementation, it dosn't mean you can't copy-paste code from one to another. It just mean there are some functions thats differ for these platforms. Generally you can copy a code and check it for compilation erros. If there are no errors, then feel free to use this code.

Speaking about the object ref error. You trying to use _PrintSettings field as a constructor argument, but you can't use fields before your object is being constructed. You should think better about lifecycle of ServiceBase instancies and determine what exactly do you need to create these objects. And pass this dependencies to the constructor.

Upvotes: 1

Related Questions