admin 管理员组文章数量: 1086019
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
1 Answer
Reset to default 0Extract 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.
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.
本文标签: aspnet coreConsuming self HTTP API or directly calling the handlerStack Overflow
版权声明:本文标题:asp.net core - Consuming self HTTP API or directly calling the handler? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744047056a2524316.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论