diff --git a/src/Microsoft.AspNet.Session/DistributedSession.cs b/src/Microsoft.AspNet.Session/DistributedSession.cs index 2f5b5e6774..134dfcbe47 100644 --- a/src/Microsoft.AspNet.Session/DistributedSession.cs +++ b/src/Microsoft.AspNet.Session/DistributedSession.cs @@ -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 tryEstablishSession, [NotNull] ILoggerFactory loggerFactory, bool isNewSessionKey) + public DistributedSession( + IDistributedCache cache, + string sessionId, + TimeSpan idleTimeout, + Func 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) { diff --git a/src/Microsoft.AspNet.Session/DistributedSessionStore.cs b/src/Microsoft.AspNet.Session/DistributedSessionStore.cs index f6a0866de9..27b3b88372 100644 --- a/src/Microsoft.AspNet.Session/DistributedSessionStore.cs +++ b/src/Microsoft.AspNet.Session/DistributedSessionStore.cs @@ -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 tryEstablishSession, bool isNewSessionKey) + public ISession Create(string sessionId, TimeSpan idleTimeout, Func 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); } } diff --git a/src/Microsoft.AspNet.Session/Properties/Resources.Designer.cs b/src/Microsoft.AspNet.Session/Properties/Resources.Designer.cs index e92a12995b..4ee53d8b6c 100644 --- a/src/Microsoft.AspNet.Session/Properties/Resources.Designer.cs +++ b/src/Microsoft.AspNet.Session/Properties/Resources.Designer.cs @@ -17,7 +17,7 @@ namespace Microsoft.AspNet.Session { get { return GetString("Exception_KeyLengthIsExceeded"); } } - + /// /// The key cannot be longer than '{0}' when encoded with UTF-8. /// @@ -33,7 +33,7 @@ namespace Microsoft.AspNet.Session { get { return GetString("Exception_InvalidSessionEstablishment"); } } - + /// /// The session cannot be established after the response has started. /// @@ -49,7 +49,7 @@ namespace Microsoft.AspNet.Session { get { return GetString("Exception_InvalidToSerializeIn2Bytes"); } } - + /// /// The value cannot be serialized in two bytes. /// @@ -57,7 +57,7 @@ namespace Microsoft.AspNet.Session { return GetString("Exception_InvalidToSerializeIn2Bytes"); } - + /// /// The value cannot be serialized in three bytes. /// @@ -65,7 +65,7 @@ namespace Microsoft.AspNet.Session { get { return GetString("Exception_InvalidToSerializeIn3Bytes"); } } - + /// /// The value cannot be serialized in three bytes. /// @@ -73,7 +73,7 @@ namespace Microsoft.AspNet.Session { return GetString("Exception_InvalidToSerializeIn3Bytes"); } - + /// /// The value cannot be negative. /// @@ -81,7 +81,7 @@ namespace Microsoft.AspNet.Session { get { return GetString("Exception_NumberShouldNotBeNegative"); } } - + /// /// The value cannot be negative. /// @@ -89,7 +89,23 @@ namespace Microsoft.AspNet.Session { return GetString("Exception_NumberShouldNotBeNegative"); } - + + /// + /// Argument cannot be null or empty string. + /// + internal static string ArgumentCannotBeNullOrEmpty + { + get { return GetString("ArgumentCannotBeNullOrEmpty"); } + } + + /// + /// Argument cannot be null or empty string. + /// + 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; } } -} \ No newline at end of file +} diff --git a/src/Microsoft.AspNet.Session/Resources.resx b/src/Microsoft.AspNet.Session/Resources.resx index 8212053460..5bb1c8e0bb 100644 --- a/src/Microsoft.AspNet.Session/Resources.resx +++ b/src/Microsoft.AspNet.Session/Resources.resx @@ -132,4 +132,7 @@ The value cannot be negative. + + Argument cannot be null or empty string. + \ No newline at end of file diff --git a/src/Microsoft.AspNet.Session/SessionMiddleware.cs b/src/Microsoft.AspNet.Session/SessionMiddleware.cs index 65294a2841..1abdcf4db4 100644 --- a/src/Microsoft.AspNet.Session/SessionMiddleware.cs +++ b/src/Microsoft.AspNet.Session/SessionMiddleware.cs @@ -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 /// The representing the session store. /// The session configuration options. public SessionMiddleware( - [NotNull] RequestDelegate next, - [NotNull] ILoggerFactory loggerFactory, - [NotNull] ISessionStore sessionStore, - [NotNull] IOptions options) + RequestDelegate next, + ILoggerFactory loggerFactory, + ISessionStore sessionStore, + IOptions 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(); _options = options.Value; diff --git a/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs b/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs index d6f16d0dc7..96feb96721 100644 --- a/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs +++ b/src/Microsoft.AspNet.Session/SessionMiddlewareExtensions.cs @@ -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 /// /// The . /// The . - 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(); } } diff --git a/src/Microsoft.AspNet.Session/SessionServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Session/SessionServiceCollectionExtensions.cs index 31bd37ca8d..4f20017203 100644 --- a/src/Microsoft.AspNet.Session/SessionServiceCollectionExtensions.cs +++ b/src/Microsoft.AspNet.Session/SessionServiceCollectionExtensions.cs @@ -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 /// /// The to add the services to. /// The . - 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(); return services; @@ -30,8 +34,13 @@ namespace Microsoft.Extensions.DependencyInjection /// The to add the services to. /// The session options to configure the middleware with. /// The . - public static IServiceCollection AddSession([NotNull] this IServiceCollection services, Action configure) + public static IServiceCollection AddSession(this IServiceCollection services, Action configure) { + if (services == null) + { + throw new ArgumentNullException(nameof(services)); + } + services.Configure(configure); return services.AddSession(); } diff --git a/src/Microsoft.AspNet.Session/project.json b/src/Microsoft.AspNet.Session/project.json index cdd5db0e77..9058ae48f1 100644 --- a/src/Microsoft.AspNet.Session/project.json +++ b/src/Microsoft.AspNet.Session/project.json @@ -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": { },