cannot add reference to .net core Class library asp.net core rc2 - asp.net-core-1.0

I am a newbie in the asp.net net core world and I am struggling to adding a simple ref .
I get an error
Steps
1) Created an "Asp.net Core Web Application(Net Framework) RC2"
2) Added a Class Library (.Net core) called "ClassLibrary1")
3)Within the web app.Project.json i added a reference to the classlibrary1 like this
"dependencies": {
"ClassLibrary1": "1.0.0-*", etc...
4) Get error
Severity Code Description Project File Line Suppression State
Error
NU1001 The dependency ClassLibrary1 could not be resolved.
I understand why microsoft is doing this as they want to be lean and modular,however
there should be an option that would add the reference for you like in the classic library.It's a step back in my view.
Is this a bug or its me?
thanks for any reply

Change your project.json in your class library to .netstandard1.4 (or lower).
Your web application is stating .NET Framework 4.6.1, but netstandard 1.5 can only target 4.6.2+ (related to .NET Framework that is).
https://github.com/dotnet/standard/blob/master/docs/versions.md
{
"version": "1.0.0-*",
"dependencies": {
"NETStandard.Library": "1.5.0-rc2-24027"
},
"frameworks": {
"netstandard1.4": {
"imports": "dnxcore50"
}
}
}

I ran into the same Problem. I had to manually run "Restore Packages" and the error was gone!

Related

.NET 5 webapi example returning 404, running in Linux

I'm trying to use .NET 5.0 on Linux, using VS Code.
I've created an empty directory, ran "code ." in it to open it up in VS Code, then created the sample project with:
dotnet new webapi -n Experimenting
When the "Required assets to build and debug are missing from 'experiments'. Add them?" dialog appeared, I clicked "Yes".
I ran the build task, then selected "Start Debugging" from the "Run" menu.
The project appears to run, and it opened up a Chrome browser tab to https://localhost:5001/, and the tab displays HTTP ERROR 404.
The "Expementing" profile in lauchSettings.json has applicationUrl set to "https://localhost:5001;http://localhost:5000", and the debug console includes:
Now listening on: https://localhost:5001
Now listening on: http://localhost:5000
So the ports seem to be correct.
If I try to open a browser tab on http://localhost:5000, it redirects to https://localhost:5001, and then shows the 404.
What am I missing?
The comment by jmoerdyk is correct - if I try https://localhost:5001/swagger, I get the swagger page.
In .NET Framework, Web API creates a landing page at the domain root. .NET Core does not.
The question, then, is why is the browser tab opening up at https://localhost:5001, and not at https://localhost:5001/swagger?
I had noticed this in launchSettings.json:
"profiles": {
"IIS Express": {
...
},
"Experimenting": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
And I thought that should open up the browser pointing to the swagger page. Browsing around I've seen statements that that launchSettings.json is not used by VS Code, but it clearly is, because if I change ports in applicationUrl, I the swagger page running on the new ports.
But launchUrl seems to be ignored.
Browsing around the web gives me the same sort of wrong and outdated answers that is typical for .NET Core questions, but I eventually tried setting the uriFormat in launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
...
// Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
"serverReadyAction": {
"action": "openExternally",
"pattern": "\\bNow listening on:\\s+(https?://\\S+)",
"uriFormat": "%s/swagger"
},
...
},
]
}
The link in the comment explains how this is supposed to work - kinda.
https://aka.ms/VSCode-CS-LaunchJson-WebBrowser
The reason for 404 code that there is no defautl GET request for root as you figured out already.
Based on this record, it tells that launchSettings.json is only for Visual Studio debug. I tried project on Windows with VS and on Linux with dotnet CLI, I can confirm this experience.
Is launchSettings.json used when running ASP.NET 5 apps from the command line on Mac?
You can use a redirect as alternative way for route to get Swagger as default instead of launchSettings.json file. You can create this class (for example as RouteRedirectController.cs) onto Controllers folder and it will redirect to swagger in case of a root GET call:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace TestAPI.Controllers
{
[ApiController]
[Route("")]
public class RouteRedirectController : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return Redirect("~/swagger");
}
}
}
Update: To be honest, it is a bit strange because if I change "applicationUrl" in launchSettings.json file (not for IIS Express profile that is for Windows), then change is reflected after dotnet run command is executed. Ignore launchUrl maybe a bug during dotnet run? But if it does anyway, then it can be resolved by this redirect endpoint.

