Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 87 additions & 10 deletions Chromium.AspNetCore.Bridge/OwinFeatureImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public class OwinFeatureImpl :
private IHeaderDictionary _requestHeaders;
private IHeaderDictionary _responseHeaders;

//--> Added the Func Task's ability to capture callbacks from OnStarting/OnComplete Middleware
private Func<Task> _responseStartingAsync = () => Task.CompletedTask;
private Func<Task> _responseCompletedAsync = () => Task.CompletedTask;
//-->

private bool _started;

/// <summary>
/// Initializes a new instance of <see cref="Microsoft.AspNetCore.Owin.OwinFeatureCollection"/>.
/// </summary>
Expand Down Expand Up @@ -102,7 +109,7 @@ IHeaderDictionary IHttpRequestFeature.Headers
{
get
{
if(_requestHeaders == null)
if (_requestHeaders == null)
{
var dict = GetEnvironmentPropertyOrDefault<IDictionary<string, string[]>>(OwinConstants.RequestHeaders);
_requestHeaders = dict is IHeaderDictionary ? (IHeaderDictionary)dict : new DictionaryStringValuesWrapper(dict);
Expand Down Expand Up @@ -174,19 +181,47 @@ PipeWriter IHttpResponseBodyFeature.Writer
}
}

bool IHttpResponseFeature.HasStarted
{
get { return false; }
}
bool IHttpResponseFeature.HasStarted => false;

void IHttpResponseFeature.OnStarting(Func<object, Task> callback, object state)
{

//not protecting with HasStarted because is always false
//normaly OnStarting Execute before stream sent to client

//-> Disable below code then create simple Middleware with Onstarting/OnComplete,
//-> You will notice that all the OnStarting/OnComplete in the middleware will never be triggered

var prior = _responseStartingAsync;
_responseStartingAsync = async () =>
{
await callback(state);
await prior();
};
//->

}

void IHttpResponseFeature.OnCompleted(Func<object, Task> callback, object state)
{


//not protecting with HasStarted because is always false

//-> Disable below code then create simple Middleware with Onstarting/OnComplete,
//-> You will notice that all the OnStarting/OnComplete in the middleware will never be triggered

var prior = _responseCompletedAsync;
_responseCompletedAsync = async () =>
{
try
{
await callback(state);
}
finally
{
await prior();
}
};
//->
}

Task IHttpResponseBodyFeature.SendFileAsync(string path, long offset, long? length, CancellationToken cancellation)
Expand All @@ -200,25 +235,67 @@ void IHttpResponseBodyFeature.DisableBuffering()
{
}

async Task FireOnSendingHeadersAsync()
{

try
{
await _responseStartingAsync();
}
finally
{
//HasStarted = true;
}
}

Task FireOnResponseCompletedAsync()
{
return _responseCompletedAsync();
}

async Task IHttpResponseBodyFeature.StartAsync(CancellationToken cancellationToken)
{
try
{
_started = true;
await FireOnSendingHeadersAsync();

}
catch (Exception)
{
throw;
}

if (_responseBodyWrapper != null)
{
await _responseBodyWrapper.FlushAsync(cancellationToken);
}

// The pipe may or may not have flushed the stream. Make sure the stream gets flushed to trigger response start.
//// The pipe may or may not have flushed the stream. Make sure the stream gets flushed to trigger response start.
await GetEnvironmentPropertyOrDefault<Stream>(OwinConstants.ResponseBody).FlushAsync(cancellationToken);


}

Task IHttpResponseBodyFeature.CompleteAsync()
{



if (!_started)
{
((IHttpResponseBodyFeature)this).StartAsync().Wait();
}

FireOnResponseCompletedAsync();

if (_responseBodyWrapper != null)
{
return _responseBodyWrapper.FlushAsync().AsTask();
_responseBodyWrapper.FlushAsync().ConfigureAwait(false);
}

return Task.CompletedTask;

return ((IHttpResponseBodyFeature)this).Writer.CompleteAsync().AsTask();
}

/// <inheritdoc/>
Expand Down
1 change: 1 addition & 0 deletions Chromium.AspNetCore.Bridge/OwinServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Task IServer.StartAsync<TContext>(IHttpApplication<TContext> application, Cancel
try
{
await application.ProcessRequestAsync(context);
await features.Get<IHttpResponseBodyFeature>().StartAsync();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;

namespace WebView2.AspNetCore.Mvc.Example.Wpf
{
Expand All @@ -14,6 +15,12 @@ public ActionResult Index()
Text = "Welcome",
Method = "GET"
};

if (string.IsNullOrEmpty(HttpContext.Session.GetString("hi")))
{
HttpContext.Session.SetString("hi", "Hello world");
}

return View("Index", model);
}

Expand All @@ -25,11 +32,13 @@ public ActionResult Index(HomeBindingModel inputModel)
Text = string.Format("Input from Form Post: {0} - {1}", inputModel.Input1, inputModel.Input2),
Method = "POST"
};
var sessionhi = HttpContext.Session.GetString("hi");
return View("Index", model);
}

public IActionResult Privacy()
{
var sessionhi = HttpContext.Session.GetString("hi");
return View();
}

Expand Down
42 changes: 42 additions & 0 deletions WebView2.AspNetCore.Mvc.Example.Wpf/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;


namespace WebView2.AspNetCore.Mvc.Example.Wpf
{
Expand All @@ -19,6 +23,15 @@ public Startup(IConfiguration configuration)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDistributedMemoryCache();
services.AddSession(opt =>
{
opt.Cookie.HttpOnly = true;
opt.Cookie.IsEssential = true;
opt.IdleTimeout = TimeSpan.FromHours(1);

});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand All @@ -35,6 +48,12 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseSession();


app.UseMiddleware<TestMiddleware>();


app.UseEndpoints(endpoints =>
{
Expand All @@ -45,4 +64,27 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

}
}

// testing for invetigation purpose
public class TestMiddleware
{
private readonly RequestDelegate _next;

public TestMiddleware(RequestDelegate next)
{
_next = next;
}

public async Task InvokeAsync(HttpContext context)
{
context.Response.OnStarting(e => {
//disabled code in IHttpResopnseFeature.OnStarting and OnComplete (OwinFeatureImpl.cs)
//create break point here,
return Task.CompletedTask;
}
, null);

await _next(context);
}
}
}