dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

Problems with netstandard and taghelpers #1580

Closed dazinator closed 8 years ago

dazinator commented 8 years ago

I am having difficulty getting a custom TagHelper to compile (upgrading from dnxcore50) because it can't resolve the TagHelper library when targeting netstandard1.5 - here is the message:

image

SideNote: "you can use the navigation bar to switch context" - can someone explain to me where that nav bar is as that would be handy!

This is odd because the NuGet packages I am referencing both say they support netstandard1.5 on the NuGet page:

Here is my project.json file:

{
  "version": "2.17.4",
  "frameworks": {
    "net452": {
      "dependencies": {
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
        "Newtonsoft.Json": "4.0.1"
      }
    },
    "net40": {
      "dependencies": {
        "Common.Logging": "3.0.0",
        "Microsoft.Bcl.Async": "1.0.168",
        "Microsoft.Owin": "2.1.0",
        "WebActivatorEx": "2.0.0",
        "Newtonsoft.Json": "4.0.1"
      },
      "frameworkAssemblies": {
        "System.Web": "4.0.0.0",
        "System.Xml": "4.0.0.0"
      }
    },
    "netstandard1.5": {
      "imports": [
        "netcore50",
        "dnxcore50",
        "portable-net45+win8"
      ],
      "dependencies": {
        "NETStandard.Library": "1.5.0-rc2-24027"
      }
    }
  },
  "dependencies": {},
  "buildOptions": {
    "preserveCompilationContext": true,
    "copyToOutput": {
      "include": []
    }
  },
  "publishOptions": {
    "include": []
  },
  "tools": {}
}

And here is the full code for the class file:

#if !NET40

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.TagHelpers;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Razor.TagHelpers;

namespace JSNLog
{
    // HtmlTargetElementAttribute is needed because this tag helper will be used in other projects
    [HtmlTargetElement("jl-javascript-logger-definitions")]
    public class JlJavascriptLoggerDefinitionsTagHelper : TagHelper
    {
        // Can be passed via <jl-javascript-logger-definitions request-id="..." />. 
        // Pascal case gets translated into lower-kebab-case.
        public string RequestId { get; set; }

        private readonly IHttpContextAccessor _httpContextAccessor;

        public JlJavascriptLoggerDefinitionsTagHelper(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        public override Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            return base.ProcessAsync(context, output);
        }

        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            output.TagName = ""; // Remove the jl-javascript-logger-definitions tag completely

            HttpContext httpContext = _httpContextAccessor.HttpContext;
            string JSCode = httpContext.Configure(RequestId);

            output.Content.SetHtmlContent(JSCode);

            output.TagMode = TagMode.StartTagAndEndTag;
        }     
    }
}

#endif

Would really appreciate any guidance here.

dougbu commented 8 years ago

The top-level dependencies property should not be empty. Place the bits you have under the .NET 4.5.2 framework there:

      "dependencies": {
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
        "Newtonsoft.Json": "4.0.1"
      }
dazinator commented 8 years ago

Thanks @dougbu - I moved the 4.5.2 dependencies to top level, but as I also target framework net40 I quickly realised I can't do that as it reports those top level dependencies cannot be resolved for net40 which is correct.

So I moved the .net40 dependencies to top level instead, and i'm working through that at the moment, as some of those dependencies are not compatible with netStandard.

Why must there be top level dependencies and is this being fixed - because surely it's legitimate that you may not have any dependencies that are common to all the frameworks you choose target?

dazinator commented 8 years ago

I now have a top level dependency in my project.json and am able to successfully do a dotnet restore but I still have the same issue. Here is my revised project.json


{
  "version": "2.17.4",
  "frameworks": {
    "net452": {
      "dependencies": {
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
        "Newtonsoft.Json": "4.0.1"
      }
    },
    "net40": {
      "dependencies": {
        "Common.Logging": "3.0.0",
        "Microsoft.Owin": "2.1.0",
        "WebActivatorEx": "2.0.0",
        "Newtonsoft.Json": "4.0.1"
      },
      "frameworkAssemblies": {
        "System.Web": "4.0.0.0",
        "System.Xml": "4.0.0.0"
      }
    },
    "netstandard1.5": {
      "imports": [
        "netcore50",
        "dnxcore50",
        "portable-net45+win8"
      ],
      "dependencies": {
        "NETStandard.Library": "1.5.0-rc2-24027"
      }
    }
  },
  "dependencies": {
    "Microsoft.Bcl.Async": "1.0.168"
  },
  "buildOptions": {
    "preserveCompilationContext": true,
    "copyToOutput": {
      "include": [ ]
    }
  },
  "publishOptions": {
    "include": [ ]
  },
  "tools": { }
}

And error:

image

dougbu commented 8 years ago

It is indeed legitimate to leave the top-level dependencies property out or set it to an empty object. However you were attempting to use MVC packages in frameworks where MVC dependencies weren't referenced.

Remove your net40 framework to use MVC packages. Those packages require .NET Core (netstandard1.5) or .NET 4.5.1 or later.

dazinator commented 8 years ago

However you were attempting to use MVC packages in frameworks where MVC dependencies weren't referenced.

Doh! This led me to the answer. Removing net40 isn't an option for me as I need to support that at present. So I just needed to add the MVC packages to the netstandard framework also! ;)

Silly me!

Thanks for you help pointing me to the answer.

Final project.json:

{
  "version": "2.17.4",
  "frameworks": {
    "net452": {
      "dependencies": {
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
        "Newtonsoft.Json": "4.0.1"
      }
    },
    "net40": {
      "dependencies": {
        "Common.Logging": "3.0.0",
        "Microsoft.Owin": "2.1.0",
        "WebActivatorEx": "2.0.0",
        "Newtonsoft.Json": "4.0.1"
      },
      "frameworkAssemblies": {
        "System.Web": "4.0.0.0",
        "System.Xml": "4.0.0.0"
      }
    },
    "netstandard1.5": {
      "imports": [
        "netcore50",
        "dnxcore50",
        "portable-net45+win8"
      ],
      "dependencies": {
        "NETStandard.Library": "1.5.0-rc2-24027",
        "Microsoft.AspNetCore.Http.Abstractions": "1.0.0-rc2-final",
        "Microsoft.Extensions.Logging": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Http.Extensions": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.Razor": "1.0.0-rc2-final",
      }
    }
  },
  "dependencies": {
    "Microsoft.Bcl.Async": "1.0.168"
  },
  "buildOptions": {
    "preserveCompilationContext": true,
    "copyToOutput": {
      "include": [ ]
    }
  },
  "publishOptions": {
    "include": [ ]
  },
  "tools": { }
}