diff --git a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs index bf3113eadacf..6f9e1bf02280 100644 --- a/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs +++ b/src/DataProtection/DataProtection/src/TypeForwardingActivator.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; -using System.Text.RegularExpressions; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; @@ -13,7 +12,6 @@ internal class TypeForwardingActivator : SimpleActivator private const string OldNamespace = "Microsoft.AspNet.DataProtection"; private const string CurrentNamespace = "Microsoft.AspNetCore.DataProtection"; private readonly ILogger _logger; - private static readonly Regex _versionPattern = new Regex(@",\s?Version=[0-9]+(\.[0-9]+){0,3}", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); public TypeForwardingActivator(IServiceProvider services) : this(services, NullLoggerFactory.Instance) @@ -64,6 +62,27 @@ internal object CreateInstance(Type expectedBaseType, string originalTypeName, o } protected string RemoveVersionFromAssemblyName(string forwardedTypeName) - => _versionPattern.Replace(forwardedTypeName, ""); + { + // Type, Assembly, Version={Version}, Culture={Culture}, PublicKeyToken={Token} + + var versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal); + while (versionStartIndex != -1) + { + var versionEndIndex = forwardedTypeName.IndexOf(',', versionStartIndex + ", Version=".Length); + + if (versionEndIndex == -1) + { + // No end index, so are done and can remove the rest + return forwardedTypeName.Substring(0, versionStartIndex); + } + + forwardedTypeName = forwardedTypeName.Remove(versionStartIndex, versionEndIndex - versionStartIndex); + versionStartIndex = forwardedTypeName.IndexOf(", Version=", StringComparison.Ordinal); + } + + // No version left + return forwardedTypeName; + + } } }