3

I am creating web api in .net core. and I am using Mac Sierra 10.12.3 and visual studio for Mac version 7.3.3(build 12). For the reference I am using below Github project.

https://github.com/unitycontainer/examples

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
               .UseUnityServiceProvider()   // Add Unity as default Service Provider
               .UseStartup<Startup>()
               .Build();
}

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }


    public void ConfigureContainer(IUnityContainer container)
    {
        container.RegisterSingleton<IUserRepository, UserRepository>();

    }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddDbContext<ApplicationContext>(opts =>
               opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));


        // Add MVC as usual
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc();
    }
}

This link code work in Windows but not on Mac.

I am trying to run this project it give me below error

Application startup exception: System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Loaded '/usr/local/share/dotnet/shared/Microsoft.NETCore.App/2.0.5/System.Diagnostics.Debug.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. [41m[1m[37mcrit[39m[22m[49m: Microsoft.AspNetCore.Hosting.Internal.WebHost[6] Microsoft.AspNetCore.Hosting.Internal.WebHost:Critical: Application startup exception

System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable`1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Application startup exception System.ArgumentNullException: Value cannot be null. Parameter name: source at System.Linq.Enumerable.Reverse[TSource](IEnumerable'1 source) at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() Exception thrown: 'System.ArgumentNullException' in Microsoft.AspNetCore.Hosting.dll

EDIT :

program.cs

public class Program
    {
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration()
                .UseStartup<Startup>()
                .Build();

            host.Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

Start.cs

public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                 .SetBasePath(env.ContentRootPath)
                 .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                 .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                 .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<ApplicationContext>(opts =>
                    opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
            services.AddSingleton(typeof(IUserRepository<User, int>), typeof(UserRepository));
            services.AddMvc();
        }


        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
        }
    }

After using upper code and without unity than it's working fine.

Any help will be Appreciated.

10
  • I've found the exact line where the error is being thrown and it looks like there are no startup filters being found. github.com/aspnet/Hosting/blob/dev/src/… Commented Feb 6, 2018 at 11:46
  • @Nkosi how can i solve it. please help me. Commented Feb 6, 2018 at 11:50
  • I'm checking but you can also review the code as well. At least now you have a starting point to look from. Commented Feb 6, 2018 at 11:51
  • @Nkosi I am working on it. Commented Feb 6, 2018 at 11:53
  • Still walking through the code but here is a troubleshooting suggestion. remove the UseUnityServiceProvider and use the built in DI. Check that the project will start up without it. If it does then that confirms that the unity container is the cause of the problem and that it is not some other issue. If it still fails then the problem is not unity but some other issue with how the project is configured. Commented Feb 6, 2018 at 12:28

1 Answer 1

2

After lots of Research finally found the solution

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseUnityServiceProvider()   // Add Unity as default Service Provider
            .UseStartup<Startup>()
            .Build();
    }

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // Configure Unity container
        public void ConfigureContainer(IUnityContainer container)
        {
            container.RegisterSingleton<IUserRepository, UserRepository>();
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Container could be configured via services as well. 
            // Just be careful not to override registrations
            services.AddDbContext<ApplicationContext>(opts =>
                   opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
       
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAllOrigins",
                    builder =>
                    {
                        builder
                            .AllowAnyOrigin()
                            .AllowAnyHeader()
                            .AllowAnyMethod();
                    });
            });

            // Add MVC as usual
            services.AddMvcCore().AddJsonFormatters();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment()) {
                app.UseDeveloperExceptionPage();
            }

            app.UseMvc();
        }
    }

and after reading this blogpost I got the diffrence.

https://offering.solutions/blog/articles/2017/02/07/difference-between-addmvc-addmvcore/

Don't forget to add the dependency injection for Unity Nuget Unity.Microsoft.DependencyInjection

and Thanks to @Nkosi for the help and support.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.