I recently created a new .net6 project and moved my source code into it.
I've set the following in the project file
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
In the project properties there is a section at the bottom of Application -> General there is a "Default namespace" which also has "$(MSBuildProjectName.Replace(" ", "_"))"
For the sake of this question I have omitted the solution completely and let's assume my project structure is as follows
MyProject (this is a project with project file MyProject.csproj)
DB (this is a folder in the project)
MySQL (this is a sub-folder under the DB folder)
MSSQL (this is a sub-folder under the DB folder)
UI (this is a folder in the project)
If I add a new class at the root level of my project it's namespace is MyProject however if I add a new class to DB it's namespace is DB.
I expect this to be MyProject.DB
Similarly if I add a new class to the MSSQL project I get a namespace of
DB.MSSQL whereas I expect this to be MyProject.DB.MSSQL
I have tried hard coding the $(MSBuildProjectName.Replace(" ", "_")) as MyProject but this doesn't help.
As strange as this sounds, the problem was with WebView2 version 1.0.1343.22
Updating to version 1.0.1370.28 resolved the issue
I did also recreate this in a fresh solution with only 1 project and no other dependencies to ensure that is the case.
Interestingly the default using statements are also different.
With version 1.0.1343.22 installed, creating a new class file you get
using System;
using System.Collections.Generic;
using System.Text;
namespace DB.MsSQL
{
internal class Class1
{
}
}
Whereas with version 1.0.1370.28 a new class file looks like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebView2NamespaceIssue.DB.MsSQL
{
internal class Class2
{
}
}
Both of these classes were created in the same folder of the same project.
The only difference was changing the version of WebView2 being referenced
Background: I am a novice in C#, and use Visual Studios 2010 Express.
I have a class (let's call it myclass) which I want to use in multiple projects. I used to add classes with Project->Add Existing Item... Which creates a copy of myclass.cs.
Now I just found out that when I build the original myclass.cs it creates a myclass.dll and places it in the release folder of my project.
But when I try to use this DLL, I get the following error:
The type or namespace name 'myclass' could not be found(are you
missing a using directive or an assembly refference?
Which is weird to me, because I already have referenced it (it is also in the Reference folder of my Solution Explorer). And I already have added this to my code:
using myclass;
So what am I doing wrong?
Update: When I tried my old method (add existing item -> myclass.cs) the error message goes away. So it's not a matter of spelling things correctly.
Add the dll first:
Click on references in your project-explorer in visual studio and add your dll then you can use it as you expected it.
Add the reference in your project and check that the target Framework version of that assembly fits the project.
Check the namespaces inside the assembly and then use them like:
using YourAssemblyNamespace.class
Okay so I found the answer myself. It turns out that when you use the using function, it automatically searches for all public classes in the namespace you want to use.
If it can't find a public class, it refuses to recognize the DLL.
Furthermore, not specifying a class makes it internal.
So:
class myclass // internal!
private class myclass // private!
public class myclass // only this makes it visible for others!
Everything was okay after changing class myclass into public class myclass.
I'm trying to use an external code file to include some helper classes for several .ashx handlers. The example one is using ListToJSON, which just turns nested Lists (Theres stuff like List<List<whatevers>> that are being worked with) into JSON (which will get thrown into context.response)
The ListToJSON class works fine when it's in the same file. I'm trying to put it into a Helper.cs file which is included in the same project in VS2010, because these classes get used in several different handlers.
I was under the impression that "using Helper;" was what I needed to do, but I still get the error "The type or namespace Helper could not be found (are you missing a directive or assembly reference?"
(and intellisense doesn't see it either)
I've also tried putting both code files into the same namespace. Same error.
It's not a DLL file, it's just a c# code file in the same project. Should I realy be compiling it as a DLL to do this? If so, how do I do that? (Once I do, I can do the right click->add reference, correct?)
I think I'm supposed to be using the App_Code folder, but I'm not really sure how to set that up in VS so that it's referenced properly.
My handler file (skeleton)
<%# WebHandler Language="C#" Class="generateReport" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Diagnostics;
using Helper;
public class generateReport : IHttpHandler {
public void ProcessRequest (HttpContext context) {
ListToJSON Converter = new ListToJSON();
//context.Response stuff goes here.
}
}
My helper file, Helper.cs - this is in the same project directory and is included in the project:
using <stuff>;
namespace Helper
{
public class ListToJSON
{
//class definition here
}
}
UPDATE: So, I put Helper.cs into the /App_Code/ folder, and it seemed to be playing nicely. Intellisense was picking up things in Helper.cs after I did using Helper;
When I tried it on IIS, it I got the following familiar error:
Compiler Error Message: CS0246: The type or namespace name 'Helper' could not be found (are you missing a using directive or an assembly reference?)
Line 19: using Helper;
Source File: <path>\info.ashx Line: 19
I get no errors when running this through visual studio's IIS emulator. When I run it through IIS (localhost) I get the IIS internal server error described.
App_Code folder was made through VS, I right clicked and chose Add ASP.NET Folder > App Code\
Edit: Added tag iis
I assume that you are using WebSite and not WebApplication.
While adding new Class file in the Website, It should be in the App_Code Folder to avoid the Accessibility issues.
Edit - 1 => Please see below the Publish details.
Edit = 2 => Try adding the individual Assembly in the bin folder as stated below. This will confirm that the App_Code folder Dll are incorporated.
For IIS to read classes correctly from the App_Code folder, the App_Code folder (or the bin folder, for compiled assemblies) needs to be in the IIS root directory in order for classes within to be detected.
In my case, I was publishing to a subfolder of the IIS root directory, and so IIS couldn't see the classes in it.
I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
My header look like this in four files, including my main core file that calls:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class shell : Form
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
Does not contain a static 'main' method suitable for an entry point
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
I've looked around so far without a solution.
The properties on each .cs file are set to 'Compile'.
I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
I'm still very new and this is my first attempt at an organizing method with c# code.
I was looking at this issue as well, and in my case the solution was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)
All I needed to do was change the output type of the project properties to Class library
Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.
I had this error and solved it using this solution.
Right click on the project
Select "Properties"
Set "Output Type" to "Class Library".
Try adding this method to a class and see if you still get the error:
[STAThread]
static void Main()
{
}
If you don't have a file named Program.cs, just add a new Class and name it Program.cs.
Then paste this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sales {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.
Had this problem in VS 2017 caused by:
static async Task Main(string[] args)
(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)
Adding
<LangVersion>latest</LangVersion>
to app.csproj helped.
Edit .csproj file
<OutputType>Library</OutputType>
cheers !
If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".
For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error.
To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.
hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main()
I hope it helps..
Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
Salaam,
I have both Visual Studio 2017 and Visual Studio 2019
Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.
Visual Studio 2017
Visual Studio 2019
Just right click on project and select properties and then set Output type on Class Library
After placing the above code in Program.cs, follow below steps
Right click on the project
Select Properties
Set Output Type to Windows Application
Startup object : namepace.Program
When you want to allow paramaters to be specified from the command, they must look like this:
[STAThread]
static void Main(params string[] paramaters)
{
you cannot specify more than one paramater, otherwise this will also cause the error reported above.
For some others coming here:
In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems> without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly
hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
A valid entry looks like:
public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}
I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.
My entry point was placed in /App_Code/Main.cs, and I had to do the following:
Set Project -> Properties -> Application -> Output type = Console Application
Create the /App_Code/Main.cs
Add the code above in it (and reference the methods in my project)
Right click on the Main.cs file -> Properties -> Build Action = Compile
After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.
Why I did this instead of 2 separate projects?
Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.
For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.
If you are using a class library project then set Class Library as output type in properties under application section of project.
Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs. The value for Build Action should be C# compiler.
I accidentally changed Build Action to None, which removed program.cs from the project and therefore wasn't included when compile started.
Did you accidentally remove the entire Program.cs file?
If you have removed,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This might work for you.
Make sure you are not using void with async like
static async void Main(string[] args)
If yes, then change void to Task like
static async Task Main(string[] args)
If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.
For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.
Check to see if the project is set as the "Startup Project"
Right click on the project and choose "Set as Startup Project" from the menu.
If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...
namespace ClassLibrary1
{
public class Class1
{
}
}
To this...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.