Can I compile only one class files and use it? - asp.net

I have a asp.net application, in which I have App_code folder with different classes.
Whenever I publish my application it creates single App_Code.dll for all classes, which is fine, but now I have to add one more class but I want to publish only that class file.
Is it possible??

Try with adding new project (Class library ) in solution, add your class file in that project .
And use it by adding reference it to in another project.

Then you shouldn't publish, place the code as is and it gets compiled on first user's request. And whenever new class is added, AppPool gets restarted. Of course, this comes with slowness for first user and after that it is similar to what is done with publish.

Related

References and external class use problems

Using Visual Studio 2015 on an ASP.NET project. Everything works fine except now I have a block of code (A common AD utility function) that I want all the pages to be able to access. The original language (inherited project) is VB.NET, but it should work just fine either way.
I created a class file, named it CommonADFunctions.vb. The file uses System.DirectoryServices, and there's a reference in the project to that assembly. However, there are two problems:
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This may seem like a trivial ask, but what am I missing?
Turns out this was a simple property on the file itself I created a new Class File, and somehow the Build Action got set to Content rather than Compile. Setting this to Compile allowed the object to be referenced and instantiated properly. It doesn't fix the IDE intelliSense from APP_Code for DirectoryServices references in the code, but it compiles and runs, which is what I needed.
EDIT: Received external help which pointed this out.
Without code and screenshots to illustrate I can only recommend a few things to try.
I can't reference the new class to call the functions from any of the page code files. This includes attempts to instantiate a new variable as the class type.
The methods in the class have Private or Friend scope.
Its a static class (a Module in VB.Net) and you do not need to instantiate it.
You are somehow referencing an old DLL which doesn't have the methods. Check the reference to this AD project and make sure its "Referenced" via Project.
When I move the new .VB file into App_Code, the reference to System.DirectoryServices breaks, and it refuses to build.
This sounds like a corrupt project file. Clean the solution, delete the .suo file, delete the obj & bin folders.
Then move the CommonADFunctions.vb to the App_Code folder and add the DLL reference to System.DirectoryServices. Or create the CommonADFunctions.vb file in App_Code to start with.

.net library class not updated in web page

I just created a new web site in Visual Studio 2012. I also created a new Class Library. I added a class, another class inside that first class and then a method. I compiled the library and added as the reference in the bin folder for the web site.
In a aspx.cs page I can now see my new method. Great. I go to create a second method in the same location, go back in my aspx.cs page, can't see it. It says there is definition for this method.
I compile the class library just in case. Can't see it. I clean the solution, build the whole solution, still can't see it.
I actually have to remove the reference from the bin folder of the web site and add it back so that now I can see the second method.
I worked on another project where (I believe) things were set up the same way (web site + class library) and by typing the new method in the class and saving the file was enough to reach this method from an aspx page.
What can I change in my solution so that the new method in the class library can be seen directly?
ps: the aspx.cs page has using myLibrary; at the top;
Thanks
Make sure in your solution that you are using the References folder. Right-click it, choose "Add Reference" and then use the Solution tab to reference your class library project. Taking this approach, every time you update your class library the web project will automatically receive the update.
It sounds like you might be adding the binary to the BIN folder of the website project and then referencing it from there?
First of all check if build is going to debug folder or bin folder. If you are referencing project then make sure that build order is correct and dependency set accordingly. Also check for class library or project referred solution is not building on client profile, select targeted framework.

Copying/Moving App Code to New Project

