Visual Studio 2015 Feature Shared Project - Part 2

Before reading this article please go through my previous article  of the series:

This is part two of the series Visual Studio 2015 Features. In part 1, you learned about Custom Layout feature, like how can you manage different types of layout for different types of the system.

Today we are going to learn a very extensive and nice feature of Visual Studio 2015, Shared Project. As we all know, till know we can only share the library, assembly, services, etc. but there is not any option with previous Visual Studio to share the project itself.

But Visual Studio 2015 provides this feature and here you can create different kinds of project which is “Shared Project” and can share it with any type of project.

Shared Project is very useful when you are going to create some common functionality or shared logic which can be used with others application. It can be accessed by any type of application like Console Application, Windows Application, Phone application, etc.

shared

Create Shared Project

To create a shared project, open Visual Studio 2015 and go to File menu and choose New and then choose Project. It will open a new Project dialog window, here from the Visual C# node, you can choose Shared Project from the right pane.

Specify the name of the project “MySharedProject" and click OK.

new

If you are using the Visual Studio 2015 with Windows 7 and it is your first time to setup Shared Project then you will get the following error,

error

To resolve this error you need to make some changes in the CodeSharing file of the Visual Studio 2015.

Go to the following location C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeSharing and select the following file.

CodeSharing

Open it with notepad and change the following line of code.

Find this code,

  1. <Import Project="$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets')" />  
  2.   
  3. <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" Condition="!Exists('$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets')" />  
And change above line with the following line of code,
  1. <Import Project="$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" Condition="false" />  
  2. <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" Condition="true" />  
Now everything is fine, you can try to create the shared project again and you will see, it has created; You will find your created project as in the following,

solution

So, after adding the Shared Project, here I am going to add a entity class “Employee” which would be accessed in the different project.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace MySharedProject   
  6. {  
  7.     public class Employee  
  8.     {  
  9.         public int EmployeeId   
  10.       {  
  11.             get;  
  12.             set;  
  13.         }  
  14.   
  15.         public string EmployeeName  
  16.         {  
  17.             get;  
  18.             set;  
  19.         }  
  20.         public string Address  
  21.         {  
  22.             get;  
  23.             set;  
  24.         }  
  25.         public int Salary  
  26.         {  
  27.             get;  
  28.             set;  
  29.         }  
  30.     }  
  31. }  
Use the Shared Project

Now, it is time to access this shared project with other application. So, go and create a Console Application “MyApp”.

To access Shared Project with this application, you just need to give the reference of the Shared Project.
So, right click on References and choose Add Reference.

Reference

It will open Reference Manager for you where you can manage your references for the project. So, go to the Shared Projects option and choose Solution, you will see all the shared project related to this solution is available in right pane. Select your shared project and click OK.

project

You can see in the following image, the Shared Project has been added with your MyApp project. You can use the component of the Shared Project with your main project.

solution

So, it is time to access the Shared Project’s class “Employee” with “MyApp” application. When you type Employee, it will be not showing because we need to add the namespace for accessing the shared project.

project

project

So, we have used successfully the entity class of the Shared Project in Console App. So, make some change in the code and pass the value to the fields of the Employee class and run it.

Employee

Press F5, to run the project and you will find the following output for this.

output

So, today we learned about the Shared Project feature of Visual Studio 2015. I believe this is a very nice feature and we can implement it with our project for common functionality.

Thanks for reading this article, hope you enjoyed it.