0

I have an application that is structured as follows:

  • Class A with certain methods exposed
  • Class B that basically acts as the command line interface, calling the exposed methods of an instance of class A

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I simplified a lot in the question. A and B currently exist in different projects, and I would like to keep them separate unless there is no other option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.