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