Search code examples
c#visual-studioassemblyreference

Trying to import and use a method from different project in a multi-project visual studio solution


I thought this would be really easy to do using just a project reference, but I believe I am missing a step. I am very unfamiliar with working with multi-project solutions and appreciate any help!

I am upgrading a huge solution file to .net 8 from .net framework 4.8, and I am trying to fix this method to use it at multiple places throughout the solution:

using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Linq;
using Microsoft.Extensions.Configuration;
using NLog.LayoutRenderers;
using P1S.Common.Extensions;
using P1S.Common.Interfaces;
using ConfigurationManager = System.Configuration.ConfigurationManager;
using IConfigurationProvider = P1S.Common.Interfaces.IConfigurationProvider;

namespace SepsisServer.Infrastructure.Configuration
{
    public class ConfigurationProvider : IConfigurationProvider, ICustomNameConfigurationProvider
    {
        public string Get(string settingKey)
        {
            var builder = new ConfigurationBuilder();
            builder.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appSettings.json", optional: false, reloadOnChange: true);
            IConfiguration config = builder.Build();


            return config[settingKey];
            //return ConfigurationManager.AppSettings[key];
        }

One place I am trying to use it is here: So I am importing the namespace with a using statement, which stays gray and Get is highlisted red, stating that Get does not exist in current context

using SepsisServer.Infrastructure.Configuration;
...
public TestEnvironment()
{
   //irrelevant code removed for simplicity
           IdentifiConnectionString = Get(Constants.Environment.IdentifiConnectionString);

}

I've added the reference to the correct project already, but that doesnt seem to change anything. How can I help visual studio find the correct method?


Solution

  • I have found the answer and will try to describe the issue as best I am able: The reason visual studio could not find my method was because of issues between static / non-static classes. In order to use the method Get in Configuration Provider I had to create an instance of a class and use that as shown, what @Steve had mentioned in the comment,

    these are the lines that make fix the problem from the file I was working in.

    private static IConfigurationService configurationService;
    ...
    
     var configurationProvider = new ConfigurationProvider();
    configurationService = new ConfigurationService(configurationProvider); 
    ...
    
    ParseOrDefault(configurationProvider.Get(Constants.Environment.UpdateConnectionStringsForLocalEnvironment));