How to create a .NET Core library I can reference from a .NET Core App (Web API) - .net-core

I'm working in Visual Studio 2015 Update 3 and .NET Core 1.0. I have a Web API project which is of type .NETCoreApp v1.0. When I add a .NET Core class library, it is of type .NETStandard v1.6. I can add this library to the Web API project as a reference, but it is not recognised when I try to add using statements.
If I create another project of type .NETCoreApp, I can reference it and use the classes without a problem.
How do I make use of a .NET Core class library from my .NET Core App?
Edit/Update:
This appears only to be an editor/Intellisense issue, because despite the editor warnings, the .NETCoreApp does build and run, calling into the class library.
I am running Resharper, which I see is blamed for similar problems with other types of projects: I have checked that I have the latest version and have cleared the Resharper cache and restarted VS2105.

This is a Resharper issue. At this time Resharper (v2016.1.2) does not support .NET Core 1.0.
There are 2 possible solutions:
Uninstall Resharper, and the Visual Studio native intellisense works.
Install the Resharper 2016.2 EAP (Early Access Program) version. I've done this and it's working. Obviously it comes with the caveats of any EAP/beta product.
Here is a link to the Jetbrains forum post where I was told .NET Core 1 was not yet supported and pointed to the EAP version.

Once you've built a library that targets netstandard1.X, you can either:
Produce a NuGet package with dotnet pack and host it locally or on NuGet. Then, install it in your netcoreapp project as any other dependency.
If your library and application are part of the same solution, make a local reference:
project.json
"dependencies":{
"MyLibrary.Core": {
"version": "1.0.0",
"target": "project"
}
}
target: project tells dotnet to look in the current solution for the dependency, instead of using your NuGet feeds. Again, this only works if you are developing the library and application in the same solution.

Related

How to run .NET framework project with VS Code, which has dependency of ReactJS?

I am beginner to .NET framework and ReactJS. I have a code repository which is .NET framework, with ReactJS dependency.
When I opened the repository, VS Code suggested me to install C# extension, I installed.
Now when I opened the main file abc.csproj; it automatically started installing packages.
It ended with message:
Finished
Failed to spawn 'dotnet --info'
and notified:
The .NET core SDK cannot be located. .NET Core debugging will not be enabled. Make sure the .NET Core SDK is installed and is on path.
What is process and list of extensions if I need it to be ran in VS Code?
P. S. It is a .NET framework 4.5 project and not core .NET project
The C# extension no longer ships with an included Mono & MSBuild Tools...
If you still need Unity or .NET Framework support, you can set
omnisharp.useModernNet to false
in your VS Code settings and restart OmniSharp.
See issue #5120 for more details.
https://github.com/OmniSharp/omnisharp-vscode/issues/5120
it seems you don't have the .net framework SDK installed on your machine.
you can download and install .net sdk from here : https://dotnet.microsoft.com/download/visual-studio-sdks to be able to build .net framework projects
I will suggest to use Microsoft Visual Studio. I am using it and it's good and dragless rather than getting SDKs and having installation troubles.

Outlook Interop in .NET Core 3.0?

