Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running .net core project from another project

I have a solution that has 3 projects. Project2 needs Project1 to be running in order to function normally (Project2 call some Apis in Project1).

Solution
|-- Project1
|   |-- Program.cs
|   |-- Startup.cs
|-- Project2
|   |-- Program.cs
|   |-- Startup.cs
|-- IntegrationTestProject

I can run the 2 projects together by changing the properties of the solution as shown in the below image and it worked fine:

enter image description here

What I want is a way to run Project1 from Project2 by code (from startup or from SomeService in Project2) and it will be great if I can also stop Project1 when I stop running Project2.

I tried this code and it didn't start Project1:

  string[] x = { };
  Project1.Program.Main(x);

Edit:

IntegrationTestProject is used to test Project2 Apis. So I created a new TestServer() from Project2.

When I run the integration test, some APIs succeeded (the one that only depends on Project2) and other failed (the one that calls Apis from Project1) because the server (Project1) is not reachable (it is down when test run).

If I run the integration test while running Project1, all test succeeded.

I tried to run a test instance of Project1 using new TestServer() and pass it to Project2. It worked for some Apis and I faced another problem that seemed not to be solved until I run Project1 and not a test instance of it.

So my only solution is to run Project1 when I start the integration test.

I made a .bat file that runs Project1 (by dotnet run) and then starts the integration test (by dotnet test) and it worked fine. But the problem with it is when running the integration test from Visual studio it didn't run Project1.

Edit 2:

This is a sample project that demonstrates the problem I faced: https://github.com/bilalseh/SampleSolution

1- If you run Project2 alone and call (Get: "/api/home") it will return a result. If you call (Get: "/api/home/5") it will have a problem because this Api depends on Project1.

2- If you open visual studio and run the tests (2 tests) or run these tests from the command line by "dotnet test" 1 test will pass (testing: "/api/home") and one will fail (testing: "/api/home/5").

3- If you run Project1 and then start the tests both will pass.

4- I made a .bat file named "run test with temp server.bat" in the integration test folder. This file will run Project1 and then starts the tests and then stops Project1. If you run this file both tests will pass.

What I want is to find a way to starts Project1 either from Project2 or when I start the integration test.

like image 342
Bilal Sehwail Avatar asked Jun 27 '19 08:06

Bilal Sehwail


2 Answers

You either add reference to the assembly of the project you want to start.

The other way would be with Process.Start() and put the path of your executable to run it

On the project that wish to open other project:

Right click References-> Add references -> Solution -> check the project you want to create -> OK

You might need to add extra references if the compiler ask you to (PresentationFramework, system.xaml ...) Or Make Project 2 Program.cs Public by adding the word "public" in front of "class Program".

And Add this In Project 1 button...

private void button_Click(object sender, EventArgs e)
{
    Project_2 pb = new Project_2()
    Process.Start(pb.returnPath() + \\"Project_2.exe");
}

EDIT : As I search in stackoverflow, your question similar as this one

like image 194
Syafiqur__ Avatar answered Oct 12 '22 02:10

Syafiqur__


It looks like you want to go the microservice way. So, couple of principles:

1) you should be able to debug the services one at the time - if you debug service A that depends on service B, make a mock service B. Then test/debug service B on its own - if all parts work, the whole machine should work

2) if you want to RUN (not just debug), use Docker/Kubernetes for that. They will manage any restarts, crashes, updates etc. If you want to test the full system, still run docker deploy, then run integration tests on that

Each microservice is it's own thing, without any real dependency on others, so no shared databases, no crossrefences via DLL etc.

If you feel like that's overkill, don't split your app into microservices, make one big service - it will be easier to manage. At least until certain point when granurality shows its goodness

like image 32
Krzysztof Skowronek Avatar answered Oct 12 '22 03:10

Krzysztof Skowronek