Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

How to separate controllers by ports inside selfhosted web service? #8502

Closed
Frank591 opened this issue Sep 26, 2018 · 5 comments
Closed

How to separate controllers by ports inside selfhosted web service? #8502

Frank591 opened this issue Sep 26, 2018 · 5 comments
Assignees
Labels

Comments

@Frank591
Copy link

Description of the problem:

I need to separate controllers by ports inside netcore2.0 selfhosted web service.

My configuration
Version of Microsoft.AspNetCore.Mvc : 2.0.1
Selfhosted service using Kestrel.
OS: Linux

Example:
There are 2 ports(p1 and p2) and 3 controllers(c1, c2, c3). Requirement scheme: c1 processes requests from p1, but c2 and c3 will processes requests from p2.

Is it available to do? If yes, them how can i do that?

PS pls mark this issue as question and sorry for my bad English

@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @Frank591.
@pranavkm, can you please look into this? Is this somethign we support?

@sebastienros
Copy link
Member

If you want the same process (web application) to process all requests, then you need a proxy in front of your application that will handle these. NGinx, HAProxy can do it quite easily.

You can also listen to multiple ports and filter the requests in a middleware. You can use the urls parameter of kestrel to add more input addresses, and I think you can use multiple ports there.

@Frank591
Copy link
Author

Frank591 commented Sep 27, 2018

Thanks for answer, @sebastienros. Variant with load balancers is not available for me. I want to separeate requests inside single app. Can you give some example of middleware, that will sort request between controllers based on input port?

@pranavkm
Copy link
Contributor

You could use an action constraint to do this:

[PortActionConstraint(5000)]
public class HomeController : Controller
{
    ...
}

[AttributeUsage(AttributeTargets.Class)]
public class PortActionConstraint : ActionMethodSelectorAttribute
{
    public PortActionConstraint(int port)
    {
        Port = port;
    }

    public int Port { get; }

    public override bool IsValidForRequest(RouteContext routeContext, ActionDescriptor action)
    {
        var port = routeContext.HttpContext.Request.Host.Port;
        return Port == port;
    }
}

@Frank591
Copy link
Author

Frank591 commented Sep 28, 2018

@pranavkm , thanks for answer! Will try to use that.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants