NuGet package | Owin.Framework.Pages.Restful |
GitHub source | OwinFramework.Pages.Restful |
This package contains a Build Engine for services. If you want the fluent builder to use this build engine you need to add a couple of lines to your startup code similar to:
var fluentBuilder = ninject.Get<IFluentBuilder>(); ninject.Get<OwinFramework.Pages.Restful.BuildEngine>().Install(fluentBuilder);
After adding this build engine to the fluent builder you can add web services to your application by writing classes that are decorated with attributes. The fluent builder will discover these classes and use the attributes to configure these services.
The code below is taken from one of the sample applications. It is a service that exposes 4 endpoints that add, subtract, multiply and divide two numbers.
You can invoke this service by typing a URL into a browser similar to http://mycompany.com/math/add?a=45&b=76.
using System; using System.Collections.Generic; using OwinFramework.Pages.Core.Attributes; using OwinFramework.Pages.Core.Enums; using OwinFramework.Pages.Restful.Interfaces; namespace Sample1.SampleServices { [IsService("arithmetic", "/math/", new[] { Method.Get })] public class ArithmeticService { [Endpoint] [EndpointParameter("a", typeof(double))] [EndpointParameter("b", typeof(double))] public void Add(IEndpointRequest request) { var a = request.Parameter<double>("a"); var b = request.Parameter<double>("b"); request.Success(a + b); } [Endpoint] [EndpointParameter("a", typeof(double))] [EndpointParameter("b", typeof(double))] public void Subtract(IEndpointRequest request) { var a = request.Parameter<double>("a"); var b = request.Parameter<double>("b"); request.Success(a - b); } [Endpoint] [EndpointParameter("a", typeof(double))] [EndpointParameter("b", typeof(double))] public void Multiply(IEndpointRequest request) { var a = request.Parameter<double>("a"); var b = request.Parameter<double>("b"); request.Success(a * b); } [Endpoint] [EndpointParameter("a", typeof(double))] [EndpointParameter("b", typeof(double))] public void Divide(IEndpointRequest request) { var a = request.Parameter<double>("a"); var b = request.Parameter<double>("b"); request.Success(a / b); } } }
The class that defines your service must be decorated with an [IsService()] attribute as shown in the example above.
Your class can contain any number of methods. Any or all of these methods can be exposed as REST endpoints by decorating the method with an [Endpoint()] attribute. These methods can be executed by sending Httl requests to your website.
Endpoint methods can optionally be decorated with one or more [EndpointParameter()] attributes to define the parameters that can be passed to the method. These attributes also define the mechanisms that are supported for parameter passing and the parameter parser to use.
Endpoint methods can optionally have additional arguments and these arguments can be decorated with [EndpointParameter()] attributes to define how parameters passed in the Html request are bound to those method arguments.
The two endpoints below will function identically to the caller of the Http endpoint. The first version produces slightly better API documentation and is the recommended syntax, but you can also use the second version of the syntax if you prefer.
[Endpoint] [EndpointParameter("a", typeof(double))] [EndpointParameter("b", typeof(double))] public void Add(IEndpointRequest request) { var a = request.Parameter<double>("a"); var b = request.Parameter<double>("b"); request.Success(a + b); }
[Endpoint] public void Add( IEndpointRequest request, [EndpointParameter] double a, [EndpointParameter] double b) { request.Success(a + b); }
This service builder supports the following features:
For more details on how to take advantage of these features, click the links below: