How to reference a csproj using csc? - c#

Using csc how can I reference another project's source?
If I have my own .csproj for a hefty project that depends on another, and am compiling the dependent with dotnet build rather than csc I can add
<ItemGroup>
<ProjectReference Include="..\MyAPI\MyAPI.csproj" />
</ItemGroup>
To the csproj file, but what about in the case of writing a single .cs file?
csc test.cs /reference:../MyAPI/MyAPI.csproj
doesn't work because
error CS0009: Metadata file '/Users/theonlygusti/MyAPI/MyAPI.csproj' could not be opened -- PE image doesn't contain managed metadata.
test.cs(2,7): error CS0246: The type or namespace name 'MyAPI' could not be found (are you missing a using directive or an assembly reference?)

Related

Not able to create Docker Image

`I have added COM reference in my project i.e(MODI) for converting image to text .
while creating docker build for creating image in terminal i am getting below error.
/app/Services/ConvertPdftoWord.cs(18,37): error CS0246: The type or namespace name 'MODI' could not be found (are you missing a using directive or an assembly reference?) [/app/ModiOcr.csproj]
Even while adding COM reference it automatically .csproj file added below line
<ItemGroup>
<COMReference Include="MODI" Condition="'$(MSBuildRuntimeType)' != 'Core'">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>12</VersionMajor>
<Guid>a5ededf4-2bbc-45f3-822b-e60c278a1a79</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>
Dockerfile i have attached and i am new to Docker Please help on this.
`
how to resolve this issue do i need to include COM referene into Dockerfile?

MSTest unit test project added to C# solution causes existing project build to fail VS2022

Im using VS2022 to create a .NET Core 3.1 C# class library (DeletemeClassLib.csproj) with no added content. Solution and Project are in the same folder. This builds as expected.
When I add a MSTest project to the existing solution using .NET Core 3.1, the MSTest project will build without errors, but the DeletemeClassLib.csproj build fails with errors like:
Error CS0246 The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) DeletemeClassLib C:\Users...\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs 5 Active
The build errors appear to point to my UnitTest1.cs file (in the MSUnitTest.csproj project), but they only occur when building the separate DeletemClassLib.cs project. I also tried the same thing using nunit instead of MSTest, but I found similar results.
My DeletemeClassLib.csproj has no packages listed under the project. My MSUnitTest1 project lists coverlet.collector 1.2.0, Microsoft.NET.Test.Sdk 16.5.0, MSTest.TestAdapter 2.1.0, and MSTest.TestFramework 2.1.0.
DeletemeClassLib.csproj
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PlatformTarget>x64</PlatformTarget>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
</PropertyGroup>
</Project>
MSUnitTests1.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateTargetFrameworkAttribute>false</GenerateTargetFrameworkAttribute>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
</Project>
Class1.cs (main project file in DeletemeClassLib.cs project):
using System;
namespace DeletemeClassLib
{
public class Class1
{
}
}
MSUnitTest1.cs (Main MSTest unit test file):
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MSUnitTests1
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
}
}
Build Output with username hidden:
1>------ Build started: Project: DeletemeClassLib, Configuration: Debug Any CPU ------
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(14,12,14,54): error CS0579: Duplicate 'System.Reflection.AssemblyCompanyAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(15,12,15,60): error CS0579: Duplicate 'System.Reflection.AssemblyConfigurationAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(16,12,16,58): error CS0579: Duplicate 'System.Reflection.AssemblyFileVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(17,12,17,67): error CS0579: Duplicate 'System.Reflection.AssemblyInformationalVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(18,12,18,54): error CS0579: Duplicate 'System.Reflection.AssemblyProductAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(19,12,19,52): error CS0579: Duplicate 'System.Reflection.AssemblyTitleAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\obj\Debug\netcoreapp3.1\DeletemeClassLib.AssemblyInfo.cs(20,12,20,54): error CS0579: Duplicate 'System.Reflection.AssemblyVersionAttribute' attribute
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(1,17,1,29): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(5,6,5,15): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>C:\Users\XXXXUser\VSProjects\DeletemeClassLib\MSUnitTests1\UnitTest1.cs(8,10,8,20): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "DeletemeClassLib.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
Any ideas on what might cause the DeletemeClassLib.csproj build to fail when the MSUnitTest1.csproj is present?
If I move MSUnitTest1.csproj to a completely separate folder, everything builds correctly. However, I am trying to retrofit an older Github solution (this example is a simple representation of the other project) to add unit tests, and I can't add a unit test folder above the git parent to separate the unit tests and main project into separate folders as I would like to.
Thanks

The type or namespace name 'Specflow_xxx_XUnitAssemblyFixture' could not be found (are you missing a using directive or an assembly reference?)

