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

View File

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

View File

@ -14,7 +14,6 @@ using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Features.Internal;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting.Internal
@ -39,10 +38,25 @@ namespace Microsoft.AspNet.Hosting.Internal
private IFeatureCollection _serverInstance;
public HostingEngine(
[NotNull] IServiceCollection appServices,
[NotNull] IStartupLoader startupLoader,
[NotNull] IConfiguration config)
IServiceCollection appServices,
IStartupLoader startupLoader,
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;
_applicationServiceCollection = appServices;
_startupLoader = startupLoader;

View File

@ -6,7 +6,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Internal
{
@ -15,14 +14,29 @@ namespace Microsoft.AspNet.Hosting.Internal
private readonly RequestDelegate _next;
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;
_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
if (httpContext.RequestServices != null)
{

View File

@ -5,7 +5,6 @@ using System;
using System.Linq;
using System.Reflection;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Server
{
@ -18,8 +17,13 @@ namespace Microsoft.AspNet.Hosting.Server
_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))
{
throw new ArgumentException(string.Empty, nameof(assemblyName));

View File

@ -5,14 +5,18 @@ using System;
using System.Linq;
using System.Reflection;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.Hosting.Startup
{
public class ConfigureServicesBuilder
{
public ConfigureServicesBuilder([NotNull] MethodInfo configureServices)
public ConfigureServicesBuilder(MethodInfo configureServices)
{
if (configureServices == null)
{
throw new ArgumentNullException(nameof(configureServices));
}
// Only support IServiceCollection parameters
var parameters = configureServices.GetParameters();
if (parameters.Length > 1 ||
@ -28,8 +32,13 @@ namespace Microsoft.AspNet.Hosting.Startup
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];
// 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.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Internal;
using Microsoft.Framework.Logging;
namespace Microsoft.AspNet.Hosting
@ -44,13 +43,23 @@ namespace Microsoft.AspNet.Hosting
private string _serverFactoryLocation;
private IServerFactory _serverFactory;
public WebHostBuilder([NotNull] IServiceProvider services)
public WebHostBuilder(IServiceProvider services)
: 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();
_loggerFactory = new LoggerFactory();
_services = services;
@ -124,14 +133,24 @@ namespace Microsoft.AspNet.Hosting
return this;
}
public WebHostBuilder UseEnvironment([NotNull] string environment)
public WebHostBuilder UseEnvironment(string environment)
{
if (environment == null)
{
throw new ArgumentNullException(nameof(environment));
}
_hostingEnvironment.EnvironmentName = environment;
return this;
}
public WebHostBuilder UseServer([NotNull] string assemblyName)
public WebHostBuilder UseServer(string assemblyName)
{
if (assemblyName == null)
{
throw new ArgumentNullException(nameof(assemblyName));
}
_serverFactoryLocation = assemblyName;
return this;
}
@ -142,22 +161,24 @@ namespace Microsoft.AspNet.Hosting
return this;
}
public WebHostBuilder UseStartup([NotNull] string startupAssemblyName)
public WebHostBuilder UseStartup(string startupAssemblyName)
{
if (startupAssemblyName == null)
{
throw new ArgumentNullException(nameof(startupAssemblyName));
}
_startupAssemblyName = startupAssemblyName;
return this;
}
public WebHostBuilder UseStartup([NotNull] Type startupType)
public WebHostBuilder UseStartup(Type startupType)
{
if (startupType == null)
{
throw new ArgumentNullException(nameof(startupType));
}
_startupType = startupType;
return this;
}
@ -167,19 +188,34 @@ namespace Microsoft.AspNet.Hosting
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);
}
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);
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,
services =>
{

View File

@ -19,7 +19,6 @@
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Logging": "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"
},
"frameworks": {

View File

@ -14,7 +14,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Internal;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost
{
@ -31,8 +30,13 @@ namespace Microsoft.AspNet.TestHost
/// Create a new handler.
/// </summary>
/// <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;
// 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>
/// <returns></returns>
protected override async Task<HttpResponseMessage> SendAsync(
[NotNull] HttpRequestMessage request,
HttpRequestMessage request,
CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException(nameof(request));
}
var state = new RequestState(request, _pathBase);
var requestContent = request.Content ?? new StreamContent(Stream.Null);
var body = await requestContent.ReadAsStreamAsync();

View File

@ -3,11 +3,9 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost
{
@ -27,8 +25,13 @@ namespace Microsoft.AspNet.TestHost
/// <param name="server"></param>
/// <param name="path"></param>
[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;
_req = new HttpRequestMessage(HttpMethod.Get, path);
}
@ -38,8 +41,13 @@ namespace Microsoft.AspNet.TestHost
/// </summary>
/// <param name="configure"></param>
/// <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);
return this;
}

View File

@ -8,7 +8,6 @@ using System.Diagnostics.Contracts;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost
{
@ -30,8 +29,18 @@ namespace Microsoft.AspNet.TestHost
private bool _firstWrite;
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;
_firstWrite = true;
_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.
using System;
@ -11,7 +11,6 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Internal;
using Microsoft.Framework.Internal;
namespace Microsoft.AspNet.TestHost
{
@ -20,10 +19,15 @@ namespace Microsoft.AspNet.TestHost
private readonly Func<IFeatureCollection, Task> _next;
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;
// PathString.StartsWithSegments that we use below requires the base path to not end in a slash.
if (pathBase.HasValue && pathBase.Value.EndsWith("/"))
{

View File

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

View File

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