Azure function Publish not creating Startup class entry in extensions.json

We are developing a set of Azure functions for our new product. We are using dependency injection for our project following the strategy , mentioned in the following article
https://blog.wille-zone.de/post/azure-functions-dependency-injection/
and registered the extension in Startup.cs
This is working fine in the local development environment. However when we tried to publish it azure , the azure functions runtime was not detecting the injected dependencies . We found a similiar problem in the following sctak over flow.com post
IExtensionConfigProvider not initializing or binding with Microsoft.Azure.WebJobs v3
We made changes as per their recommendation in the accepted answer. As per this
"In C# the SDK 1.0.19 looks at build time for classes attributed with WebJobsStartup assembly attribute in the current function project or any dependency (ProjectReference or PackageReference) of the current project, and generates the corresponding extensions.json file."
However we ran in to strange problem, When we do a release build , the startup class entry is created in the extensions.json
{
"extensions":[
{ "name": "AzureStorage", "typeName":"Microsoft.Azure.WebJobs.Extensions.Storage.AzureStorageWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.Storage, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"},
{ "name": "Startup", "typeName":"AAA.BBBB.Functions.Startup, AAA.BBB.Functions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"}
]
}
however when we try to publish , the startup class entry is NOT created in the extensions.json
{
"extensions":[
{ "name": "AzureStorage", "typeName":"Microsoft.Azure.WebJobs.Extensions.Storage.AzureStorageWebJobsStartup, Microsoft.Azure.WebJobs.Extensions.Storage, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}
]
}
We are stumped and do not know what is the problem
Looks this this is a known issue. Refer the Github URL for more details -
https://github.com/Azure/Azure-Functions/issues/972#issuecomment-426708865
Currently proceeded in the workaround approach

NuGet Packages do not compile Azure CSX

I have included a NuGet Package in an Azure Function app that I downloaded to work on in Visual Studio. I have added it to the project.json and I still get "error CS0246: The type or namespace name 'NetTopologySuite' could not be found (are you missing a using directive or an assembly reference?)". I've read through microsoft's documentation and cannot find what I could be doing wrong.
Here is a sample of what my csx looks like:
#r "System.Data"
using System;
using System.Data;
using System.Data.SqlClient;
using System.Net;
using NetTopologySuite;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
\\ Code to retrieve data from database and turn it into an array
\\ of GeoJSON features called DataFromDatabase not shown
NetTopologySuite.Features.Feature[] TrailSegments = DataFromDatabase;
HttpResponseMessage resp = req.CreateResponse(HttpStatusCode.OK);
resp.Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(DataFromDatabase), System.Text.Encoding.UTF8, "application/json");
return resp;
}
Here is my project.json:
{
"frameworks": {
"net46": {
"dependencies": {
"NetTopologySuite.IO.GeoJSON": "1.14.0"
}
}
}
}
Does anyone have more experience with this that could offer a little more than what's in the documentation?
"FUNCTIONS_EXTENSION_VERSION": "~1"
"WEBSITE_NODE_DEFAULT_VERSION": "6.5.0"
If you do upload the project.json file to your function folder(not function app folder), what you have done is exactly right. I have followed your steps and things work fine on my side.
Nuget restoring for function editable online is not so sensitive, so you may wait for a while(you can do some edit in function code and click save or directly restart whole function app).
After that, you can see a project.lock.json under the function folder. It means the package has been installed successfully. Then everything goes well.
Update for multiple functions sharing reference.
One function package restore can't be used by others. So we have to upload dlls manually if you don't want to add project.json to every function. See shared assemblies.
Download NetTopologySuite.IO.GeoJSON.
Find four dlls(NetTopologySuite.dll/NetTopologySuite.IO.GeoJSON.dll/GeoAPI.dll/PowerCollections.dll) in package and upload them to a bin folder under function app folder.
Add four assemblies in code like #r "..\bin\NetTopologySuite.IO.GeoJSON.dll". You may also need add #r "Newtonsoft.Json" as it's one dependency in that package.
If you use the dll with namespace like NetTopologySuite.Features.Feature[], you don't have to import namespaces. And vice versa.
If you know those dependencies clearly, you can only upload and reference dlls you need.
I see that you are using 3rd party library which is widely available in Nuget official repository. In such cases, you need to let Azure know which Nuget repository your package, 'NetTopologySuite' resides in..
Github: https://github.com/NetTopologySuite/NetTopologySuite
NuGet v3: https://www.myget.org/F/nettopologysuite/api/v3/index.json
NuGet v2: https://www.myget.org/F/nettopologysuite/api/v2
Create Nuget.config file
Add the following contents in that file and re-configure it for your environment.
Nuget.config content - you can find exhaustive file online.