After I installed this nuget package in my specflow demo C# .NET Core 3.1 project, I got this error:
Severity Code Description Project File Line Suppression State
Error CS0246 The type or namespace name
'Specflow_Demo_XUnitAssemblyFixture' could not be found (are you
missing a using directive or an assembly
reference?) Specflow.Demo ...\Specflow.Demo\Features\LoggedInDiscount.feature.cs\LoggedInDiscount.feature 3 Active
This is my csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="SpecFlow.Tools.MsBuild.Generation" Version="3.9.22" />
<PackageReference Include="SpecFlow.xUnit" Version="3.9.22" />
<PackageReference Include="SpecRun.SpecFlow" Version="3.9.7" />
</ItemGroup>
</Project>
After I close all the files in Visual Studio and rebuild, I got another error:
There is an open issue with SpecFlow related to this: Type or namespace [TestNamespace]_XunitAssemblyFixture could not be found. Are you still able to run your test cases? The above link says its an intellisense only issue.
From a comment on the GitHub issue discussion:
The generated assemblyFixture file (e.g.: [TestNamespace]_XunitAssemblyFixture) can be found in the obj folder (obj\Debug\targetframework\xUnit.AssemblyHooks.cs).
We realized that this is IntelliSense related "issue" (IntelliSense of VisualStudio itself, not R#).
As you all noticed, the build was successful, but the error was still there. This is because the generated code behind file (*.feature.cs) is opened in your editor. If you close them, and build again, the error will disappear.
...
BTW there is an ongoing PR which will move the generated files to the obj folder, so this problem will disappear when it'll be merged.
I had a quick look through the types in the referenced nuget packages. None of them uses the naming style that you have there 'Specflow_Demo_XUnitAssemblyFixture' - and none of them has a type with a similar name.
So, as the error says, the type doesnt exist.

Convert Project to SDK style and referenced project cannot be found

Converting project TEditor in my larger solution to use SDK with target frameworks including netstandard2.0.
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;MonoAndroid90</TargetFrameworks>
In visual studio I told different project BASE to reference that project TEditor. Shows up in dependencies.
But when I have Using TEditor in CS code in Base, it does not find the TEditor (error on Using TEditor;). Furthermore when I build Base I get CS0246 error:
...\PageModel.cs(12,7,12,14): error CS0246: The type or namespace name 'TEditor' could not be found (are you missing a using directive or an assembly reference?)
Base is using netstandard2.0
netstandard2.0
and both projects use
<Project Sdk="MSBuild.Sdk.Extras/2.0.54">
Lastly I am getting DLL's produced from TEditor in correct bin/debug folders:
1>------ Rebuild All started: Project: TEditor, Configuration: Debug Any CPU ------
1>TEditor -> C:\Users\Brent\Documents\MyDataApp\Teditor\bin\Debug\netstandard2.0\TEditor.dll
1>TEditor -> C:\Users\Brent\Documents\MyDataApp\Teditor\bin\Debug\monoandroid90\TEditor.dll
What am I missing and how can I diagnose further?
Well, I found out my TEditor.dll was empty when I looked at the object browser. With the new SDK setup I didn't have the correct Includes and it compiled and looked happy, because it wasn't including any code.

C#: Including assemblies without visual studio

How do I include an external assembly .dll in a C# .exe? I'm specifically working with the MySql connectors for .NET 4.0.
Please note, I'm not using Visual Studio at this time and do not plan to in order to accomplish this. Here's the error I get when I compile: CS0246: The type or namespace MySql could not be found (are you missing a using directive or an assembly reference?)
I have tried:
- Copying the .dlls into the source folder for the project.
- Copying the .dlls into the .NET folder under Windows
The first three lines of code are:
using System;
using MySql;
using MySql.Data;
Sample Program (Program.cs)
using System;
using MySql.Data.MySqlClient;
namespace TestApp
{
internal class Program
{
private static void Main(string [] args)
{
Console.WriteLine("Hello world!");
var command = new MySqlCommand();
Console.WriteLine(command.ToString());
}
}
}
Project file (TestApp.csproj)
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<Target Name="Build">
<Csc Sources="#(Compile)" AdditionalLibPaths="lib" References="MySql.Data.dll">
</Csc>
</Target>
</Project>
MySql.Data.dll is in a folder named lib
Run msbuild from the command line
msbuild TestApp.csproj /t:Build
Microsoft's guide to writing your own msbuild files.
https://msdn.microsoft.com/en-us/library/dd576348.aspx
csc task details
https://msdn.microsoft.com/en-us/library/s5c8athz.aspx
You can use the /reference (or /r) command line option:
csc yourcode.cs /r:MySql.Data.dll
Assuming that, as you said, you have copied the MySql.Data.dll into the source folder for the project.
If you haven't copied the dynamic link libray to your source folder, then you can use the /lib: command line option to specify additional directories to search in for your references.

Categories

Resources