pmilet.Playback 1.0.7

.NET Core 1.1 .NET Framework 4.5.2
There is a newer version of this package available.
See the version list below for details.
NuGet\Install-Package pmilet.Playback -Version 1.0.7
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.
dotnet add package pmilet.Playback --version 1.0.7
<PackageReference Include="pmilet.Playback" Version="1.0.7" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add pmilet.Playback --version 1.0.7
#r "nuget: pmilet.Playback, 1.0.7"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install pmilet.Playback as a Cake Addin
#addin nuget:?package=pmilet.Playback&version=1.0.7

// Install pmilet.Playback as a Cake Tool
#tool nuget:?package=pmilet.Playback&version=1.0.7

Asp.Net Core Playback

An Asp.Net Core middleware library that simplifies the recording and playback of WebApi incoming requests and incoming responses from outgoing requests. Suitable for saving user interactions in production to be replayed in development.

Three usage scenarios

  1. Record incoming Api requestsin order to reproduce for testing or troubleshooting.
  2. Fake Api responses in order to quickly design your rest api interface .
  3. Prepare your service proxy to record incoming responses from outgoing calls (to external systems or services).

When the X-Playback-Mode header is set to Record the request is saved to a remote storage (remote blob or local file storage available for the moment) and then a X-Playback-Id reponse header is returned that should be used for replay.

To replay a recorded request set the X-Playback-Mode request header to Playback mode and X.Playback-Id request header to the value returned in the previous step.

When the X-Playback-Mode is set to Fake fake responses will be returned. The faked responses are codified in a fake factory class you should implement and explicitly register.

There is also the possibility to capture the responses of any outgoing Api request in order to test the Api in total isolation. Use the IPlaybackContext interface into your outgoing service proxies ( by injecting the IPlaybackContext into the constructors ). This interface provides methods for saving and replaying the responses from outgoing calls ( and correlate to the Api playback-id).

Use case 1 : has a developer I want to record api requests in order to be able to replay them.

In you web api project install pmilet.Playback package: Install-Package pmilet.Playback -Version 1.0.6

Configure your Startup class

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            
            services.AddPlayback(Configuration);

            ...
            
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Swashbuckle.AspNetCore.Swagger.Info { Title = "My API", Version = "v1" });
            });

        }
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            ...
            
            app.UseSwagger();
            app.UseSwaggerUI(c=> c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"));

            app.UsePlayback();
      
            ...
        }

Configure playback storage settings. The default storage service is Azure Blob Storage. A Storage connection string and container name should be provided. Add this section to theappsettings.json file:

"PlaybackBlobStorage": { "ConnectionString": "UseDevelopmentStorage=true", "ContainerName": "playback" }

To view the playback headers in swagger decorate your api method with the PlaybackSwaggerFilter

       [HttpGet]
       [SwaggerOperation("Hello")]
       [SwaggerOperationFilter(typeof(PlaybackSwaggerFilter))]
       public async Task<string> Get()

Test it:

  1. Navigate to swagger UI
  2. Set the X-Playback-Mode request header to Record and try-out
  3. Copy the X-Playback-Id value
  4. Set the X-Playback-Mode to Playback and try-out.
  5. You should receive the same result has in step 2.

Use case 2 : has a developer I want to fake my api responses in order to design my api contract quickly.

   public void ConfigureServices(IServiceCollection services)
   {
       ...

           services.AddPlayback(Configuration, fakeFactory: new MyPlaybackFakeFactory());

       ...
   }

Implement your fake factory: for example in this example the when requesting the uri: /api/hello with GET verb a Hello Fake string is returned

   public class MyPlaybackFakeFactory : FakeFactoryBase
   {
       public override void GenerateFakeResponse(HttpContext context)
       {
           switch (context.Request.Path.Value.ToLower())
           {
               case "/api/hello":
                   if (context.Request.Method == "POST")
                       GenerateFakeResponse<HelloRequest, string>(context, HelloPost);
                   else if (context.Request.Method == "GET")
                       GenerateFakeResponse<string, string>(context, HelloGet);
                   break;
               default:
                   break;
           }
       }
      
       private string HelloGet(string request)
       {
           return "Hello FAKE";
       }

Test it:

  1. Navigate to swagger UI
  2. Set X-Playback-Mode header to Fake and try-out
  3. You should receive the faked response.

Use case 3 : has a developer I want to record my api requests and outgoing responses in order to be able to replay them.

Use the IPlaybackContext interface into your outgoing services proxy to record and replay outgoing responses:

   public class MyServiceProxy
   {
        IPlaybackContext _playbackContext;
        public MyServiceProxy(IPlaybackContext context )
        {
            _playbackContext = context;
        }

        public async Task<MyServiceResponse> Execute( MyServiceRequest command)
        {
            var result =  new MyServiceResponse() {  Output = $"MyService received input: {command.Input}" };
            if (_playbackContext.IsRecord)
            {
                await _playbackContext.RecordResult<MyServiceResponse>(result);
            }
            else if ( _playbackContext.IsPlayback )
            {
                return await _playbackContext.PlaybackResult<MyServiceResponse>();
            }
            return result;
        }
    }
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp1.1 netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Framework net452 net46 net461 net462 net463 net47 net471 net472 net48
Compatible target framework(s)
Additional computed target framework(s)
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
1.2.2 1,323 4/1/2019
1.2.1 920 2/19/2018
1.2.0 820 2/19/2018
1.1.2 900 1/23/2018
1.1.1 950 1/23/2018
1.1.0 824 1/23/2018
1.0.10 1,588 10/27/2017
1.0.9 1,144 10/25/2017
1.0.8 869 10/25/2017
1.0.7 1,003 10/22/2017
1.0.6 829 10/21/2017
1.0.5 761 10/21/2017
1.0.4 802 10/21/2017
1.0.3 775 10/18/2017
1.0.2 757 10/18/2017
1.0.1 772 10/18/2017
1.0.0 655 10/15/2017

netstandard 2.0