Namespace Apache.Ignite.Core.Services
Classes
ServiceCallContextBuilder
Service call context builder.
ServiceConfiguration
Service configuration.
ServiceDeploymentException
Indicates an error during Grid Services deployment.
ServiceInvocationException
Indicates an error during Grid Services invocation.
Interfaces
IService
Represents Ignite-managed service.
IServiceCallContext
Represents service call context.
This context is implicitly passed to the service and can be retrieved inside the service using CurrentCallContext. It is accessible only from the local thread during the execution of a service method.
Use ServiceCallContextBuilder to instantiate the context.
Note: passing the context to the service may lead to performance overhead, so it should only be used for "middleware" tasks.
Usage example:
// Service implementation.
public class HelloServiceImpl : HelloService
{
private IServiceContext ctx;
public void Init(IServiceContext ctx)
{
this.ctx = ctx;
}
public string Call(string msg)
{
return msg + ctx.CurrentCallContext.Attribute("user");
}
...
}
...
// Call this service with context.
IServiceCallContext callCtx = new ServiceCallContextBuilder().Set("user", "John").build();
HelloService helloSvc = ignite.GetServices().GetServiceProxy<HelloService>("hello-service", false, callCtx);
// Print "Hello John".
Console.WriteLine( helloSvc.call("Hello ") );
IServiceCallInterceptor
Represents service call interceptor.
Allows the user to intercept the call to any service method except lifecycle methods (Init(IServiceContext), Execute(IServiceContext) and Cancel(IServiceContext)).
A typical use of an interceptor is a middleware logic that applies to all custom methods in a service.
The user can specify multiple interceptors in the ServiceConfiguration.
Each interceptor invokes the next interceptor in the chain using a delegated call, the last interceptor
will call the service method.
class Security : IServiceCallInterceptor
{
public object Invoke(string mtd, object[] args, IServiceContext ctx, Func<object> next)
{
if (!CustomSecurityProvider.Instance().Access(ctx.CurrentCallContext.GetAttribute("sessionId")))
throw new SecurityException();
// Execute remaining interceptors and service method.
return next.Invoke();
}
}
class Audit : IServiceCallInterceptor
{
public object Invoke(string mtd, object[] args, IServiceContext ctx, Func<object> next)
{
var sessionId = ctx.CurrentCallContext.GetAttribute("sessionId");
var audit = AuditProvider.Instance();
audit.RecordEvent("start", mtd, sessionId);
try
{
// Execute service method.
return next.Invoke();
}
catch (Exception e)
{
// Record error.
audit.RecordEvent("error", mtd, "id=" + sessionId + ", err=" + e.Message);
// Re-throw exception to initiator.
throw;
}
finally
{
// Record finish event after execution of the service method.
audit.RecordEvent("finish", mtd, sessionId);
}
}
}
...
var svcCfg = new ServiceConfiguration()
{
Name = "service",
Service = new MyService(),
MaxPerNodeCount = 1,
Interceptors = new List<IServiceCallInterceptor> { new Audit(), new Security() }
};
// Deploy service.
ignite.GetServices().Deploy(svcCfg);
// Set context parameters for the service proxy.
var callCtx = new ServiceCallContextBuilder().Set("sessionId", sessionId).Build();
// Make service proxy with caller context to define sessionId attribute.
var proxy = ignite.GetServices().GetServiceProxy<IMyService>("service", false, callCtx);
// Service method call will be intercepted.
proxy.PlaceOrder(order1);
proxy.PlaceOrder(order2);
IServiceContext
Represents service execution context.
IServiceDescriptor
Service deployment descriptor.
IServices
Defines functionality to deploy distributed services in the Ignite.