Is there a way to use shared projects (shproj) in Visual Studio Code? - visual-studio-code

Hi,
Shared project seemed like a good idea to share code between .NET Core 2.0 web project and .NET 4.7 WinForms project.
I have bunch of extensions inside as well as web api client code which is used by both projects.
Everything works well in Visual Studio 2017.
However, on the road I use MacBook computer with Visual Studio Code which does not recognise shproj in any way. I also tried Insider version with new multiple workspace, but that also seem to work only with actual projects (csproj).
I know there is Visual Studio for Mac, but it is huge and far exceeds my on-the-road needs, so I would like to avoid it.
So, my question is - is there a way to utilize Visual Studio Code with projects that use shared projects (shproj)?
I would like to run/debug such projects in VSCode.
If not, does it make sense to create feature request somewhere? I mean, shared projects were designed for cross-platform code sharing and VSCode is brilliant cross-platform editor so it makes sense.
Or is this not part of VSCode at all, but some extension (like OmniSharp)?
Thanks,
Mario

At least now (2021) it seems to work fine.
TLDR; Create a new project with Visual Studio (2019) and add a "Shared Project" to that Project/Solution. Then open the project folder in Visual Studio Code and add the Shared Project folder to Workspace. In VS Code add a new Class to the Shared Project and adjust the *.projitems file to include the new class in the compile process. Run the project in both VS Code and VS to verify everything works as expected.
Following are the steps that I used. It looks like a lot, but it should only take about 10 minutes to setup this basic sample.
Open Visual Studio (2019)
Create a new "Console App (.NET Core)", give it a name and a location and select "Place solution and project in the same directory".
After creating the project you should have a new solution in "Solution Explorer" containing the new Console Application project.
Right-Click on the solution and select "Add" > "New Project..."
Select and add a new "Shared Project"
Add a new class to your Shared Project. In "Solution Explorer" right-click the shared project and select "Add" > "New Item..." and select the "Class" template.
In your "Console Application" add a reference to your Shared Project by right-clicking on your Console Application project, then select "Add" > "Shared Project Reference..." and select the listed shared project (*.shproj)
Use the new class of the Shared Project in the Console Application project
Run the Console Application to check if it builds and uses the shared class successfully.
Close Visual Studio (2019)
Open Visual Studio Code
Select "File" > "Open Folder" and select the folder where the Console Application is placed.
Note: If VS Code shows a message "Required assets to build and ... are missing" then select "Yes".
Select "File" > "Add Folder to Workspace..." and select the folder where the Shared Project is located.
Select "File" > "Save Workspace As..." and save the file to the project folder of the Console Application.
Open a new integrated Terminal in VS Code (in Menu "Terminal" > "New Terminal") and make sure that the current working directory is the folder of the Console Application. Then type "dotnet clean && dotnet run" to clean, build and run the solution.
Assuming we now want to add a new Class to the Shared Project from Visual Studio Code, we have to do some extra work, that normally Visual Studio (2019) would do for us when adding a new Class.
In VS Code right-click the folder where your shared class is located and add a new File. Give it a name with .cs extension and add the according code to the file to make it a valid class file.
Open the *.projitems file and find the part where your first class file has been added already. It should look like this:
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)SharedClass1.cs" />
</ItemGroup>
Now add the new class file to this ItemGroup section like this
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)SharedClass1.cs" />
<Compile Include="$(MSBuildThisFileDirectory)SharedClass2.cs" />
</ItemGroup>
Use the new class in the Console Application, save all files and type "dotnet run" in the integrated Terminal.
Check if everything builds and runs as expected.
If you notice that it builds successfully, but VS Code shows some error like "The type or namespace name 'Class2' does not exist in the namespace 'SharedProject1'", then restart VS Code. After restart it should be able to detect everything as expected.
Close VS Code and Open VS (2019) by double-clicking the *.sln file.
Build and Run the project to verify that all changes are compatible with VS (2019).

Related

Visual studio code notification asking if I want to exclude Java project settings files or not

I suddenly got this notification when open a flutter project in vs code:
Do you want to exclude the Visual Studio Code Java project settings files (.classpath, .project. .settings, .factorypath) from the file explorer? (options: Exclude Globally, Exclude in Workspace, Never)
I chose Never for the time being, but I was wondering what it even implies?
This is a prompt that comes from the Java extension from VS Code:
https://github.com/redhat-developer/vscode-java/blob/06793b174437fee55985c62917f08da926f37058/src/settings.ts#L73
I guess it's asking whether you want those java-related project files to show up in the VS Code explorer side bar, or be hidden (if you choose to exclude, it will write exclusions into your VS Code settings so they are hidden).