I migrated our project from .NET Framework to .NET Core 3.0 (C#, WPF) and now I can not use Microsoft.Office.Interop.Outlook anymore, because it is not compatible with .NET Core 3.0. What I want to achieve is opening/sending prefilled Outlook Emails.
Is there an alternative to this interop dll, or maybe a way to use .NET Framework for only this reference?
Microsoft.Office.Interop.Outlook
NetOffice.Outlook
This worked for me:
Right click Dependencies and click "Add Reference"
Select Microsoft Outlook 16.0 Object Library under the COM tab.
Under Dependencies/COM in your project, select Interop.Microsoft.Office.Interop.Outlook reference, then under "Properties" set "Embed Interop Types" to "Yes"
I also had to uninstall the Microsoft.Office.Interop.Outlook NuGet package because it caused a conflict with the reference in Dependencies/COM.
Outlook automation is now working fine.
I am having the same issue when trying to use Microsoft.Office.Interop.Excel 15.0.4795.1000. It won't run when I target .net core 3.00 preview 7.
At this stage I do not think there is any other option than reverting back to .NEW Core 2.2.
I had the same problem while migrating components with Office interop for Excel and Outlook to .NET Core 3.0. I found out that this is only a problem of the Nuget packages Microsoft.Office.Interop.Outlook and Microsoft.Office.Interop.Excel. The packages probably need an update to work with 3.0?
If I make direct references to the Interop assemblies all is working well. Here is a link to an example for this.
If you use Visual Studio, you need an additional workaround to achieve this. From the linked sample:
Adding COM references to .NET Core projects from Visual Studio is not
currently supported. The workaround is to create a .NET Framework
project, add the COM references, and then copy the relevant
COMReference elements in the project.
This is possible in a roundabout way. Here is how:
Create a temporary .NET framework project.
Add the references to COM components (Microsoft.Outlook.Interop).
Open that csproj file and copy the entire those components are located in, over to your .NET Core project.
The nuget packages seem to be incompatible at the moment.
Reference: DotNet Samples

Need to build Roslyn Analyzer targeting .NET Standard \ Core rather than .NET Portable

I'm using default Roslyn SDK templates that came with Visual Studio 2017. The projects they create target .NET Framework Portable. I'm assuming Roslyn extensibility projects can target .NET Standard \ Core instead of Portable and I'm looking for templates or a sample of Roslyn Analyzer \ Refactoring project that I could study.
Sample of converted analyzer from default analyzer template is available here. There is original analyzer for comparison along with TestAnalyzerStandard which targets .NET standard.
Steps to make it work:
Create new .NET Standard library
Library must target .NET Standard 1.3. This is required if you wish to run analyzer as extension inside VS (extensions target .NET 4.6). Mapping between standard versions and full framework versions is available here. Also if you try to target lower version than 1.3, you will not be able to include required analyzer packages.
Add nuget package for Microsoft.Composition latest version. This is needed by Microsoft.CodeAnalysis.CSharp.Workspaces. If you try to add workspaces first, you will get error that referenced composition package is not compatible.
Add nuget package for Microsoft.CodeAnalysis.CSharp (I'm using latest 1.* version)
Add nuget package for Microsoft.CodeAnalysis.Csharp.Workspaces (version should match the version of Microsoft.CodeAnalysis.CSharp).
At this point you can copy code from portable project and build it. There should be no errors (you may have to close and reopen solution if VS is still displaying red squiggles).
To make VS extension work, just open source.extension.vsixmanifest, go to assets tab and change reference to .NET standard library
To create .nuget package just execute nuget pack Diagnostic.nuspec .. Diagnostic.nuspec is valid for Nuget 2.x. If you are using nuget via package management console in VS 2017 you will have to change <file src="*.dll" ..." to <file src="bin\*\netstandard1.3\*.dll" ....
Those steps are result of my experimentation with analyzers (I previously played with creating DLL which targeted full framework instead of being portable library). They are not by any means official.
I started working on a new Roslyn project and built things one by one instead of using template. https://github.com/IKoshelev/Roslyn.AutoLogging/commit/1f88e3e49141e0fa425c51fdcb3457a7c3d6dcaa
I managed to have the following targeting:
Refactoring project - .NET Standard 1.3 (this .dll will be distributed, version kept to minimum)
UnitTests project - .NET Core 2.0
VSIX project - .NET Framework 4.6 (I believe, only full Visual Studio supports VSIX, so that is okay)
Update Versioning of Roslyn is a bit more complicated right now, i.e. if you want to use your extensions with Visual Studio 2015 you will have to use PCL libraries. More info at the end of this article article on Roslyn

No executable found matching command dotnet-projectmodel-server

I'm getting this error when opening .NET Core projetcs in VS 2015 Community:
The following error ocurred attempting to run the project model server
process (1.0.0-preview-003585).
Unable to start the process. No executable found matching command
"dotnet-projectmodel-server".
It was working well until yesterday. I've already tried do reinstall the .NET Core SDK and repair the VS installation.
Some tips?
I had this same problem. I was able to fix it by uninstalling Microsoft .NET Core 1.0.1 - SDK Preview 3 (x64) and reinstalling the .NET Core 1.1 SDK from here:
https://www.microsoft.com/net/core#windowscmd
Another option to fix this is to add a Solution folder called "Solution Items" and add a global.json file in it with an "sdk" element specifying the correct SDK version if you have multiple installed side-by-side. I installed 2017 RC and I started getting this error because it installs Preview 4. I don't want to uninstall Preview 4 or 2017 will stop working. Adding the global.json file with the following text fixed it for me. (check your %PROGRAMFILES%\dotnet\sdk folder for installed SDK versions.)
{ "sdk" : { "version" : "1.0.0-preview2-1-003177" } }
Update (11 May, 2017)
According to the new Announcing .NET Core Tools 1.0 blog post:
We’re now encouraging everyone to migrate to MSBuild and csproj from
project.json. As I stated above, we will not be supporting any of the
new .NET Core tools in Visual Studio 2015. We also won’t be updating
the Visual Studio 2015 project.json-based tools.
Original answer
There is a closed issue related to this problem in the .NET Core CLI repo:
Unable to start process "dotnet-projectmodel-server" while starting VS2015
According to the post Changes to Project.json, since Preview 3 the .NET Core tooling moved from .xproj/project.json to .csproj/MSBuild. So basically you have three choices:
Continue using the .xproj/project.json format with a Preview 2 SDK (as said in #Tim Schmidt's answer)
Use the dotnet migrate tool or the VS 2017 RC to migrate your existing project.json assets to csproj. Note that if you are using VS 2015, you will lost the .NET Core Tooling until the VS 2015 Tools (Preview 2) is updated.
Work with project.json and csproj on the same machine, switching between the Preview 2 and Preview 3+ with the help of the global.json file (as said in #randcd's answer).
But, if you plan to migrate to .csproj/MSBuild, pay attention to the post Using MSBuild to build .NET Core projects:
We recommend that most people targeting .NET Core with new projects today use the default tooling experience with project.json because of the following reasons:
MSBuild doesn't yet support a lot of the benefits of project.json
A lot of the ASP.NET based tooling doesn't currently work with MSBuild projects
When we do release the .NET Core tooling that uses MSBuild, it will be able to automatically convert from project.json to MSBuild projects
I got this message when I was trying out a new Angular2 with .NET Core template from the ASP.NET Core Template Pack.
Basically, you'll need a global.json file as a Solution Item in the Solution folder (NOT Projects)
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-preview2-003131"
}
}
For more information, you can read further on this page.
Dot Net Core Tutorials
In my case, after installing .net core 1.1.0 there were still versions 1.0.1 installed. Delete old versions and reinstall Microsoft .NET Core tools.
I got the same error after installing .Net Core 1.0.1 (x64) on Visual Studio 2015 Enterprise following these steps:
Installed .Net Core SDK 1.0.1
Opened Visual Studio, Went to Tools, Extensions and Updates and saw there was an update for Asp.Net and Web Tools which I chose to to install
I tried to fix it by uninstalling and re-installing the latest version of .Net Core SDK but that didn't solve my problem. I kept getting the error:
In my case, I was trying to create a new project using a custom template, so I changed and tried to create a new Asp.Net Core Web Application (.Net Framework) and I got an error that told me exactly what version of the .Net Core SDK I needed:
I followed the link from the dialog (click here or go here: https://github.com/aspnet/Tooling/blob/master/known-issues-vs2015.md#missing-sdk) and downloaded the specific version. I Uninstalled the current .Net Core SDK (1.0.1 x64), then installed the suggested one (1.0.0-preview2-003131 in my case) and that fixed my problem.
I has that problem after installing visual studio 2017, I just created a global.json file
with this configuration:
{
"projects": [ "src", "test" ],
"sdk": {
"version": "1.0.0-xxxxx"
}
}
To know which version you are possibly using, open your console and go to C:\Program Files\dotnet\sdk> then execute a simple dir command, that will list the folders of the different dot net core versions you may have on your system, try one by one and select the version that works for you.
BTW: it worked for me to place the global.json in the project root, not the solution.
I just encountered the same issue after updating Microsoft Azure SDK v2.9.6
I uninstalled the 2.9.6 SDK and noticed I had a problem opening my global.json file. So I got the most recent version from VSTS for my solution and that fixed my problem.
Not a good position at all. Dot Net Core 1.0 is the LTS version and this is the one to target. .NET Core 1.1 is NOT the LTS version (and will not run on Amazon for example). So recommending this route goes against Microsoft's policy on LTS versions.
An LTS version MUST continue to be "supported for three years after the general availability date of a LTS release"

How to add project reference to ASP.NET Core 1.0 MVC project

I have a ASP.NET Core 1.0 MVC app in solution X and I have some common projects (.net 4.5.2 class libs) in solution Y.
I want to reference the projects in solution Y from my app, when I do so via add reference -> Browse .. I get:
.NET Core projects only support referencing .NET framework assemblies in
this release. To reference other assemblies,
they need to be included in a NuGet package and reference that package.
I then created a nuget package of those projects, added the folder that contains the nuget packages as a repo source and loaded the projects. This adds the projects successfully to my project.json, but 'nothing' else actually happens, I still can't use the code in my app.
Now ASP.NET Core is past its beta status, what is the official way of dealing with this?
Many people have struggled with this issue and there is a long running thread on GitHub about it. Even the people using the latest RC3 build are reporting the same problem that you are having.
The only way I've been able to reference class library projects in an ASP.NET Core web application is to create both the web application and the class library projects in Visual Studio 2015 Update 2. And they all have to target .NET Framework 4.6.1.
I had to copy the code from my old class library projects to the new ones. But in the end I think I saved myself time by not having to mess with all the workarounds that don't seem to work for a lot of people.

Resources