最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Consuming self HTTP API or directly calling the handler? - Stack Overflow

programmeradmin1浏览0评论

I have two AspNetCore projects, A and B. They both must run a few pieces of common business logic.

I wish to centralize that common logic into project A. It's easy to expose it on some endpoints and have project B send an HTTP request to project A. I then wrap the requests into a service class that project A can use to call the common logic - think Typed HTTP Client. I am faced with a decision:

  • should I just have project A consume its own HTTP endpoints? Pros: easy to develop; cons: overhead because of the HTTP roundabout.
  • should I fork in the service class, so that it either sends an HTTP call (used by project B and/or other projects, when an HTTP base URL is passed) or directly calls a method (that is, the HTTP request handler; used by project A, when no HTTP base URL is passed)? Pros: avoids unnecessary HTTP call to self; cons: more calls to maintain, less guarantee that it's doing the same thing.

I'm especially concerned about this last con. If project A ever changes the handler to endpoint E from ActionE to AlternativeActionE, the service class doesn't get to know it and goes on calling ActionE, which means it does different things when it's used in project A than when it's used in project B (or other projects).

Is there a way to have the best of both worlds, i.e., a guarantee that I'm actually reusing the logic without the overhead of self-HTTP-consumption?

I have two AspNetCore projects, A and B. They both must run a few pieces of common business logic.

I wish to centralize that common logic into project A. It's easy to expose it on some endpoints and have project B send an HTTP request to project A. I then wrap the requests into a service class that project A can use to call the common logic - think Typed HTTP Client. I am faced with a decision:

  • should I just have project A consume its own HTTP endpoints? Pros: easy to develop; cons: overhead because of the HTTP roundabout.
  • should I fork in the service class, so that it either sends an HTTP call (used by project B and/or other projects, when an HTTP base URL is passed) or directly calls a method (that is, the HTTP request handler; used by project A, when no HTTP base URL is passed)? Pros: avoids unnecessary HTTP call to self; cons: more calls to maintain, less guarantee that it's doing the same thing.

I'm especially concerned about this last con. If project A ever changes the handler to endpoint E from ActionE to AlternativeActionE, the service class doesn't get to know it and goes on calling ActionE, which means it does different things when it's used in project A than when it's used in project B (or other projects).

Is there a way to have the best of both worlds, i.e., a guarantee that I'm actually reusing the logic without the overhead of self-HTTP-consumption?

Share Improve this question asked Mar 28 at 8:59 SimoneSimone 1,3371 gold badge18 silver badges35 bronze badges 3
  • 1 In my opinion, I will extract the common logic into a separate class library that both projects reference. – Brando Zhang Commented Mar 31 at 8:50
  • @BrandoZhang This is probably what I will do, at least for now. But I would still like to execute it in one of the two projects. – Simone Commented Mar 31 at 12:37
  • Its OK for now, but in the future, I suggest you could move into separate class library. – Brando Zhang Commented Apr 2 at 6:41
Add a comment  | 

1 Answer 1

Reset to default 0
  1. Extract the common logic into a separate shared library (e.g., a .ClassLib project).

    Both Project A and Project B reference this library.

    • No HTTP overhead, no duplication, guaranteed consistency.
  2. If a shared library isn’t possible, use conditional dependency injection:

    In Project A, inject the direct implementation (no HTTP).

    In Project B, inject the Typed HTTP Client (calls Project A’s API).

    Ensures the same logic is executed in both cases.

Why Avoid Self-HTTP Calls?

  • Unnecessary overhead (network, serialization).

  • Risk of inconsistency if logic diverges.

Alternative: MediatR (In-Process Mediator)

  • Use MediatR to handle internal calls in Project A while exposing the same logic via HTTP for Project B.
发布评论

评论列表(0)

  1. 暂无评论