Services.Context

An example demonstrating how to obtain the global context and to lookup a service via the Context.service(Name) method.

  static void global () {

    final Name name =
      Services.name (
        "service.one"
      );

    final Context context =
      Services.context ();

    assert
      context ==
        Services.context ();

    final Service service =
      context.service (
        name
      );

    assert
      service ==
        context.service (
          name
        );

  }

This example demonstrates how to create an anonymous context not shared by the signaling runtime with other callers.

  private static void anonymous () {

    final Name name =
      Services.name (
        "service.one"
      );

    final Context context =
      Services.context (
        Services.environment ()
      );

    assert
      context !=
        Services.context (
          Services.environment ()
        );

    final Service service =
      context.service (
        name
      );

    assert
      service ==
        context.service (
          name
        );

    assert
      service !=
        Services.context (
          Services.environment ()
        ).service (
          name
        );

  }

To access a shared managed context within a process the environment must have a "opensignals.service.context.id" value.

  private static void identified () {

    final Environment env =
      Services.environment (
        Services.name (
          "opensignals.services.context.id"
        ),
        "service.1"
      );

    final Context context =
      Services.context (
        env
      );

    assert
      context ==
        Services.context (
          env
        );

  }