Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Net Core: how to separate controllers by ports?

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

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.

Any ideas about how can i do that?

like image 428
Frank59 Avatar asked Nov 07 '22 01:11

Frank59


1 Answers

Got answer on GitHub https://github.com/aspnet/Mvc/issues/8502

[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)
    {
        //external port
        var externalPort = routeContext.HttpContext.Request.Host.Port;
        //local port 
        var localPort = routeContext.HttpContext.Connection.LocalPort;
        //write here your custom logic. for example  
        return Port == localPort ;
    }
}
like image 57
Frank59 Avatar answered Nov 15 '22 08:11

Frank59