Ok, I am in the process of breaking apart of intranet application (VS 2010 Web Site, ASP.NET Web Forms with VB code behind). During this process i'm trying to convert some of our our app_code files in to WCF rest service in a new project. However, when I copy or "add existing" vb files into the new "services" project. I get tons of errors including...
error BC30002: Type 'XXX' is not defined
warning BC40056: Namespace
or type specified in the Imports 'System.ServiceModel' doesn't
contain any public member or cannot be found. Make sure the
namespace or the type is defined and contains at least one public
member. Make sure the imported element name doesn't use any aliases.
From what i've read it may (or may not) have something to do with Assemblies and references that I just have very little knowledge on. I have added the namespaces from the current web.config to new project's web.config, and the files are identical. So there is something in the background that needs to be added to the new project I just don't know what its.
PLEASE HELP!!
THANKS
JOSH
UPDATE 1
So one of the errors i'm getting = "error BC30002: Type 'MailMessage' is not defined." Which is a namespace that is added to the web.config, which apparently isn't being picked up??? IDEAS?
I've come across a similar issue - not sure what the reasoning behind it is, but I got around it by creating a new .vb class with the same name as the one you want to copy, then copy and paste all the text from the old one to the new one. Save it, then it seems to like it - it didn't like me copying and pasting the actual file, nor did it like me adding existing files.
Hope this helps :/
This issue seems to be tied to the the differences between Web Application Projects versus Web Site Projects. Eventually I had to add different namespaces on the new project level.
Here is more info about WAP vs WSP....
http://msdn.microsoft.com/en-us/library/dd547590(v=vs.110).aspx

where should inheritance classes be stored?

I want to create classes that can be inherited by other classes to use along my project.
i.e. i want to create a separate class for each Mail method, each class will implement differently the Compose method, and more Mail classes will be created in the future.
My question is - Where should I create these classes/interfaces? in the App_Code ?
I would suggest creating a separate assembly and include that assembly as a reference in your Web Project. That will enable you to write independent test cases against your classes / interfaces.
Other than user controls you can store source files in App_Code. In your case you can store Interfaces/classes in App_Code. You can have multiple folders for group of class/interfaces inside the AppCode.
AppCode
You can store source code in the App_Code folder, and it will be
automatically compiled at run time. The resulting assembly is
accessible to any other code in the Web application. The App_Code
folder therefore works much like the Bin folder, except that you can
store source code in it instead of compiled code. The App_Code folder
and its special status in an ASP.NET Web application makes it possible
to create custom classes and other source-code-only files and use them
in your Web application without having to compile them independently.
Edit: You may see this discuss: How to organize ASP.NET app_code folder?

Convert Class to Class Library

I'm trying to follow along with Paul Sheriff's e-book "Fundamentals of N-tier" which is really good so far.
At the end of chapter 2 he says we should break the classes we have created into separate class libraries and that these libraries(dlls) can then be used from any application. The book doesn't explain how to do this.
I have the classes built, but I don't know how to convert them to Class Libraries and reference them in my project.
===========================================================
Thanks for everyone's help I really appreciate it.
I've created the class library in the same project and added a reference to it.
DataCommon is the name of the Class Library
DataLayer is the class
GetDataTable is a method in the class
how do I access this method from the web project.
I added a "using DataCommon;" statement at the top of the class that I'm trying to access the class library in. I get a "the type or namespace could not be found" message
Start by adding a new project to your solution of type Class Library.
(source: c-sharpcorner.com)
Then move those classes to this project.
Finally reference the project in the ASP.NET site.
Just cut out the code you want to reuse and copy into a new dll project. Then reference that dll project from your application and include the namespace anywhere you want to use those classes. There isn't any other magic involved. Just cut from one project and put in another.
Create a new "class library", a project type when creating new project. Then just copy the class into the project, ensuring you are changing the namespace to the appropriate namespace.
I have the classes built, but I don't
know how to convert them to Class
Libraries and reference them in my
project.
Create a new project of type "Class Library" and add your class files to that.
To reference them from your project, you just right click the project in solution explorer and "Add Reference". If the you've got both projects in the same solution, you can then click the Project tab and make your selection. If not, then click the Browse tab and navigate to the bin/debug or bin/release folder from your class library project which contains the compiled dll. (you must have built your class library project for the compiled dll to exist, of course)

Resources