Link to home
Create AccountLog in
Avatar of satmisha
satmishaFlag 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
Avatar of ste5an
ste5an
Flag of Germany image

Can you rephrase your question?

Cause the worker service project template was introduced with .NET Core 3.0.
Avatar of 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
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.
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.

User generated image

As already written, I haven't done this with Core. So you need to test it.
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? 
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
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.
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?

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.
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
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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.

Thank you, ste5an. As always spot on :-).
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.