Azure function: Could not load file or assembly Microsoft.IdentityModel.Tokens, Version=5.2.1.0

Im writing an azure function to generate a JWT token and return it to the client. The code is tested locally in a console app and all seems to work fine. This is the package reference included in the working console app, and in my functions app:
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.2.1" />
When running the function host locally with func host start and executing the code it results in the error:
Could not load file or assembly 'Microsoft.IdentityModel.Tokens, Version=5.2.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'."
I don't understand why this is happening, the dll is laying in the output folder along with my application dll. The only other thing I can think of is that the function host has its own set of packages that it sources from and this one is not available yet, having been only released 12 days ago.
I'm not sure. Any help on why this is happening or how to get around it?
Details:
Azure Functions Core Tools (2.0.1-beta.22)
Function Runtime Version: 2.0.11415.0
I got this issue and it seems to be related to some kind of bug in the Azure function SDK. the fix was to add:
<_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>
to your csproj file. As documented here
I had installed this package Microsoft.AspNetCore.Authentication.JwtBearer
And for me, the issue was resolved.
You can uninstall System.IdentityModel.Tokens.Jwt
Because the Microsoft package depends on the system package, so it gets installed automatically.
I was able to solve this exact issue by using an older version of the nuget package. My starting point was that I had copied a class file from an old project to a new one. The class file referenced JwtSecurityToken. This did not compile in the new project, so I added Security.IdentityModel.Tokens.Jwt from nuget package manager. I just added latest version. This worked fine locally, but just like you, it failed when published to azure. I then looked at the old project and noticed that it was using 5.1.4 of that Security.IdentityModel.Tokens.Jwt. So, I downgraded to that version and it now works when published.
fwiw: this is the v2 preview runtime version at the time I did this.
https://<mysite>.azurewebsites.net/admin/host/status?code=<myadminkey>
{
"id": "<mysite>",
"state": "Running",
"version": "2.0.11587.0",
"versionDetails": "2.0.11587.0-beta1 Commit hash: 1e9e7a8dc8a68a3eff63ee8604926a8d3d1902d6"
}
tl;dr
None of the above worked for me and this would randomly happen from time to time until today it happened all the time. The only reason I could see was that Microsoft.IdentityModel.Tokens was not directly referenced in the executing project, but was on a referenced project. The library was sitting in the bin folder, it just wouldn't load.
Reference
Taking a clue from this solution to another problem I was able to resolve it like so:
Solution
Create a static constructor in the app's entry point class
static MainClass()
{
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}
Add the handler
private static System.Reflection.Assembly? CurrentDomain_AssemblyResolve(object? sender, ResolveEventArgs args)
{
var domain = sender as AppDomain;
var assemblies = domain.GetAssemblies();
foreach(var assembly in assemblies)
{
if (assembly.FullName.IsEqualTo(args.Name))
{
return assembly;
}
}
var folder = AppDomain.CurrentDomain.BaseDirectory;
var name = args.GetLibraryName().Name.Split(Symbols.Comma).FirstOrDefault();
var library = $"{name}.dll";
var file = Path.Combine(folder, library);
if (File.Exists(file))
{
return Assembly.LoadFrom(file);
}
return null;
}

Deploy .netcore to iis

I have a .net core app and runs fine on kestrel server.
http://localhost:5000/mapapi/v1.0/branch
I have deployed the app to IIS using publish tool in visual studio 2017 and with in program.cs, I have this line of code
.UseIISIntegration()
when I run the app I get an error
An error occurred while starting the application.
Exception: Cannot read variable
It is not reading the variable from launchsettings.json. I also created a profile by the name "IIS"
"IIS": {
"commandName": "iis",
"launchBrowser": true,
"launchUrl": "mapapi/v1.0/Branch",
"environmentVariables": {
"ConnectionString": "myhiddenconnectionstring",
}
}
What am I doing wrong? what other places do I have to look and make sure the application reads the values appropriately. Anyone in the community who had experience .net core to IIS, please share you knowledge with the community. We would appreciate it.

Resources