Replacing NotNullAttribute with thrown exceptions

This commit is contained in:
Pranav K 2015-09-14 10:42:16 -07:00
parent 3e6585dcc8
commit 896c146e24
14 changed files with 169 additions and 46 deletions

View File

@ -3,7 +3,6 @@
using System; using System;
using System.IO; using System.IO;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting namespace Microsoft.AspNet.Hosting
{ {
@ -14,8 +13,13 @@ namespace Microsoft.AspNet.Hosting
/// </summary> /// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param> /// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param>
/// <returns>True if the environment name is Development, otherwise false.</returns> /// <returns>True if the environment name is Development, otherwise false.</returns>
public static bool IsDevelopment([NotNull]this IHostingEnvironment hostingEnvironment) public static bool IsDevelopment(this IHostingEnvironment hostingEnvironment)
{ {
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return hostingEnvironment.IsEnvironment(EnvironmentName.Development); return hostingEnvironment.IsEnvironment(EnvironmentName.Development);
} }
@ -24,8 +28,13 @@ namespace Microsoft.AspNet.Hosting
/// </summary> /// </summary>
/// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param> /// <param name="hostingEnvironment">An instance of <see cref="IHostingEnvironment"/> service.</param>
/// <returns>True if the environment name is Production, otherwise false.</returns> /// <returns>True if the environment name is Production, otherwise false.</returns>
public static bool IsProduction([NotNull]this IHostingEnvironment hostingEnvironment) public static bool IsProduction(this IHostingEnvironment hostingEnvironment)
{ {
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return hostingEnvironment.IsEnvironment(EnvironmentName.Production); return hostingEnvironment.IsEnvironment(EnvironmentName.Production);
} }
@ -36,9 +45,14 @@ namespace Microsoft.AspNet.Hosting
/// <param name="environmentName">Environment name to validate against.</param> /// <param name="environmentName">Environment name to validate against.</param>
/// <returns>True if the specified name is same as the current environment.</returns> /// <returns>True if the specified name is same as the current environment.</returns>
public static bool IsEnvironment( public static bool IsEnvironment(
[NotNull]this IHostingEnvironment hostingEnvironment, this IHostingEnvironment hostingEnvironment,
string environmentName) string environmentName)
{ {
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
return string.Equals( return string.Equals(
hostingEnvironment.EnvironmentName, hostingEnvironment.EnvironmentName,
environmentName, environmentName,
@ -52,9 +66,14 @@ namespace Microsoft.AspNet.Hosting
/// <param name="virtualPath">Path relative to the root.</param> /// <param name="virtualPath">Path relative to the root.</param>
/// <returns>Physical path corresponding to virtual path.</returns> /// <returns>Physical path corresponding to virtual path.</returns>
public static string MapPath( public static string MapPath(
[NotNull]this IHostingEnvironment hostingEnvironment, this IHostingEnvironment hostingEnvironment,
string virtualPath) string virtualPath)
{ {
if (hostingEnvironment == null)
{
throw new ArgumentNullException(nameof(hostingEnvironment));
}
if (virtualPath == null) if (virtualPath == null)
{ {
return hostingEnvironment.WebRootPath; return hostingEnvironment.WebRootPath;

View File

@ -7,8 +7,7 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*", "Microsoft.AspNet.Http.Abstractions": "1.0.0-*",
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*", "Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*"
"Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" }
}, },
"frameworks": { "frameworks": {
"dnx451": {}, "dnx451": {},

View File

@ -14,7 +14,6 @@ using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Internal; using Microsoft.AspNet.Http.Features.Internal;
using Microsoft.Framework.Configuration; using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting.Internal namespace Microsoft.AspNet.Hosting.Internal
@ -39,10 +38,25 @@ namespace Microsoft.AspNet.Hosting.Internal
private IFeatureCollection _serverInstance; private IFeatureCollection _serverInstance;
public HostingEngine( public HostingEngine(
[NotNull] IServiceCollection appServices, IServiceCollection appServices,
[NotNull] IStartupLoader startupLoader, IStartupLoader startupLoader,
[NotNull] IConfiguration config) IConfiguration config)
{ {
if (appServices == null)
{
throw new ArgumentNullException(nameof(appServices));
}
if (startupLoader == null)
{
throw new ArgumentNullException(nameof(startupLoader));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
_config = config; _config = config;
_applicationServiceCollection = appServices; _applicationServiceCollection = appServices;
_startupLoader = startupLoader; _startupLoader = startupLoader;

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder; using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Internal namespace Microsoft.AspNet.Hosting.Internal
{ {
@ -15,14 +14,29 @@ namespace Microsoft.AspNet.Hosting.Internal
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly IServiceProvider _services; private readonly IServiceProvider _services;
public RequestServicesContainerMiddleware([NotNull] RequestDelegate next, [NotNull] IServiceProvider services) public RequestServicesContainerMiddleware(RequestDelegate next, IServiceProvider services)
{ {
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
_services = services; _services = services;
_next = next; _next = next;
} }
public async Task Invoke([NotNull] HttpContext httpContext) public async Task Invoke(HttpContext httpContext)
{ {
if (httpContext == null)
{
throw new ArgumentNullException(nameof(httpContext));
}
// All done if there request services is set // All done if there request services is set
if (httpContext.RequestServices != null) if (httpContext.RequestServices != null)
{ {

View File

@ -5,7 +5,6 @@ using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Server namespace Microsoft.AspNet.Hosting.Server
{ {
@ -18,8 +17,13 @@ namespace Microsoft.AspNet.Hosting.Server
_services = services; _services = services;
} }
public IServerFactory LoadServerFactory([NotNull] string assemblyName) public IServerFactory LoadServerFactory(string assemblyName)
{ {
if (assemblyName == null)
{
throw new ArgumentNullException(nameof(assemblyName));
}
if (string.IsNullOrEmpty(assemblyName)) if (string.IsNullOrEmpty(assemblyName))
{ {
throw new ArgumentException(string.Empty, nameof(assemblyName)); throw new ArgumentException(string.Empty, nameof(assemblyName));

View File

@ -5,14 +5,18 @@ using System;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Startup namespace Microsoft.AspNet.Hosting.Startup
{ {
public class ConfigureServicesBuilder public class ConfigureServicesBuilder
{ {
public ConfigureServicesBuilder([NotNull] MethodInfo configureServices) public ConfigureServicesBuilder(MethodInfo configureServices)
{ {
if (configureServices == null)
{
throw new ArgumentNullException(nameof(configureServices));
}
// Only support IServiceCollection parameters // Only support IServiceCollection parameters
var parameters = configureServices.GetParameters(); var parameters = configureServices.GetParameters();
if (parameters.Length > 1 || if (parameters.Length > 1 ||
@ -28,8 +32,13 @@ namespace Microsoft.AspNet.Hosting.Startup
public Func<IServiceCollection, IServiceProvider> Build(object instance) => services => Invoke(instance, services); public Func<IServiceCollection, IServiceProvider> Build(object instance) => services => Invoke(instance, services);
private IServiceProvider Invoke(object instance, [NotNull] IServiceCollection exportServices) private IServiceProvider Invoke(object instance, IServiceCollection exportServices)
{ {
if (exportServices == null)
{
throw new ArgumentNullException(nameof(exportServices));
}
var parameters = new object[MethodInfo.GetParameters().Length]; var parameters = new object[MethodInfo.GetParameters().Length];
// Ctor ensures we have at most one IServiceCollection parameter // Ctor ensures we have at most one IServiceCollection parameter

View File

@ -12,7 +12,6 @@ using Microsoft.AspNet.Http.Internal;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Configuration; using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection; using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging; using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting namespace Microsoft.AspNet.Hosting
@ -44,13 +43,23 @@ namespace Microsoft.AspNet.Hosting
private string _serverFactoryLocation; private string _serverFactoryLocation;
private IServerFactory _serverFactory; private IServerFactory _serverFactory;
public WebHostBuilder([NotNull] IServiceProvider services) public WebHostBuilder(IServiceProvider services)
: this(services, config: new ConfigurationBuilder().Build()) : this(services, config: new ConfigurationBuilder().Build())
{ {
} }
public WebHostBuilder([NotNull] IServiceProvider services, [NotNull] IConfiguration config) public WebHostBuilder(IServiceProvider services, IConfiguration config)
{ {
if (services == null)
{
throw new ArgumentNullException(nameof(services));
}
if (config == null)
{
throw new ArgumentNullException(nameof(config));
}
_hostingEnvironment = new HostingEnvironment(); _hostingEnvironment = new HostingEnvironment();
_loggerFactory = new LoggerFactory(); _loggerFactory = new LoggerFactory();
_services = services; _services = services;
@ -124,14 +133,24 @@ namespace Microsoft.AspNet.Hosting
return this; return this;
} }
public WebHostBuilder UseEnvironment([NotNull] string environment) public WebHostBuilder UseEnvironment(string environment)
{ {
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
_hostingEnvironment.EnvironmentName = environment; _hostingEnvironment.EnvironmentName = environment;
return this; return this;
} }
public WebHostBuilder UseServer([NotNull] string assemblyName) public WebHostBuilder UseServer(string assemblyName)
{ {
if (assemblyName == null)
{
throw new ArgumentNullException(nameof(assemblyName));
}
_serverFactoryLocation = assemblyName; _serverFactoryLocation = assemblyName;
return this; return this;
} }
@ -142,22 +161,24 @@ namespace Microsoft.AspNet.Hosting
return this; return this;
} }
public WebHostBuilder UseStartup([NotNull] string startupAssemblyName) public WebHostBuilder UseStartup(string startupAssemblyName)
{ {
if (startupAssemblyName == null) if (startupAssemblyName == null)
{ {
throw new ArgumentNullException(nameof(startupAssemblyName)); throw new ArgumentNullException(nameof(startupAssemblyName));
} }
_startupAssemblyName = startupAssemblyName; _startupAssemblyName = startupAssemblyName;
return this; return this;
} }
public WebHostBuilder UseStartup([NotNull] Type startupType) public WebHostBuilder UseStartup(Type startupType)
{ {
if (startupType == null) if (startupType == null)
{ {
throw new ArgumentNullException(nameof(startupType)); throw new ArgumentNullException(nameof(startupType));
} }
_startupType = startupType; _startupType = startupType;
return this; return this;
} }
@ -167,19 +188,34 @@ namespace Microsoft.AspNet.Hosting
return UseStartup(typeof(TStartup)); return UseStartup(typeof(TStartup));
} }
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp) public WebHostBuilder UseStartup(Action<IApplicationBuilder> configureApp)
{ {
if (configureApp == null)
{
throw new ArgumentNullException(nameof(configureApp));
}
return UseStartup(configureApp, configureServices: null); return UseStartup(configureApp, configureServices: null);
} }
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp, Func<IServiceCollection, IServiceProvider> configureServices) public WebHostBuilder UseStartup(Action<IApplicationBuilder> configureApp, Func<IServiceCollection, IServiceProvider> configureServices)
{ {
if (configureApp == null)
{
throw new ArgumentNullException(nameof(configureApp));
}
_startup = new StartupMethods(configureApp, configureServices); _startup = new StartupMethods(configureApp, configureServices);
return this; return this;
} }
public WebHostBuilder UseStartup([NotNull] Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices) public WebHostBuilder UseStartup(Action<IApplicationBuilder> configureApp, Action<IServiceCollection> configureServices)
{ {
if (configureApp == null)
{
throw new ArgumentNullException(nameof(configureApp));
}
_startup = new StartupMethods(configureApp, _startup = new StartupMethods(configureApp,
services => services =>
{ {

View File

@ -19,7 +19,6 @@
"Microsoft.Framework.DependencyInjection": "1.0.0-*", "Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Logging": "1.0.0-*", "Microsoft.Framework.Logging": "1.0.0-*",
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*", "Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Sources": { "type": "build", "version": "1.0.0-*" },
"Newtonsoft.Json": "6.0.6" "Newtonsoft.Json": "6.0.6"
}, },
"frameworks": { "frameworks": {

View File

@ -14,7 +14,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Http.Internal;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost namespace Microsoft.AspNet.TestHost
{ {
@ -31,8 +30,13 @@ namespace Microsoft.AspNet.TestHost
/// Create a new handler. /// Create a new handler.
/// </summary> /// </summary>
/// <param name="next">The pipeline entry point.</param> /// <param name="next">The pipeline entry point.</param>
public ClientHandler([NotNull] Func<IFeatureCollection, Task> next, PathString pathBase) public ClientHandler(Func<IFeatureCollection, Task> next, PathString pathBase)
{ {
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
_next = next; _next = next;
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash. // PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
@ -51,9 +55,14 @@ namespace Microsoft.AspNet.TestHost
/// <param name="cancellationToken"></param> /// <param name="cancellationToken"></param>
/// <returns></returns> /// <returns></returns>
protected override async Task<HttpResponseMessage> SendAsync( protected override async Task<HttpResponseMessage> SendAsync(
[NotNull] HttpRequestMessage request, HttpRequestMessage request,
CancellationToken cancellationToken) CancellationToken cancellationToken)
{ {
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var state = new RequestState(request, _pathBase); var state = new RequestState(request, _pathBase);
var requestContent = request.Content ?? new StreamContent(Stream.Null); var requestContent = request.Content ?? new StreamContent(Stream.Null);
var body = await requestContent.ReadAsStreamAsync(); var body = await requestContent.ReadAsStreamAsync();

View File

@ -3,11 +3,9 @@
using System; using System;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO; using System.IO;
using System.Net.Http; using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost namespace Microsoft.AspNet.TestHost
{ {
@ -27,8 +25,13 @@ namespace Microsoft.AspNet.TestHost
/// <param name="server"></param> /// <param name="server"></param>
/// <param name="path"></param> /// <param name="path"></param>
[SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "Not a full URI")] [SuppressMessage("Microsoft.Usage", "CA2234:PassSystemUriObjectsInsteadOfStrings", Justification = "Not a full URI")]
public RequestBuilder([NotNull] TestServer server, string path) public RequestBuilder(TestServer server, string path)
{ {
if (server == null)
{
throw new ArgumentNullException(nameof(server));
}
_server = server; _server = server;
_req = new HttpRequestMessage(HttpMethod.Get, path); _req = new HttpRequestMessage(HttpMethod.Get, path);
} }
@ -38,8 +41,13 @@ namespace Microsoft.AspNet.TestHost
/// </summary> /// </summary>
/// <param name="configure"></param> /// <param name="configure"></param>
/// <returns></returns> /// <returns></returns>
public RequestBuilder And([NotNull] Action<HttpRequestMessage> configure) public RequestBuilder And(Action<HttpRequestMessage> configure)
{ {
if (configure == null)
{
throw new ArgumentNullException(nameof(configure));
}
configure(_req); configure(_req);
return this; return this;
} }

View File

@ -8,7 +8,6 @@ using System.Diagnostics.Contracts;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost namespace Microsoft.AspNet.TestHost
{ {
@ -30,8 +29,18 @@ namespace Microsoft.AspNet.TestHost
private bool _firstWrite; private bool _firstWrite;
private Action _abortRequest; private Action _abortRequest;
internal ResponseStream([NotNull] Action onFirstWrite, [NotNull] Action abortRequest) internal ResponseStream(Action onFirstWrite, Action abortRequest)
{ {
if (onFirstWrite == null)
{
throw new ArgumentNullException(nameof(onFirstWrite));
}
if (abortRequest == null)
{
throw new ArgumentNullException(nameof(abortRequest));
}
_onFirstWrite = onFirstWrite; _onFirstWrite = onFirstWrite;
_firstWrite = true; _firstWrite = true;
_abortRequest = abortRequest; _abortRequest = abortRequest;

View File

@ -1,4 +1,4 @@
// 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 System;
@ -11,7 +11,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http; using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal; using Microsoft.AspNet.Http.Internal;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost namespace Microsoft.AspNet.TestHost
{ {
@ -20,8 +19,13 @@ namespace Microsoft.AspNet.TestHost
private readonly Func<IFeatureCollection, Task> _next; private readonly Func<IFeatureCollection, Task> _next;
private readonly PathString _pathBase; private readonly PathString _pathBase;
internal WebSocketClient([NotNull] Func<IFeatureCollection, Task> next, PathString pathBase) internal WebSocketClient(Func<IFeatureCollection, Task> next, PathString pathBase)
{ {
if (next == null)
{
throw new ArgumentNullException(nameof(next));
}
_next = next; _next = next;
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash. // PathString.StartsWithSegments that we use below requires the base path to not end in a slash.

View File

@ -6,8 +6,7 @@
"url": "git://github.com/aspnet/hosting" "url": "git://github.com/aspnet/hosting"
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Hosting": "1.0.0-*", "Microsoft.AspNet.Hosting": "1.0.0-*"
"Microsoft.Framework.NotNullAttribute.Sources": { "version": "1.0.0-*", "type": "build" }
}, },
"frameworks": { "frameworks": {
"dnx451": { "dnx451": {

View File

@ -413,7 +413,7 @@ namespace Microsoft.AspNet.Hosting
private class ReadOnlyFeatureCollection : IFeatureCollection private class ReadOnlyFeatureCollection : IFeatureCollection
{ {
public object this[[NotNull] Type key] public object this[Type key]
{ {
get { return null; } get { return null; }
set { throw new NotSupportedException(); } set { throw new NotSupportedException(); }