Identifying and removing deprecated APIs
Outline
At a glance
- Deprecation occurs due to framework evolution, improved design, standardization, and security enhancements.
- Unaddressed deprecated code causes build failures, runtime errors, performance issues, and increased technical debt.
- Modern replacements include
appsettings.json, DI, middleware, and ASP.NET Core patterns.
This module focuses on a critical task during the migration from Optimizely CMS 11 to CMS 12: identifying and systematically removing deprecated APIs and outdated coding patterns. The transition to ASP.NET Core requires careful updates to prevent runtime errors, performance degradation, and security vulnerabilities.
Why Deprecation Occurs and Why it Matters
1. Reasons for Deprecation:
- Framework Evolution: ASP.NET Core introduces DI, middleware, and other paradigms.
- Improved Design and Performance: New APIs are cleaner, faster, and more secure.
- Standardization: Aligns with .NET standards and community best practices.
- Security Enhancements: Replace older APIs with more secure alternatives.
2. Impact of Unaddressed Deprecations:
- Build Failures: Breaking changes prevent compilation.
- Runtime Errors: Deprecated code may fail at runtime.
- Performance Degradation: Older patterns not optimized for ASP.NET Core.
- Increased Technical Debt: Harder to maintain and upgrade solutions.
- Security Risks: Known vulnerabilities may persist.
Common Deprecated APIs and Patterns (CMS 11 to CMS 12)
1. Configuration Management
-
<web.config> Sections:
-
Deprecated: Using
web.configfor settings. -
Replacement:
appsettings.json, environment variables,IConfiguration. -
Example: Move connection strings to
ConnectionStringsinappsettings.json.
-
Deprecated: Using
2. Dependency Injection and Service Location
-
ServiceLocator.Current:
-
Deprecated:
ServiceLocator.Currentfor service resolution. - Replacement: ASP.NET Core DI container and constructor injection.
- Example:
-
Deprecated:
// Deprecated (CMS 11)
// var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
// Modern (CMS 12)
public class MyService
{
private readonly IContentLoader _contentLoader;
public MyService(IContentLoader contentLoader) // Injected
{
_contentLoader = contentLoader;
}
// ... use _contentLoader
}
3. HTTP Context and Request Handling
-
System.Web.HttpContext.Current:
-
Deprecated: Static access to
HttpContext.Current. -
Replacement: Inject
IHttpContextAccessoror useHttpContextin controllers. - Example:
-
Deprecated: Static access to
// Deprecated (CMS 11)
// var currentUrl = HttpContext.Current.Request.Url;
// Modern (CMS 12) - via IHttpContextAccessor
public class MyHttpContextService
{
private readonly IHttpContextAccessor _httpContextAccessor;
public MyHttpContextService(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public Uri GetCurrentUrl()
{
return _httpContextAccessor.HttpContext?.Request.GetUri();
}
}
4. Routing and URL Management
-
Legacy Routing:
- Use Endpoint Routing (
MapContent(),MapControllerRoute()) instead of oldRouteCollection.
- Use Endpoint Routing (
-
URL Rewriting:
- Use ASP.NET Core URL Rewriting Middleware instead of
web.configrules.
- Use ASP.NET Core URL Rewriting Middleware instead of
5. Optimizely-Specific APIs
-
IInitializableModule: Methods now receive
InitializationEngine,IServiceCollection, orIApplicationBuilderfor ASP.NET Core startup. -
Content Loading & Saving: Some
IContentLoaderandIContentRepositorymethods may have changed; check documentation. - Event Handling: Updated to match ASP.NET Core event model.
-
UI Components: Legacy controls (
System.Web.UI) may need reimplementation.
Tools and Techniques for Identification
- Compiler Warnings: Observe warnings after upgrading packages.
- Static Analysis: Roslyn analyzers, ReSharper/Rider, and .NET Upgrade Assistant.
- Manual Review: Search deprecated namespaces, consult migration guides and community forums.
Strategies for Removal and Replacement
- Prioritize: Fix breaking changes first, then address less critical deprecated code.
- Find Modern Equivalents: Use official docs, extension methods, or middleware replacements.
- Incremental Refactoring: Refactor feature by feature with tests in place to avoid regressions.
Conclusion
Identifying and removing deprecated APIs is essential for a successful migration to Optimizely CMS 12 and Commerce 14. This process reduces technical debt, improves performance, and aligns the solution with modern .NET practices.
