ASP.NET - Share code - asp.net

I have a web application that contains a bunch of classes in the App_Code folder. I compile the project and publish it to the IIS server.
I want to share some of the code in the app_code folder with another application on the server and therefore I think I need to register an assembly in the GAC.
What is the best way to do this? I have read this article: http://msdn.microsoft.com/en-us/library/Aa479044, which suggests a number of options?

Put the code in a class library, and add the library as a project reference to both applications.
Side Note:
If you need to access the request or response, etc. import the Sysyem.Web library and use the HttpContext object. This will give you most, if not all the information available to the page.

You'll have to move the code into a separate project, which will output a library.
If you have any references to dlls related to the ASP .Net or web in general, you can reference them from that library.
The code might not compile in the first, but you can refactor it, it really depends on how tight is with what is in App_Code.
You can then reference that library on the Web Site (you'll have to refactor here too some things). The library, once is signed, can be added to GAC also.

The solution for me was to expose the shared functionality in a web service.

Related

C# .net, how to deploy class library dll when using 3rd party component references

This is my first post so here goes...
I have a c# .net class library I've build and been using in production for some time now. The project (using .net framework 3.5, output type Class Library, strongly named) didn't have any non-framework/standard references. It's set to be used as a COM component and we're using it against classic ASP websites. To deploy I'm setting the Build -> Configuration Manager to Release, building, copying the bin/release/PrintJob.dll file to our web servers. I then use regasm "c:\path info\PrintJob.dll" /tlb /codebase to register this on the server. At that point our classic ASP sites can successfully create an instance of the object and use it's functionality.
Today I've been tasked with implementing a new piece of functionality (encryption/decryption) using a 3rd party .net component. I'm used to working with ASP.NET applications more so that class libraries and COM components. So I update my existing project like I would a web project. I have the new dll on my computer, I first had to give it a strong name itself so my class library would accept it, I add the reference to the customCrypto.dll file in my application, reference the namespace in my class file(s), test it, build it, and it works fine locally.
I'm attempting to deploy it using the same method as previously. I go to the bin/release/ folder, copy the PrintJob.dll file to my web servers, register it the same, and when my sites attempt to use it now, it's throwing an exception that it can't find my customCrypto.dll file (the exceptions displays the full strong name assemply attributes).
So my question is am I doing this correctly? In a web project, the customCrypto.dll file is going to be moved along with my site automatically during the publish. So the dll automatically ends up with the project no problem. But with this class library, do I need to also copy the customCrypto.dll file to the other servers? When the build happened, how come my PrintJob.dll assembly didn't include this functionality internally. How do I get the COM component PrintJob.dll to be able to use the customCrypto.dll when deployed? Since the 3rd party customCrypto.dll component is a .net component, should I somehow be adding this to the machine and using it with my application another way?
I've spent the entire day trying to find references to this but all I can find are more direct issues like how to strongly name an assembly, add it as a reference, etc. Any help is greatly appreciated.

SharePoint UserControl without Codebehind: How do I add/consume a Service Reference?

I have a several projects full of .ascx UserControls that contain C# code blocks rather than having codebehind pages. I'm looking to consume a WCF service I've created, and I've added the Service Reference to the project, but I'm having trouble referencing it on the page using various directives.
Is this possible? The UserControls are used inside of WebParts in SharePoint 2010 if that makes a difference. Web service is deployed to _vti_bin if that matters as well, and is definitely operational (I've tested the exposed methods via a console app and through deployable projects which utilize codebehinds).
If this were not being done for SharePoint (yes, it makes a huge difference!) then I'd say that you should create a class library project, add the service references to the class library, and then use the class library from your user controls.
However, since it's SharePoint, you have to be concerned about deployment of DLL files.
I think some variant on my suggestion would work. You would have to sign the class library and place into the GAC, or you should arrange to bin-deploy it. In either case, I believe you'll want to include your controls and this class library in the same Feature.

ASP.NET Class Library

When an ASP.NET application is published using the publish option in Visual Studio a series of DLLs are produced in the BIN folder on the web server. What is the difference between the DLLs generated by Visual Studio specifically for ASP.NET and a standard Class Library?
I am wanting to reuse code in classes that are contained in an ASP.NET application.
One option for me is to convert the ASP.NET classes into a class library and hence the reason for this question. The other option is to use a web service to expose the functionality required by other applications.
There is no difference between DLL's generated by ASP.NET and a standard class library. Although the web application project produces a DLL, which can't really be reused (well it could, but it contains the code-behind of your pages and user controls, but not the markup), every other DLL can be reused.
Using services and implementing Service Oriented Architecture (SOA) is another option too, so it really depends on what your requirements are, and what you are trying to reuse.
The main reason for choosing a class library over built in classes is so they can be shared with other projects, or the DLL could be distributed for further use.
The benefits of a class library that I can think of are:
Tidier project structures
Quicker Project build time
Different versions of your library can be referenced, hence resilient to upgrades
I generally use web services for projects that require communication between applications/servers, rather than wrapping common code.
You should extract the code you want to reuse in a separate assembly and reference it in the web project and in the other project. Don't use the assemblies with the web pages and other application specific code in another application.

problems when make a web service as an alternative to dll

Q:
Recently , i face some problems, i have a dll common among a lot of applications,and any change to this dll require to build it, copy and paste it in each bin folder of these applications ,and add the new reference so i decided to convert this dll to a web service in stead to overcome this overload..
I make a web service application contains set of web services(mapping to each class).
I face some problems here:
In the original dll, there are
classes use methods exist in other
classes in the same dll, and i face problems when i
tried to use web service methods in
other web service class.how to fix
this problem.
In the original dll there are set of
static classes with static
methods.how to make the equivalent in
my web service application.
Any overload method cause a problem
in my web service.
When using Dictionary<string, string>
as a parameter in my method.
Note:the original dll contains the connections to the data base. Is converting it to web service is the optimum solution?which one is faster in my case a web service or the dll?Should i tend to WCF instead of?
thanks in advance.
In my applications I usually have all related projects in the same solution. But when I need to use projects across applications I replace the project for a dll reference.
Because I use Subversion I solve the problem of copying the dll by adding an external property do my libs folder, referencing the build of the dll.
If the external dll is updated very often than you probably need a continuous integration system to handle that for you.
I usually add project reference and keep all my projects under single solution, so I do not have file copy issue. You might be missing project reference, instead you could have used assembly reference. Verify that first.
First advice, don't create a web service if you don't know what it is and how it can be used. People tend to think everything is a web service since creating a web service in WCF is so easy by using visual studio tools. Since you ask like converting DLL to a web service, I assume that you are beginning with learning web service.
All you need is kind of continues integration system otherwise a simple build system which does the build for you and it copies the files wherever depended applications folder. Hence you can save your time for coping files manually. When you do this, you will not have any of those 4 problems you have mentioned.
If you are lazy enough to learn the build system, simply write a batch file that would copy the files for you. I do have batch files which does xcopy files.

ASP.NET Compilation And Code Visibility

How ASP.NET compiles its assemblies generally confuses me. It seems that I cannot program against anything outside the App_Code folder. In the application I am working on I have several server side controls and a few user controls. The user controls are outside the App_Code folder and I cannot refer to them from the App_Code folder. It is a "Web Site" project.
How can I refer to the UserControls from the App_Code directory?
Is this a compilation issue?
Thanks in advance.
Yes, it is a compilation issue, read a bit more about this same question at
http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-appcode-folder.html
Whilst I'm sure suggesting you convert the web site to a web application is something you have considered doing and rejected, it may be the way forward if accessing your user controls is something you're looking to do frequently.
The way that I have gotten around the app_code issue is by putting all of my classes into a separate project and include the dll from that project as a reference in the web project. That also allows me to reuse the classes in other applications. I believe this is a common practice for business logic and data access when building an application using an n-tier environment.
Are you sure you're referencing the namespace which contains the user controls?

Resources