web-servicesbond

How do I implement functions in a Bond services definition?


Looking at the Bond Comm documentation, it wasn't clear to me how the functions I define for services are connected to specific functions in my code.

Does it look for a function with the same signature in the project and assign it to the endpoint? Is there some underlying settings file I am missing?


Solution

  • NB: Bond Comm is deprecated. It isn't supported any more, and will be removed from Bond in an upcoming release. Bond-over-gRPC is its replacement.

    When using either Bond-over-gRPC or Bond Comm, the generated server-side code is an abstract class with an abstract method for each method in the service definition. To provide your logic for these methods, you inherit from the generated base and provide implementations for all the service methods. Then, typically in your main function, you create a Server (for Bond-over-gRPC) or a Listener (for Bond Comm) and register an instance of the implementation class. This sets up the routing for IDL service method to your implementation code.

    From the Bond-over-gRPC C# documentation:

    Given a service definition like the following:

    service Example
    {
        ExampleResponse ExampleMethod(ExampleRequest);
    }
    

    gbc will generate C# classes for gRPC with the --grpc flag:

    gbc c# --grpc example.bond
    

    ...

    To build the service functionality, simply write a concrete service implementation by subclassing the server base and supplying the business logic:

    public class ExampleServiceImpl : Example.ExampleBase {
        public override async Task<IMessage<ExampleResponse>>
            ExampleMethod(
                IMessage<ExampleRequest> param,
                ServerCallContext context)
        {
            ExampleRequest request = param.Payload.Deserialize();
            var response = new ExampleResponse();
    
            // Service business logic goes here
    
            return Message.From(response);
        }
    }
    

    This service implementation is hooked up to a gRPC server as follows:

    var server = new Grpc.Core.Server {
        Services = { Example.BindService(new ExampleServiceImpl()) },
        Ports = { new Grpc.Core.ServerPort(ExampleHost, ExamplePort, Grpc.Core.ServerCredentials.Insecure) } };
    server.Start();
    

    At this point the server is ready to receive requests and route them to the service implementation.

    There are more examples as well:

    It's worth pointing out that (Bond-over-) gRPC and Bond Comm are neither SOAP nor REST. The question was tagged with , and sometimes people mean SOAP/REST when they talk about web services. I think of both gRPC and Bond Comm as custom binary protocols over TCP, although gRPC is run atop HTTP/2.