Skip to content

Commit

Permalink
AspNetCore: Fix OutputCache middleware support and update README (#426)
Browse files Browse the repository at this point in the history
* Complete response so it works with OutputCache middleware
* Update ReadMe and Todo
* Update VersionPrefix to 5.3.1
  • Loading branch information
Daniel-Svensson authored Jun 21, 2023
1 parent 6bf155c commit 5ff0fac
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Unreleased
# AspNetCore 0.4.0

* AspNetCore
* Copies "All" attributes to endpoint metadata
Expand All @@ -11,6 +11,8 @@
.WithGroupName("Cities");
});
```
* Updated README.md and added Sample project
* Make it compatible with more middleware such as OutputCache middleware by "Completing" responses
* CHANGES:
* `services.AddOpenRiaServices<T>()` now requires T to derive from DomainServce
* `services.AddOpenRiaServices<T>()` has different return type
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<GenerateDocumentationFile Condition="'$(IsFrameworkProject)' == 'true'">true</GenerateDocumentationFile>
<CopyLocalLockFileAssemblies Condition="'$(IsFrameworkProject)' == 'true'">false</CopyLocalLockFileAssemblies>

<VersionPrefix>5.2.0</VersionPrefix>
<VersionPrefix>5.3.1</VersionPrefix>
<!-- Set correct version suffix below if manually building
<VersionSuffix>preview.5</VersionSuffix>
-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,9 @@ public async Task WriteTo(HttpResponse response, CancellationToken ct)
{
await response.StartAsync(ct);
WriteTo(response.BodyWriter);
//await response.BodyWriter.FlushAsync(ct);
//await response.CompleteAsync(); //? needed ??

// Say that we have finished writing the request, needed for other middleware such as OutputCache middleware
await response.CompleteAsync();
}

private void WriteTo(PipeWriter bodyWriter)
Expand Down
65 changes: 58 additions & 7 deletions src/OpenRiaServices.Hosting.AspNetCore/Framework/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,25 @@ This excludes usage by the Russian state, Russian state-owned companies, Russian
- You allow anonymized telemetry to collected and sent during the preview releases to gather feedback about usage


## PREVIEW
## Production Ready - "preview"

The package is currently in preview, please look at TODO in project's folder for more details.
Only use if you feel competent and have the time to debug and troubleshoot issues yourself.
The package is production ready, but does not yet contain all features planened for 1.0.
Please look at TODO in project's folder for more details.

**Public API will change before 1.0.0 release**

There is no documentation yet, please see AspNetCoreWebsite project in repository for usage.
*Code generation is not tested and will probably not work with this package.*

* For a sample see [WpfCore_AspNetCore in Samples repository](https://github.com/OpenRIAServices/Samples/tree/main/WpfCore_AspNetCore)



## Getting Started

1. Create a new dotnet 6 web application `dotnet new web` or similar
2. Add a reference to *OpenRiaServices.Hosting.AspNetCore*
`dotnet add package OpenRiaServices.Hosting.AspNetCore`
3. Add a reference to *OpenRiaServices.Server* **5.1.0-preview.6 OR LATER**

3. Add a reference to *OpenRiaServices.Server*

4. Add one or more domainservices

Expand Down Expand Up @@ -70,10 +72,59 @@ builder.Services.AddTransient<CityDomainService>();
var app = builder.Build();
app.MapOpenRiaServices(builder =>
{
builder.AddDomainService(typeof(CityDomainService));
builder.AddDomainService<CityDomainService>();
});
app.Run();
```

## Asp.Net Core integration

Since 0.4.0 any attivbute applied to Invoke/Query are added to the corresponding AspNetCore-Endpoint allowing the use
of any AspNetCore endpoint middleware (not MVC specific filters, they must work with "minimal api's").

This means you can use standard [aspnetcore Authentication and Authorization](https://learn.microsoft.com/en-us/aspnet/core/security/?view=aspnetcore-7.0)
to validate most requests (but **NOT** indivudual Insert,Update,Delete methods, you can apply atttributes to your DomainService class for those).
The validation will happen before the DomainService is even created, making them more powerful than built in attributes.

You can still (and probably should) use the OpenRiaServices specific attributes such as `[RequiresAuthentication]` or [your own Authorization attributes](https://openriaservices.gitbook.io/openriaservices/ee707361/ee707357)
[Simple authorization](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/simple?view=aspnetcore-7.0), which works on all methods.




### Output Cache Integration

Sample showing how to integrate the [OutputCache middleware](https://learn.microsoft.com/en-us/aspnet/core/performance/caching/output?view=aspnetcore-7.0)
**WARNING:** Se caching documentation and ensure that any usage of output cache is not sent to the wrong user.

```
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenRiaServices();
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(builder => builder.NoCache());
});
builder.Services.AddTransient<CacheTestDomainService>();
var app = builder.Build();
app.UseOutputCache();
[EnableClientAccess]
public class CacheTestDomainService : DomainService
{
[Invoke(HasSideEffects = false)]
public string NoCache()
=> DateTime.Now.ToString();
[Invoke(HasSideEffects = false)]
[Microsoft.AspNetCore.OutputCaching.OutputCache(Duration = 5)]
public string OutputCache()
=> DateTime.Now.ToString();
}
````

0 comments on commit 5ff0fac

Please sign in to comment.