Search code examples
c#.netvb.netreferencenuget

Public static class and methods not found in NuGet package


I'm running into an odd issue where I've developed a NuGet package. Because of the architecture of the legacy software I'm trying to write new functionality for, this is in .NET Framework 4.6.1 (I know, I know. We're working toward upgrading). The NuGet package is written in C#, while the app that will reference the NuGet package is in VB .NET that is in .NET Framework 4.6.1. The legacy app leverages three total NuGet packages I've written for this solution, but they are not appearing as errors.

The issue is in the title for this question. The public static class and methods in the NuGet package I've written are being marked as "not declared. It may be inaccessible due to its protection level." Because of this error, I obviously cannot build.

I've tried the following:

  1. Removing BIN/OBJ folders
  2. Rebuilding/Cleaning and Building the solutions
  3. Incrementing the NuGet version to verify old references are not held onto
  4. Adding NuGet package references that support my NuGet package (Json supporting packages, mainly)
  5. Removing and readding the NuGet package references to my packages

It may also be worth noting that the branch I'm on for the legacy application builds without changes, it also builds with my NuGet packages referenced. It doesn't build as soon as I added the calls into the NuGet package. I'm wondering if it's a an issue with the Wait() function, but I can't seem to derive the actual issue at hand. The errors that come up in the output during build just say the same 'not found/inaccessible' language.

Code samples below:

Within the NuGet package, I have the following code.

public static class ClassInNugetPackage
{
    public static async Task Initialize(string customerNumber, int locationId)
    {
        await SystemState.Instance.Initialize(customerNumber, locationId);
    }

    public static async Task SetApiUrl(string url)
    {
        await SystemState.Instance.SetApiUrl(url);
    }
}

The legacy application will call this in a form constructor like this:

Public Sub New()
    InitializeComponent()

    Dim customerNumber = "170"
    Dim customerLocationId = 2140
    NamespaceInNugetPackage.ClassInNugetPackage.SetApiUrl("https://localhost:7012").Wait()
    NamespaceInNugetPackage.ClassInNugetPackage.Initialize(customerNumber, customerLocationId).Wait()

    If ClassInNugetPackage.BooleanValue.IsEnabled Then
        MsgBox("Yes!")
    End If
    
    ...
End Sub

Solution

  • First off, I did not come to this resolution by myself. The comments of the community really steered me in a better direction, and led to this resolution. The codebase that I'm attempted to add functionality to is older and has a lot of references to older versions of NuGet packages (again, we're working on upgrading them).

    I'm able to build now because I did the following:

    1. Made sure I was on a branch of the core application git repository
    2. Used the .NET Upgrade Assistant to move to the .NET SDK format
    3. Attempted to build (this changed the output message that Visual Studio gave me, stating that it detected two package downgrades).
    4. Reverted the legacy application via git
    5. Rebuilt the legacy application to make sure I was in a successful build state.
    6. Downgraded Microsoft.Bcl.AsyncInterfaces and System.Threading.Tasks.Extensions to the versions found in the legacy application
    7. Incremented my NuGet package assembly information
    8. Ran NuGet pack in my nuget package directory via Windows Terminal
    9. Referenced the new NuGet Package version in the legacy app
    10. Built the legacy application successfully
    11. Added my code
    12. Built the legacy app with the code that references the NuGet packages successfully.

    Thanks all for your help, and especially to Craig for giving the direction that led to the packageref and packages.config. This made me think of leveraging the Upgrade Assistant to see if the output would differ, and it did. Before the Upgrade Assistant migration to .NET SDK, I saw a singular error for each instance of calling my NuGet package, which was of the same verbiage (not found or not accessible), which is what confused me.

    Thank you all for your consideration, input, and help. This got me to the next step!