Automatic Discovery and Registration
Manual registration of request handlers with dependency injection mechanism is acceptable for relatively small projects but becomes tedious in large systems where new operations are added frequently:
services.AddKeyedTransient<IRpcRequestHandler, BankMakeDepositHandler>("Bank.MakeDeposit");
services.AddKeyedScoped<IRpcRequestHandler, ShipmentSearchHandler>("Shipment.Search");
...
A more scalable approach is to register handlers automatically based on assembly attributes and request handler attributes.
For example, we may define an attribute named RpcAssembly, that we can put on assemblies that contain RPC handlers:
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = false)] public sealed class RpcAssemblyAttribute : Attribute
If we have a Bank assembly that contains handlers for banking operations, we can mark it with this attribute, by writing the following line of code inside the assembly metadata file (commonly AssemblyInfo.cs):
[assembly: RpcAssembly("Bank")]
We may also define another attribute named RpcMethod, that we can put on handler classes:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] public sealed class RpcMethodAttribute : Attribute
If we have a handler class for the Bank.MakeDeposit operation, we can mark it with this attribute and specify the method name and description:
[RpcMethod("Bank.MakeDeposit", Description="Deposits money into an existing bank account and returns the new balance.", IsReadOnly=false)]
public class BankMakeDepositHandler
As a result, at startup, our code can:
Table of Content Operation-Oriented APIs and AI Tools Previous: One Operation, One Handler Next: Swagger and RPC Discoverability
Business Process Programming in .Net
© 2004–2026 Laskarzhevsky Software Inc.
Unless otherwise noted, the content of this website is licensed under the
Creative Commons Attribution 4.0 International License (CC BY 4.0).
Code examples are provided under the MIT License.
You are free to share and adapt the material provided that appropriate
credit is given and any modifications are clearly indicated.
The information provided on this website is for educational purposes only.
The author and publisher make no warranties regarding the completeness
or suitability of the information and are not responsible for any damages
resulting from its use.