Thsise Faek
Thsise Faek

Reputation: 311

Initialize ASP.NET Core Web API from another project

I have an application that is structured as follows:

Class B's constructor takes an instance of class A as an argument and works with that instance from then on.

public class B
{
    private A ainstance

    public B(A ainstance)
    {
        this.ainstance = ainstance;
    }
}

class Program
{
    void Main()
    {
        var x = new A();
        var y = new B(x);
    }
}

This structure has worked fine so far. Now however I need to implement an API so that the same operations can be performed on A from a web interface. I chose to use .NET Core for the API, and have implemented most of it. My plan was to add a reference for the API project to the original project and then call the startup method from my Main(), once gain passing the instance of class A as an argument. This doesn't work. Visual Studio refuses to add the reference for reasons unknown.

Is there a way to implement my original idea? If not then ideas for an alternative structure would be greatly appreciated.

Upvotes: 0

Views: 806

Answers (2)

Thsise Faek
Thsise Faek

Reputation: 311

As it turns out, all I needed to do to get it to work was to use create .NET Core Console Application project instead of a normal Windows one.

When I was trying with a normal Console Application project, Visual Studio gave a misleading error: "A reference to [API project name] could not be added. An assembly must have a 'dll' or 'exe' extension in order to be referenced", which led to my confusion. I discovered the actual problem when I tried doing the reverse (referencing the console project from the API one), and VS showed the framework incompatibility.

Upvotes: 1

Nadeem
Nadeem

Reputation: 324

What I understood that your Class A and Class B will remain same except the Class 'Program' logic in your example will go in Asp.NET page. I think you just needed to create a Library project and put the old logic withing this ie: Class A and Class B, compile it, reference this library output in your Asp.Net application, you should be good to go.

Upvotes: 0

Related Questions