-
-
Notifications
You must be signed in to change notification settings - Fork 200
Description
Summary
Add support for .NET Framework 4.8 as an additional target framework to Dotmim.Sync.Web.Server, enabling the library to be used with ASP.NET Web API 2 and MVC 5 applications
running on .NET Framework 4.8.
Motivation
Currently, Dotmim.Sync.Web.Server only supports ASP.NET Core environments (netstandard2.0, net6.0, net8.0). Many production applications still run on .NET Framework 4.8 with
ASP.NET Web API 2, and cannot be migrated to ASP.NET Core immediately. Supporting net48 would allow these applications to use Dotmim.Sync without requiring a full
migration.
Proposed Changes
- Add net48 Target Framework
Update Dotmim.Sync.Web.Server.csproj:
- Add net48 to TargetFrameworks
- Add conditional package references for net48:
- System.Web (framework reference)
- Microsoft.Extensions.DependencyInjection.Abstractions v2.2.0
- Microsoft.AspNet.WebApi.Core v5.2.3
- Microsoft.AspNet.WebApi.Client v6.0.0
- Create HTTP Abstraction Layer
Create HttpExtensions.cs with extension methods to bridge differences between ASP.NET Core and Web API 2:
- Request/Response handling: Map between HttpRequestMessage/HttpResponseMessage (Web API 2) and HttpRequest/HttpResponse (ASP.NET Core)
- Header access: Provide consistent API for reading/writing headers across both frameworks
- Body reading/writing: Abstract stream operations for both frameworks
- Support Multiple Session Types
Extend SessionExtensions.cs to support:
- HttpSessionState and HttpSessionStateBase (Web API 2)
- ISession (ASP.NET Core)
With consistent Get(), Set(), GetString(), SetString() methods across all types.
- Add Conditional Compilation to Core Files
Update the following files with #if NET48 conditional compilation:
- WebServerAgent.cs: Main agent class with dual method signatures
- WebServerAgent.Incremental.cs: Incremental sync operations
- HttpContextArgs.cs: HTTP context arguments
- DependencyInjection.cs: Dependency injection extensions (exclude ASP.NET Core-specific extensions from net48)
Key differences to handle:
- Session access (System.Web.HttpContext.Current.Session vs HttpContext.Session)
- HTTP request/response types (HttpRequestMessage/HttpResponseMessage vs HttpRequest/HttpResponse)
- Header operations (different APIs between frameworks)
- Status codes (HttpStatusCode enum vs StatusCodes static class)
- Use Type Aliases for Clarity
Use type aliases in net48 code to map Web API 2 types to expected names:
#if NET48
using HttpRequest = System.Net.Http.HttpRequestMessage;
using HttpResponse = System.Net.Http.HttpResponseMessage;
using HttpContext = System.Web.HttpContextBase;
#endif
Expected Benefits
- Enables .NET Framework 4.8 applications to use Dotmim.Sync server-side components
- No breaking changes to existing ASP.NET Core functionality
- Maintains single codebase with conditional compilation
- Allows gradual migration path for legacy applications
Reference Implementation
This approach is based on the pattern used in the earlier feature/1_webapi2 branch (which supported net462), updated for the current codebase and net48.
Usage Example (Post-Implementation)
// In ASP.NET Web API 2 Controller
public class SyncController : ApiController
{
private readonly WebServerAgent webServerAgent;
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
return await webServerAgent.HandleRequestAsync(
this.Request,
this.Request.GetOwinContext().HttpContext as HttpContextBase,
cancellationToken: CancellationToken.None
);
}
}
Compatibility
- Maintains full backward compatibility with existing ASP.NET Core targets
- No changes required to existing ASP.NET Core applications
- Package will multi-target: netstandard2.0, net6.0, net8.0, and net48