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.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
  • I've found the exact line where the error is being thrown and it looks like there are no startup filters being found. https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs#L212 – Nkosi Feb 06 '18 at 11:46
  • @Nkosi how can i solve it. please help me. – Harshad Pansuriya Feb 06 '18 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. – Nkosi Feb 06 '18 at 11:51
  • @Nkosi I am working on it. – Harshad Pansuriya Feb 06 '18 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. – Nkosi Feb 06 '18 at 12:28
  • @Nkosi If I am using built in DI then it working fine and run. But I want to implement it with Unity because for the future use and large project i want loose coupling. – Harshad Pansuriya Feb 06 '18 at 12:36
  • No, just wanted you to check and make sure that it was unity causing the problem. – Nkosi Feb 06 '18 at 12:38
  • @Nkosi yes I try first without Unity and then I try with Unity. See my edit question. – Harshad Pansuriya Feb 06 '18 at 12:39
  • Well then there is your problem, you did not have any startup filters in the original example. Which is what was null. – Nkosi Feb 06 '18 at 12:51
  • @Nkosi This is working fine in Windows but only the problem is with the Mac – Harshad Pansuriya Feb 07 '18 at 03:53

1 Answers1

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.

Elan Hasson
  • 1,214
  • 1
  • 19
  • 29
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95