Avatar of satmisha
satmisha
Flag for India asked on

How to combine .NET core Rest API project with worker service project under one solution

Hi Experts,

Is it possible to combine .NET core API project with the worker service project under one solution?

Appreciate if you could share something on that, looking forward to hearing from you.

Satmisha
.NET Programming* Background worker* .net core

Avatar of undefined
Last Comment
satmisha

8/22/2022 - Mon
ste5an

Can you rephrase your question?

Cause the worker service project template was introduced with .NET Core 3.0.
satmisha

ASKER
Hi Ste5an,

I have two separate projects

1. Rest API in .net core.
2. Worker service in .net core.

I want to combine the above two projects in one project rather than having two separate projects. How can I achieve that?

Satmisha
ste5an

So you want to self-host your Rest API.

You need to test this, I never tried it with Core.

Normally you add Microsoft.AspNet.WebApi.SelfHost via nuget and add the necessary hosting code.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
satmisha

ASKER
Hi Ste5an,

I recently made the following by keeping a blank solution and then added the following projects/templates to it:

1. API Template/project having name ASWRestAPIs
2. nUnit Template/project having name  ASWRestAPIs.Tests
3. Worker service project/template having name AWSPollingService
4. nUnit Template/project having the name  AWSPollingService.Tests

Enclosing quick snap-shot, Is it the right approach to move forward? Appreciate your inputs.



ste5an

As already written, I haven't done this with Core. So you need to test it.
satmisha

ASKER
Hi Ste5an, I couldn't get your point. Let me re-phrase myself, I want to ask is my project structure correct, the way I created the project structure by combining the rest API project with the worker service project template along with unit tests projects? 
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
satmisha

ASKER
The other way around, nothing to do with .Net core. If I ask you, Will the following project structure correct where I combine the following projects under one blank project?
1. WindowService
2. RestAPIs
3. NUnit project ( WindowService .Tests)
4. NUnit project  RestAPIs.Tests
ste5an

Nope, it should be

1. Class library for Unit Of Work.
2. Unit test project for class library.
3. Rest API.
4. Self hosting service.

Testing the API or the service would be an integration test.
satmisha

ASKER
Thank you Ste5an, we are now heading in right direction :-).

What are two of these for, apologies for my limited knowledge:
1. Class library for Unit Of Work.
2. Unit test project for the class library.

My understanding is that I should have two nUnit projects in the solution, one meant to test the code written in the API project and another meant to test the code of the worker service.

Can you please suggest?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ste5an

A unit test is for test a unit. Well, sounds odd. Thus an example. Consider a calculator available via rest API.

Then you would implement a separate Calculator class with just the Add, Minus, Mulitply and Divide methods.
The unit test project would only test this calculator class.
Now you implement your calculator as rest API in a separate project. And maybe in your self-hosted worker project.

Testing the rest API is possible, but this is an integration test. It tests whether you have integrated the calculator class correctly into the rest API. While it is a test, it is not a unit test. Same for tests about the worker service.
satmisha

ASKER
Does that mean,

1.  the project "class library for unit of work" (UnitOfWork) will contain the classes to test the both worker service project and API project. It should contain classes from both the projects with methods used in the actual project.
2. However project "unit test project for the class library" (UnitOfWork.Tests)will test the project "class library for the unit of work" as mentioned above?

Sorry for asking too much as my confusion has gone up :-(

Or Can't I have two nunit test project one to test the API and another one to test the worker service?
ASKER CERTIFIED SOLUTION
ste5an

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
satmisha

ASKER
Thank you so much ste5an. I guess it clears a lot to me. I am going to open a new ticket which talks for the deployment of monster, please visit that too.

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
satmisha

ASKER
Thank you, ste5an. As always spot on :-).
satmisha

ASKER
I have created another question " How to deploy API and worker service project on production environment" which is just the extension of this current one and on the deployment part. Appreciate it if you could provide your inputs there as I did not want to ask everything in one ticket.