Import Eclipse C++ Workspace Into Visual Studio Code for Linux

I have a workspace in Eclipse on Linux Mint. This project builds for and ARM platform device running Debian linux.
I am not an expert on this. We have a guru who set it all up for us. I am not sure which method the project uses to build the apps, but I do know all I have to do it right click the project in Eclipse and select build on any of the apps and they build.
I would like to use VS Code to develop this app. Is there any way to import the whole workspace into VS Code and have it actually build and debug on the target device?
If so can someone please give me the steps?
I just clicked menu item File->Open Folder...
Then navigate to the parent folder of your workspace, which is actually your Eclipse workspace folder. Highlight it, then click the Select Folder button. Then it just loaded all my Source files in. I can't tell you how to convert to building your code, as I only did this to search files and hopefully find variable's definitions. And this worked! I didn't even need to do a full scan of my source files or anything (or maybe it did this automatically at some point). I just went to the definition-unknown variable's location I know about, highlighted it, and right-clicked and selected Go to definition, and it went and opened the file with its definition.

Visual studio Intellisense color-coding not working for unity script editing [duplicate]

When I want to edit C# Unity scripts, they open in Visual Studio. It is supposed to provide auto complete for all Unity related code, but it doesn't work.
Here you can see the missing functionality:
As seen, the transform object does not open the autocomplete menu.
Unity version: 5.5.2f1
Visual studio 2015
Visual Studio Tools for Unity is installed
There is no auto-completion because the script says "Miscellaneous Files" instead of the of the name of the Project. Take a look at the image below that came from the video in your question:
The "Miscellaneous Files" message can happen for many reasons:
It can happen when you open your Unity C# file from another folder instead of opening it from Unity Editor.
This can also happen because Unity crashed while Visual Studio is still open therefore corrupting some files.
It can happen because Unity was closed then re-opened but is no longer connected to Visual Studio. When Visual Studio is opened you get "Miscellaneous Files" and no auto-completion.
This can happen when Visual Studio Tools for unity is not installed.
When you create a script from Unity then quickly open it before Unity finish processing it or before the round icon animation stuff finish animating.
Most of the times, restarting Unity and Visual Studio should fix this.
I can't tell which one is causing the problem but I will cover the most likely solution to fix this.
Fix Part 1:
Download and Install Visual Studio Tools for unity from this link. Do this while Unity and Visual Studio are both closed.
From Unity Editor, go to Edit → Preferences... → External Tools. On the External Script Editor drop down menu, change that to Visual Studio 2015.
Fix Part 2:
If newly created C# files are coming up as Miscellaneous then follow the instruction below:
From Visual Studio, go to Tools → Options... → Tools for Unity → Miscellaneous. Under Show connectivity icon, set it to true then restart Visual Studio.
When you re-start, connection icon should now be available in Visual Studio. Click it then choose the Unity instance to connect to. The red 'x' icon should now turn into a brown checkmark icon. Now, when you create a new C# file in Unity, it should open without saying Miscellaneous.
Fix Part 3:
Still not fixed?
Re-import project then open C# Project.
Close Visual Studio.
From Unity, re-import project by going to Assets → Reimport All.
Now, open the project in Visual Studio by going to Assets → Open C# Project. This will reload the project and fix possible solution file problems.
Fix Part 4:
Still not fixed?
Fix each C# file individually.
Click on Show All Files icon.
Select the script that doesn't do auto-complete then right-click and select Include In Project.
Fix Part 5:
Not fixed yet?
Credit goes to chrisvarnz for this particular solution which seems to have worked for multiple people.
Close Visual Studio
Go your project directory and delete all the generated Visual Studio files.
These are the files extensions to delete:
.csproj
.user
.sln
Example:
Let's say that the name of your Project is called Target_Shoot, these are what the files to delete should look like:
Target_Shoot.csproj
Target_Shoot.Editor.csproj
Target_Shoot.Editor.csproj.user
Target_Shoot.Player.csproj
Target_Shoot.Player.csproj.user
Target_Shoot.sln
Do not delete anything else.
Double click on the script again from Unity which should generate new Visual Studio file then open Visual Studio. This may solve your problem.
Fix Part 6:
If not working, check if you are having this error:
The "GetReferenceNearestTargetFrameworkTask" task was not found
Install Nuget PackageManager from here.
Restart Visual Studio.
See this answer for more information.
Fix Part 7
Make sure all of the projects are loaded.
In Solution Explorer it should tell you # of # projects.
If all of the projects are not showing, right click on "Solution (# of # projects)" and click Load Projects.
Try this,
In Unity Editor Go to Menu, Click on Edit -> Preferences -> External Tools -> External Script Editor. Set it to Visual Studio (your installed version of VS).
Now in Menubar go to Edit -> Project Settings -> Player Settings -> Other Settings -> Under Configuration -> Check API Compatibility Level -> Change it to your installed .Net version. In my case I set it to .Net 4.x
Now if Visual Studio is running already go to Visual Studio, it will ask to reload project. Reload the project. Check if it works, if not close Visual Studio. Now Open cs file from Unity Editor, and now it should work.
I found another way to fix this issue in a more convenient manner:
Select the broken file in Solution Explorer.
Open its Properties.
Switch field "Build Action" from "Compile" to "None".
Then switch it back to "Compile".
This will kill the synchronization between Unity and Visual Studio somehow.
The next time Visual Studio will reload the project, it will prompt a warning.
Just click on "Discard".
If you have done all of the above and still isn't working , just try this:
Note: you should have updated VS.
Goto Unity > edit> preference >External tools> external script editor.
Somehow for me I had not selected "visual studio" for external script editor and it was not working. As soon as i selected this and doubled clicked on c# file from unity it started working.
I hope it helps you too.
Unload and reload the project, in Visual Studio:
right click your project in Solution Explorer
select Unload Project
select Reload Project
Fixed!
I found this solution to work the best (easiest), having run into the problem multiple times.
Source: https://alexdunn.org/2017/04/26/xamarin-tips-fixing-the-highlighting-drop-in-your-xamarin-android-projects/
This page helped me fix the issue.
Fix for Unity disconnected from Visual Studio
In the Unity Editor, select the Edit > Preferences menu.
Select the External Tools tab on the left.
For External Script Editor, Choose the Visual Studio version you have.
Click regenerate Files
You Done
Select project in Visual Studio
Click "Refresh" button
I hit the same issues today using Visual Studio 2017 15.4.5 with Unity 2017.
I was able to fix the issue by right clicking on the project in Visual Studio and changing the target framework from 3.5 to 4.5.
Hope this helps anyone else in a similar scenario.
Two Alternative Options:
Fix 1
#singleton pointed me in this direction. Instead of changing the target in Visual Studio you should change it in Unity since the project is auto-generated.
First delete the auto generated Visual Studio files:
.csproj
.user
.sln
Then from within Unity go to PlayerSettings and under 'Other Settings' change the 'Scripting Runtime Version' from Stable 3.5 to Experimental 4.6.
However, that didn't fix it for me.
Fix 2
I noticed all of the references to Unity related code was marked with a yellow warning. Check your error logs and see if this is the case. In particular see if you get the following error: getreferenceNearestTargetframeworkTask
If so try:
Start Visual Studio Installer again.
On the Build Tools 2017, click Modify,
Ensure that "Nuget targets and build tasks" are ticked. This should become ticked if you click on Universal Windows Platform development.
Update 2020 with Visual Studio Community 2019 and Unity 2019.3:
Open Visual Studio Installer as Administrator, select to modify your current installation and add "Game development for Unity"
If you add a new c# script in Unity now, and open it (automatically) with Visual Studio, it is not described as "Miscellaneous" at the top of the window but with "Assembly-CSharp", and the autocomplete works.
i found my solution by creating the .cs file from visual studio itself instead of unity editor
right click on project folder in solution explorer
add > new item
type "unity" on the search field on the top right
select "CSharp MonoBehaviour"
name your script on the bottom and click Add
In my case, correct .net version was not installed on my PC. I install the .net 3.5 on my pc and that worked for me.
For Windows or macOS:
Download/Install the Visual Studio IDE (with Unity Tools)
When installing, make sure you include installation of
Game development with Unity
Then using Unity (you can double click one of your C# files), open a new C# project and the Visual Studio IDE should open with your new project structure.
From there, you should be able to see what you are looking for.
For example:
For Linux (suggestion):
Try Monodevelop - Additional Information, it provides code completion/hints.
My autocomplete also didn't work because Visual Studio Tools for Unity wasn't installed. So, after you install that, delete the auto generated Visual Studio files. Others said that you open file again and the problem is solved but it's not.
The trick is: instead of normally double-clicking the file, you need to open the C# file from Unity by right click and then "Open C# Project".
The issue I faced was that the C# Project was targeting a different .NET Framework (4.7.2), whereas the Unity project had a different target (.NET 3.5).
I fixed this by changing the target in Unity as-
File -> Build Settings -> Player Settings -> Other Settings -> API
Compatibility Level : Set it to the .NET version you already have
installed (Check your .NET Version here). In my case, it was 4.x
After this, Visual Studio worked perfectly and autocorrect was fixed too.
Try pressing Ctrl + Alt + Space (which toggles between suggestion and standard completion modes)
For some odd reason, the "Game development with Unity" tool can become disabled in Visual Studio.
To fix this..
Open Visual Studio
Go to Extensions → "Manage Extensions" → Installed
Find "Visual Studio 2019 Tools for Unity"
If it is disabled, enable it
Restart VS
Credit to Yuli Levtov's answer on another Thread
The following works for me.
Go to Edit->Preferences->External Tools->External Script Editor Select Scripting Editor
I solved to install the same version of .NET on WIN that was configured in my Unity project. (Player Settings)
Go to Options on the Tools menu and then select Documents in the Environment node. (If Documents does not appear in the list, select Show all settings in the Options dialog box.)
Put a tick on "Miscellaneous files in Solution Explorer" and Click OK. (This option displays the "Miscellaneous Files" node in Solution Explorer. Miscellaneous files are files that are not associated with a project or solution but can appear in Solution Explorer for your convenience if you tick this option.)
Locate your file in the Solution Explorer under "Miscellaneous Files". Then drag and drop your file to where it should belong and voila! This will copy the file to where you drop it. You may now safely delete the older file under Miscellaneous Files folder if you wish to do so
Credits: https://stackoverflow.com/a/47662523/10471480
In case Scripts folder is not visible:
Click on "Show all files" in Solution Explorer
Locate the Scripts folder.
Right Click on Scripts and select "Include in Project"
Keep in mind that if you are using the ReSharper tool, it will override the IntelliSense and show it's own. To change that, on VS, go to Extensions -> ReSharper -> Options -> IntelliSense -> General then choose Visual Studio and not ReSharper.
Before restarting and/or re-installing VS, First try opening any other of your projects to see if Intellisence works, if it does, then issue probably lies with your current project. First, most probable victim would be the NUGET packages with pending updates. To Fix this,
Right click on references
Proceed to Manage NUGET Packages Under NUGET Packages
proceed to updates Install Updates and recheck Intellisence
I tried all of these but ended up finding out that I needed to right-click the solution in Solution Explorer and add existing items and find the C# assembly file in Window's Explorer. There seem to be a bazillion different problems that give you this error, this is likely the most simple solution. If you double click on your script from unity, it does not seem to drag the assembly along.
Another possible fix:
In the project window, click on the Assets folder
Right click, and Create -> C# Script
Double click that, and wait.
For some reason, this work.
None of the above solutions worked for me. However I opened the ProjectName.CSPROJ file and manually added the new file and it worked like charm
What worked me is that I copied all the code inside the broken class and removed that file.
Then, I opened an empty file with the same name and pasted back.
Result: beautiful syntax highlights came back!
"Preferences" -> "External tools" -> set you exteranl tool
Thats one fix. Also for VS you can use ReSharper by JetBrains, but I recommend use Rider. That one is also free for students.
It provides less performance than visual studio, but more than VS+Resharper definitely.
Have a good day, mate)
Try with combination: Ctrl + Alt + Space
one of the above methods are worked for me and I just found a solution to this problem,
1. First, go to the project directory and delete .sln file
2. Second, go to unity and double click your script. Then Visual Studio will be open with an error,
Then click ok and close Visual Studio editor.
Finally, turn off your Windows Defender and then go to your project directory and there will be .csproj file. Just double click and open this from your Visual Studio editor and open the scripts folder inside the assets folder and open the scripts and autocompletion will be working perfectly fine.
These actions solved the problem for my projects in Visual Studio 2022
FIX 1
Solution
Assembly-CSharp (right-click)
Load Entire Dependency Tree
FIX 2
Solution
Assembly-CSharp
References
Double click on any lib to force loading

