Search Results for

Show / Hide Table of Contents

# Interface 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);

###### Namespace: Apache.Ignite.Core.Services

###### Assembly: Apache.Ignite.Core.dll

##### Syntax

    public interface IServiceCallInterceptor

### Methods

#### Invoke(String, Object[], IServiceContext, Func<Object>)

Intercepts delegated service call.

##### Declaration

    object Invoke(string mtd, object[] args, IServiceContext ctx, Func<object> next)

##### Parameters

Type | Name | Description
--- | --- | ---
System.String | mtd | Method name.
System.Object[] | args | Method arguments.
IServiceContext | ctx | Service context.
Func<System.Object> | next | Delegated call to a service method and/or interceptor in the chain.

##### Returns

Type | Description
--- | ---
System.Object | Service call result.

### See Also

IServiceCallContext
IServiceContext