Tore.Service.ActionShell 5.0.0

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Tore.Service.ActionShell --version 5.0.0
NuGet\Install-Package Tore.Service.ActionShell -Version 5.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Tore.Service.ActionShell" Version="5.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Tore.Service.ActionShell --version 5.0.0
#r "nuget: Tore.Service.ActionShell, 5.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Tore.Service.ActionShell as a Cake Addin
#addin nuget:?package=Tore.Service.ActionShell&version=5.0.0

// Install Tore.Service.ActionShell as a Cake Tool
#tool nuget:?package=Tore.Service.ActionShell&version=5.0.0

Tore.Service.ActionShell

Language: C#.

Nuget package: Tore.Service.ActionShell

Dependencies: <br/>

ActionShell v5.0.0 for net5 .<br/>   .net5<br/>   Microsoft.AspNetCore.Mvc.NewtonsoftJson (5.0.10) [Please refer to note 2 below]<br/> <br/> ActionShell v6.0.0+ for net6 .<br/>   .net6<br/>   Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 6) [Please refer to note 2 below]<br/>

ActionShell :

A standard action filter attribute class for .Net web API <br/>

Why?<br/>

  • Bored of writing the same thing every time for services.

  • This code contains the common behaviour of an action filter attribute.

  • Having project dependent request scope data is so good.

  • I can gather any data requied and process it and/or check sessions, access caches, databases, get the IP's etc. <br/> In the multi tenant server mode (Kestrel default) each request arriving is processed in its own thread,<br/> The request context is built, then the target controller instance is created.<br/> If there are action filters:<br/> Filter OnActionExecuting methods are called before the endpoint is invoked and,<br/> Filter OnActionExecuted methods are called after the endpoint is left.<br/> <br/> ActionShell class as a filter attribute:<br/>

  • Constructs a developer defined object descendant of RequestScopeBase class gathering request scope data.

  • Stores the scope in HttpContext.

  • Before endpoint invocation, calls ActionShell.enter delegate if bound, with scope.

  • After endpoint completion, calls ActionShell.leave delegate if bound, with scope.

For using it, modifications must be done before service kicks in, e.g.: in startup.cs:

// Add this to your startup.cs usings.

using Tore.Service;

Add bindings at service configure method:

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
      ...
      ActionShell.requestScopeType = typeof(DeveloperDefinedClassDescendingFromRequestScopeBase);
      ActionShell.enter = SomeClass.aStaticMethodToCallBeforeEnteringEndpoint;
      ActionShell.leave = SomeClass.aStaticMethodToCallAfterLeavingEndpoint;
      ...
      
      app.UseRouting();
 
      app.UseAuthorization();

      app.UseEndpoints(endpoints => {endpoints.MapControllers();});
  }

The enter and leave methods are not mandatory.<br/> They receive scope object which is RequestScopeBase descendant as parameter.<br/> So if defined they should be a delegate of type:

public delegate void ActionShellDelegate(RequestScopeBase scope);

The methods should be bound as:

    ActionShell.enter = SomeClass.aStaticMethodToCallBeforeEnteringEndpoint;
    ActionShell.leave = SomeClass.aStaticMethodToCallAfterLeavingEndpoint;

Binding to controllers:

using Tore.Service; // <-- Needed.

namespace WorldDomination.Secret.Project.Controllers {

    [ApiController]
    [ActionShell] // <-- Easy.
    public class TheSuperDuperController: ControllerBase {
        // The super duper controller things here.
    }
}

RequestScopeBase :

To generate project oriented request information, developers must extend the RequestScopeBase class. <br/> It is already populated as follows : <br/>

    routerPath = context.HttpContext.Request.Path;
    actionName = context.ActionDescriptor.RouteValues["Action"];
    modelValid = context.ModelState.IsValid;
    controller = (ControllerBase)context.Controller;
    leftAction = false;   // this is set to true at OnActionExecuted.

Here is an example, additionally collecting the IP address of the client:

public class ExampleRequestScope : RequestScopeBase {
    public string ipAddress {get; private set;} = null // We will store IP address of requester here.
    // other things needed...
    
    public ExampleRequestScope(ActionExecutingContext context):base(context){
        ipAddress = context.HttpContext.Connection.RemoteIpAddress?.ToString();
        /// collect other data needed.
    }
}

For the extended class to take control it has to be assigned like this: <br/>

    ActionShell.requestScopeType = typeof(ExampleRequestScope);

Wherever HttpContext object is accessible then scope is accessible via:

    var ctx = Somewhere.to.obtain.HttpContext;
    var scope = (ExampleRequestScope)ActionShell.fetchScope(ctx);

Notes:<br/> <br/> 1] ActionShell assignments should be done at configuration.<br/>   After service starts, since system goes multithreading, do not change assignments.<br/>   Turkish proverb :While crossing the river, one does not switch horses...<br/> <br/> 2] Why Microsoft.AspNetCore.Mvc.NewtonsoftJson? <br/>   Weirdly enough default http abstractions miss some methods.<br/>   So it saves me from a lot of class chasings and abstractions and I use it in my API's anyway.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
7.0.0 227 4/11/2023
6.0.1 475 4/6/2022
5.0.1 483 2/4/2022

Comments corrected for ActionShell class definition.
This is the last compilation for net 5.