VS 2012 TFS Source Control Explorer - Add Item to Right Click Menu

I have written a PowerShell script that takes a single file as an argument and runs a compare to another file in the background. I'd like to be able to right click a file in Team Foundation Server's Solution Explorer, and choose that file as the argument for my script. I've done the exact same thing for files in the Windows Explorer, but I can't quite figure out how to do it for TFS.
I thought the registry items to be edited would be located at HKCU\Software\Microsoft\VisualStudio\11.0\TeamFoundation\SourceControl\, but I added an item the same way I added it to the Windows Explorer shell, to no avail. Can anyone point me in the right direction?
I don't think adding a key to Registry will be enough, you will need to create a Visual Studio Extension for that.
Developing Visual Studio Extensions
Extend Visual Studio
Creating Extensions By Using the VSIX Project Template
There is this SO thread that shows how to do this strictly in Visual Studio using Options -> External Tools and Options->Customizing the context menu.
Add an Item to the visual studio folder right-click menu within AddIn

NuGet Package Manager Console Default Project dropdown is empty

I recently upgraded to Visual Studio 2012 RTM Ultimate from MSDN. I'm using EF Code First Migrations to build my database in my app, and I recently added a new entity and want to scaffold the migration for it.
To do this, you need to open the Package Manage Console window in VS, and type add-migration "some name here". This will scaffold any changes to your database since the last time it was updated.
The Problem
This issue did not occur on VS 2012 RC
The problem I'm encountering is the "Default Project" dropdown in the Package Manager Console is not populated, despite having several projects in my solution. The default project that is used when I just type the command above is the wrong project (my migrations are in another project). I get the following error when I do this:
No migrations configuration type was found in the assembly 'ProjectA'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
What I've Tried
I have tried setting the correct project (ProjectB) as the startup project, only to get this error:
Could not load assembly 'ProjectA'. (If you are using Code First Migrations inside Visual Studio this can happen if the startUp project for your solution does not reference the project that contains your migrations. You can either change the startUp project for your solution or use the -StartUpProjectName parameter.)
The Question
How can I manually specify which project migrations are added to, or force the Default Project dropdown to populate?
In VS 2015, I just restarted the IDE and the dropdown was filled again.
I was able to manually specify the project by using the following:
add-migration "Locations" -StartupProjectName "ProjectA" -ProjectName "ProjectB"
The documentation for this command is sparse, so here's what I assume is happening:
-StartupProjectName specifies the project where the database configuration is stored (an MVC4 project in my case)
-ProjectName specifies the project where the migrations are to be scaffolded.
I had ProjectB set as the startup project in my app due to testing for this question, but I think you can omit -StartupProjectName if the correct project is set as a startup project in VS.
Close the IDE and open again may help. It worked on my VS2015
Close the solution and open it again. Closing IDE sometimes takes more time. This works in VS 2017
If anyone is experiencing this issue with Visual Studio 2019 and .NET Core projects this is because the interface lacks a method of adding solution files to the project.
I was able to resolve the issue by using the command line tool as follows:
First navigate to the folder that holds the project and open a (git) bash console window.
Then add a solution file to the folder.
dotnet new sln
Then add a reference to each project to the solution (* will find all solutions)
dotnet sln *.sln add <project-folder-1>/<project-name-1>.csproj
dotnet sln *.sln add <project-folder-2>/<project-name-2>.csproj
N.B. Remember to replace the project folder and the name with what you need.
Close Visual Studio 2019 and reopen.
Nuget Package Manager and console will work as expected.
I had to open the solution.
To view to the solutions, look at the bottom of the "Solution Explorer" pane. There should be a "Team Explorer" tab. Click it. Then in the bottom half of the pane, your solutions should be listed. Double-click on the solution that you want to install things for, or right-click on it and select "Open".
That should open up the Package Manager Console with the correct project pre-selected.
I had to Restore NuGet packages first. Then it showed.
I had a similar issue with Visual Studio 2013. Right clicking on [Solution]->[Properties]->[Startup Project] make sure [Single startup project] is selected. I previously had multiple projects selected for Azure worker role testing.
After existing VS, Package manager worked fine.
first close vs,
then go to setting of your computer, apps & features, microsoft-visual-studio-installer,
modify, In the second tab you will get an option to define which parts of vs you want to use, select whatever belong to nuget.
when it is finished, open the vs again. now it is supposed to work
success!!!
You can select the projects under the Solution explorer .Then it showed up on package manager console . It worked for me
If you see projects under Solution explorer, but default project dropdown is empty, then my 100% precise solution is:
Select all projects under Solution explorer, right click, and REMOVE
Click in SAVE ALL
Now, select Solution explorer and ADD each project to Solution again
After that, You will see all projects added in Package Manager Console's Default Project DropDown.
That's it.