Replace NotNullAttribute with thrown exceptions
This commit is contained in:
parent
b36d5663b2
commit
a6ac3f58f5
|
|
@ -9,7 +9,6 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.AspNet.Session
|
||||
|
|
@ -29,9 +28,34 @@ namespace Microsoft.AspNet.Session
|
|||
private bool _loaded;
|
||||
private bool _isNewSessionKey;
|
||||
|
||||
public DistributedSession([NotNull] IDistributedCache cache, [NotNull] string sessionId, TimeSpan idleTimeout,
|
||||
[NotNull] Func<bool> tryEstablishSession, [NotNull] ILoggerFactory loggerFactory, bool isNewSessionKey)
|
||||
public DistributedSession(
|
||||
IDistributedCache cache,
|
||||
string sessionId,
|
||||
TimeSpan idleTimeout,
|
||||
Func<bool> tryEstablishSession,
|
||||
ILoggerFactory loggerFactory,
|
||||
bool isNewSessionKey)
|
||||
{
|
||||
if (cache == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cache));
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(sessionId));
|
||||
}
|
||||
|
||||
if (tryEstablishSession == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tryEstablishSession));
|
||||
}
|
||||
|
||||
if (loggerFactory == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerFactory));
|
||||
}
|
||||
|
||||
_cache = cache;
|
||||
_sessionId = sessionId;
|
||||
_idleTimeout = idleTimeout;
|
||||
|
|
@ -56,8 +80,13 @@ namespace Microsoft.AspNet.Session
|
|||
return _store.TryGetValue(new EncodedKey(key), out value);
|
||||
}
|
||||
|
||||
public void Set(string key, [NotNull] byte[] value)
|
||||
public void Set(string key, byte[] value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(value));
|
||||
}
|
||||
|
||||
var encodedKey = new EncodedKey(key);
|
||||
if (encodedKey.KeyBytes.Length > KeyLengthLimit)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
using System;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.Extensions.Caching.Distributed;
|
||||
using Microsoft.Extensions.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Microsoft.AspNet.Session
|
||||
|
|
@ -14,8 +13,18 @@ namespace Microsoft.AspNet.Session
|
|||
private readonly IDistributedCache _cache;
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
public DistributedSessionStore([NotNull] IDistributedCache cache, [NotNull] ILoggerFactory loggerFactory)
|
||||
public DistributedSessionStore(IDistributedCache cache, ILoggerFactory loggerFactory)
|
||||
{
|
||||
if (cache == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cache));
|
||||
}
|
||||
|
||||
if (loggerFactory == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerFactory));
|
||||
}
|
||||
|
||||
_cache = cache;
|
||||
_loggerFactory = loggerFactory;
|
||||
}
|
||||
|
|
@ -33,8 +42,18 @@ namespace Microsoft.AspNet.Session
|
|||
_cache.Connect();
|
||||
}
|
||||
|
||||
public ISession Create([NotNull] string sessionId, TimeSpan idleTimeout, [NotNull] Func<bool> tryEstablishSession, bool isNewSessionKey)
|
||||
public ISession Create(string sessionId, TimeSpan idleTimeout, Func<bool> tryEstablishSession, bool isNewSessionKey)
|
||||
{
|
||||
if (string.IsNullOrEmpty(sessionId))
|
||||
{
|
||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(sessionId));
|
||||
}
|
||||
|
||||
if (tryEstablishSession == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(tryEstablishSession));
|
||||
}
|
||||
|
||||
return new DistributedSession(_cache, sessionId, idleTimeout, tryEstablishSession, _loggerFactory, isNewSessionKey);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
get { return GetString("Exception_KeyLengthIsExceeded"); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The key cannot be longer than '{0}' when encoded with UTF-8.
|
||||
/// </summary>
|
||||
|
|
@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
get { return GetString("Exception_InvalidSessionEstablishment"); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The session cannot be established after the response has started.
|
||||
/// </summary>
|
||||
|
|
@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
get { return GetString("Exception_InvalidToSerializeIn2Bytes"); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The value cannot be serialized in two bytes.
|
||||
/// </summary>
|
||||
|
|
@ -57,7 +57,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
return GetString("Exception_InvalidToSerializeIn2Bytes");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The value cannot be serialized in three bytes.
|
||||
/// </summary>
|
||||
|
|
@ -65,7 +65,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
get { return GetString("Exception_InvalidToSerializeIn3Bytes"); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The value cannot be serialized in three bytes.
|
||||
/// </summary>
|
||||
|
|
@ -73,7 +73,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
return GetString("Exception_InvalidToSerializeIn3Bytes");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The value cannot be negative.
|
||||
/// </summary>
|
||||
|
|
@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
get { return GetString("Exception_NumberShouldNotBeNegative"); }
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The value cannot be negative.
|
||||
/// </summary>
|
||||
|
|
@ -89,7 +89,23 @@ namespace Microsoft.AspNet.Session
|
|||
{
|
||||
return GetString("Exception_NumberShouldNotBeNegative");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Argument cannot be null or empty string.
|
||||
/// </summary>
|
||||
internal static string ArgumentCannotBeNullOrEmpty
|
||||
{
|
||||
get { return GetString("ArgumentCannotBeNullOrEmpty"); }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Argument cannot be null or empty string.
|
||||
/// </summary>
|
||||
internal static string FormatArgumentCannotBeNullOrEmpty()
|
||||
{
|
||||
return GetString("ArgumentCannotBeNullOrEmpty");
|
||||
}
|
||||
|
||||
private static string GetString(string name, params string[] formatterNames)
|
||||
{
|
||||
var value = _resourceManager.GetString(name);
|
||||
|
|
@ -107,4 +123,4 @@ namespace Microsoft.AspNet.Session
|
|||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,4 +132,7 @@
|
|||
<data name="Exception_NumberShouldNotBeNegative" xml:space="preserve">
|
||||
<value>The value cannot be negative.</value>
|
||||
</data>
|
||||
<data name="ArgumentCannotBeNullOrEmpty" xml:space="preserve">
|
||||
<value>Argument cannot be null or empty string.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
@ -7,7 +7,6 @@ using System.Threading.Tasks;
|
|||
using Microsoft.AspNet.Builder;
|
||||
using Microsoft.AspNet.Http;
|
||||
using Microsoft.AspNet.Http.Features;
|
||||
using Microsoft.Extensions.Internal;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.OptionsModel;
|
||||
|
||||
|
|
@ -34,11 +33,31 @@ namespace Microsoft.AspNet.Session
|
|||
/// <param name="sessionStore">The <see cref="ISessionStore"/> representing the session store.</param>
|
||||
/// <param name="options">The session configuration options.</param>
|
||||
public SessionMiddleware(
|
||||
[NotNull] RequestDelegate next,
|
||||
[NotNull] ILoggerFactory loggerFactory,
|
||||
[NotNull] ISessionStore sessionStore,
|
||||
[NotNull] IOptions<SessionOptions> options)
|
||||
RequestDelegate next,
|
||||
ILoggerFactory loggerFactory,
|
||||
ISessionStore sessionStore,
|
||||
IOptions<SessionOptions> options)
|
||||
{
|
||||
if (next == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(next));
|
||||
}
|
||||
|
||||
if (loggerFactory == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(loggerFactory));
|
||||
}
|
||||
|
||||
if (sessionStore == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(sessionStore));
|
||||
}
|
||||
|
||||
if (options == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(options));
|
||||
}
|
||||
|
||||
_next = next;
|
||||
_logger = loggerFactory.CreateLogger<SessionMiddleware>();
|
||||
_options = options.Value;
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.AspNet.Session;
|
||||
using Microsoft.Extensions.Internal;
|
||||
|
||||
namespace Microsoft.AspNet.Builder
|
||||
{
|
||||
|
|
@ -16,8 +16,13 @@ namespace Microsoft.AspNet.Builder
|
|||
/// </summary>
|
||||
/// <param name="app">The <see cref="IApplicationBuilder"/>.</param>
|
||||
/// <returns>The <see cref="IApplicationBuilder"/>.</returns>
|
||||
public static IApplicationBuilder UseSession([NotNull] this IApplicationBuilder app)
|
||||
public static IApplicationBuilder UseSession(this IApplicationBuilder app)
|
||||
{
|
||||
if (app == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(app));
|
||||
}
|
||||
|
||||
return app.UseMiddleware<SessionMiddleware>();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
|
||||
using System;
|
||||
using Microsoft.AspNet.Session;
|
||||
using Microsoft.Extensions.Internal;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection
|
||||
{
|
||||
|
|
@ -17,8 +16,13 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
/// </summary>
|
||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
||||
/// <returns>The <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddSession([NotNull] this IServiceCollection services)
|
||||
public static IServiceCollection AddSession(this IServiceCollection services)
|
||||
{
|
||||
if (services == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(services));
|
||||
}
|
||||
|
||||
services.AddOptions();
|
||||
services.AddTransient<ISessionStore, DistributedSessionStore>();
|
||||
return services;
|
||||
|
|
@ -30,8 +34,13 @@ namespace Microsoft.Extensions.DependencyInjection
|
|||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
||||
/// <param name="configure">The session options to configure the middleware with.</param>
|
||||
/// <returns>The <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddSession([NotNull] this IServiceCollection services, Action<SessionOptions> configure)
|
||||
public static IServiceCollection AddSession(this IServiceCollection services, Action<SessionOptions> configure)
|
||||
{
|
||||
if (services == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(services));
|
||||
}
|
||||
|
||||
services.Configure(configure);
|
||||
return services.AddSession();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,11 +9,11 @@
|
|||
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Extensions.Caching.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Extensions.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" },
|
||||
"Microsoft.Extensions.OptionsModel": "1.0.0-*"
|
||||
},
|
||||
"compilationOptions": {
|
||||
"allowUnsafe": true
|
||||
"allowUnsafe": true,
|
||||
"warningsAsErrors": true
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": { },
|
||||
|
|
|
|||
Loading…
Reference in New Issue