Search code examples
c#.netasp.net-corednxcoreclr

ASP.Net DNX Core CLR issue with StructureMap.Dnx: The type 'Object' is defined in an assembly that is not referenced


I'm trying out the new Core CLR type ASP.Net on my Mac and running in to no end of problems.

I've managed to get StructureMap referenced and installed via dnu and dnu restore, but now I'm getting errors in VS Code stating that:

The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes'. [dnx451]

I've done a good amount of googling but all I can find are things stating I need to add a using for System, that doesn't fix the problem.

Startup.cs:

using System;
using System.Reflection;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Extensions.DependencyInjection;
using StructureMap;

namespace Authorization
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            var container = new Container();
            container.Configure(x => {
                x.Scan(scanning => {
                   scanning.Assembly(Assembly.GetExecutingAssembly());
                   scanning.TheCallingAssembly();
                   scanning.WithDefaultConventions(); 
                });
            });

            container.Populate(services);

            return container.GetInstance<IServiceCollection>(services);
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

        // Entry point for the application.
        public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
    }
}

project.json:

{
    "version": "1.0.0-*",
    "compilationOptions": {
        "emitEntryPoint": true
    },
    "tooling": {
        "defaultNamespace": "Authorization"
    },

    "dependencies": {
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "StructureMap.Dnx": "0.4.0-alpha4"
    },

    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel"
    },

    "frameworks": {
        "dnx451": {},
        "dnxcore50": {}
    },

    "exclude": [
        "wwwroot",
        "node_modules"
    ],
    "publishExclude": [
        "**.user",
        "**.vspscc"
    ]
}

Any help gratefully received!

Thanks


Solution

  • I tried a little and can suggest you two versions to fix the problem with compilation.

    The first way is removing dnxcore50 and making some changes in Startup.cs. To be exactly one can use the following project.json:

    {
      "version": "1.0.0-*",
      "compilationOptions": {
        "emitEntryPoint": true
      },
    
      "dependencies": {
        "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
        "structuremap": "4.0.1.318",
        "StructureMap.Dnx": "0.4.0-alpha4"
      },
    
      "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel"
      },
    
      "frameworks": {
        "dnx451": { }
      },
    
      "exclude": [
        "wwwroot",
        "node_modules"
      ],
      "publishExclude": [
        "**.user",
        "**.vspscc"
      ]
    }
    

    and the following Startup.cs:

    using System.Reflection;
    using Microsoft.AspNet.Builder;
    using Microsoft.AspNet.Http;
    using Microsoft.Extensions.DependencyInjection;
    using StructureMap;
    using StructureMap.Graph;
    
    namespace HelloWorldWebApplication
    {
        public class Startup
        {
            public IServiceCollection ConfigureServices(IServiceCollection services)
            {
                var container = new Container();
                container.Configure(x => {
                    x.Scan(scanning => {
                        scanning.Assembly(typeof(Startup).GetTypeInfo().Assembly);
                        scanning.TheCallingAssembly();
                        scanning.WithDefaultConventions();
                    });
                });
                container.Populate(services);
                return container.GetInstance<IServiceCollection>();
            }
    
            public void Configure(IApplicationBuilder app)
            {
                app.UseIISPlatformHandler();
                app.Run(async context => {
                    await context.Response.WriteAsync("Hello World!");
                });
            }
    
            public static void Main(string[] args) => Microsoft.AspNet.Hosting.WebApplication.Run<Startup>(args);
        }
    }
    

    Alternatively one can add back "dnxcore50": { } in the "frameworks" part, but comment the line scanning.TheCallingAssembly(); and using StructureMap.Graph;