Can't call static class from different class library? - asp.net

I'm using the .NET 4 framework, and I have a static class with static functions in an asp.net web application.
I have a second class library project. The class library project wants to call the static method in the web application. Intellisense works, but then the compiler reports that
"The name [MyClassName] does not exist in the current context".
Can I make this call, or this not allowed?
PS, the static class is in the /App_Code folder.
Thanks!

Is it not possible to refactor the class out of the web project? Referencing a web project from a class library sounds awkward. If your class does not contain web-specific code then you could pop it into that other class library you mention or create a new one. Should it reference web-specific libraries something like MyProject.MyLib.Web could do the trick.

You need to add a reference to the project containing the class you want to access.

You need to ensure that you are using the full name of the class (including the namespace).
Alternatively add a using directive with the namespace to your code file.

If your target framework is .Net Framework 4.0 Client Profile change it to .NET Framework 4 and rebuild the solution.

Related

Access .NET Core Configuration Class From Another Assembly

In The Olden Days
In a web.config file, settings could be placed in an appSettings section like so:
<appSettings>
<add key="mysetting" value="123"/>
</appSettings>
Even though my web.config file was in my web project, any assemblies/libraries used in that project could access the settings using:
ConfigurationManager.AppSettings["mysetting"]
Today (and the problem)
I am starting to use .NET core, and just like before, I have assemblies/libraries that are not web projects in of themselves and need to access various configuration settings.
Microsoft's Configuration documentation (https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration) along with all of the other examples I can find, have the configuration class being consumed by a controller and don't provide any guidance on how to make it work with a class in another assembly/library that is not a controller.
For ONE example, if I have a custom attribute that I can decorate a class with and that custom attribute is defined in another library (not in a web project) and it needs to access a configuration setting, how do I do that today? I can't pass in anything to a constructor in such an instance either.
I'm going to assume you're talking about a ASPNET Core project as you specifically mention web.config.
Here's what you need to do.
IOptions<FooSettingsClass> is usually configured at application start which means that it's available at runtime with code that looks something like this.
// Adds services required for using options.
services.AddOptions();
services.Configure<AppSettings>(Configuration.GetSection("FooAppSettings"));
The easiest way is to have the framework inject it through the constructor. Typically you'll see it (as you mentioned) being injected in the controller like this:
class FooController : Controller {
public FooController(IOptions<FooSettingsClass> settings) { .
//..
}
}
If you need to access this configuration is say, a service, then you simply have a constructor, in a different assembly, which accepts those options. So:
public class SomeServiceInAnotherAssembly {
public SomeServiceInAnotherAssembly(IOptions<FooSettingsClass> settings) {
//..
}
}
This obviously means that your FooSettingsClass class needs to be outside of your ASPNET Core project (to avoid circular dependencies), but this is one way of propagating your configuration without writing any code, which is what I've seen other developers do. To me, writing code is a hoop jumping solution bound to have bugs.
Don't forget that your class (in this exampe SomeServiceInAnotherAssembly) needs to be registered at startup, i.e. services.AddScoped<SomeServiceInAnotherAssembly>();
The nice thing about this approach is that it makes your classes testable.
In 8-2017 Microsoft came out with System.Configuration.Manager for .NET CORE v4.4. Currently v4.5 and v4.6 preview.
Install this nuget package. Add directive to a code file
using System.Configuration;
Now, you can do your
var val = ConfigurationManager.AppSettings["mysetting"];
There is one trick for web sites though - you no longer use web.config for application settings and configuration sections. You use app.config as well as other types of projects. But if you deploy in ISS, you might need to use both. In web.config you supply strictly ISS-related entries. Your app-specific entries go to app.config

Adding script/web resources for ScriptControl implementation

I'm not quite familiar with asp.net ajax, so my question might be a silly one.
I'm working on the asp.net ajax control library for self-educational purposes and it is not quite clear to me how to add script/web resources to my class library project.
I've added the following lines to the AssenblyInfo.cs
[assembly: WebResource("BlackGoat.UI.Controls.TooltipControl.Tooltip.js","text/javascript")]
[assembly: ScriptResource("BlackGoat.UI.Controls.TooltipControl.Tooltip.js","BlackGoat.UI.Tooltip", "BlackGoat.UI.Resource")]
The relative path to the javascript file is Controls\TooltipControl\Tooltip.js, however the directories Controls and TooltipControl are not namespace providers so I'm not sure how should I specify the the js file 'namespace' in the AssemblyInfo.cs
Any clarification would be appreciated.
Thanks in advance.
Embedded resources do use folder names as namespaces, so if your default namespace is BlackGoat.UI, and folder is Controls\TooltipControl, it would be namespaced as BlackGoat.UI.Controls.TooltipControl... that's how the framework does it internally.
Check this out: http://www.codeproject.com/KB/dotnet/embeddedresources.aspx
To reference it in your AJAX control, setup the ScriptReference to use the full name as the type, and the name of the assembly (rather than using the path option).

How to create a DLL from a WSDL for use in DNN Module

I'm creating a DNN 4.9.5 module and need to create a DLL from a WSDL (Doba API). I've created a separate Class Library project in my DNN solution with Class1.vb in it. What do I need to include in my class from the WSDL file? Obviously, I won't be going with Class1.vb, but just need a gentle push as to how to get this going.
Thanks much for your guidance.
This is not a problem. Simply create your class library and then use "Add Service Reference" to point to the WSDL.
Do not use "Add Web Reference" unless you have to. Microsoft considers that to be "legacy technology".
Why do you want to create a dll to access the webservice? If you just want to use it (assumung that you are using ms-visualStudio) you need to add a webservice reference to your separate Class Library project .
That will create the sourcecode for a wrapper class to call the webservice described by wsdl.
See msdn:How to: Add and Remove Web References

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)

Bulk Move ASP.NET Pages Into A New Namespace?

I have an C# ASP.NET Web Application Project where all the pages are in the global/default/top-level namespace. (I have no explicit namespace declarations. And when I look at my compiled web application's DLL in Red Gate's .NET Reflector, I can verify that all the classes are in the top-level .NET namespace.) Is there any good, automated way to move all the pages and custom controls into a new namespace, say "MyWebApplication"? Ideally it'd be nice to do it with just Visual Studio, but I'd be open to considering a commercial refactoring tool if necessary.
I thought maybe by setting the "Default namespace" property in the project's Application properties I could get the compiler to implicitly put all pages into the specified namespace, but this appears not to be the case; this "Default namespace" setting seems to make Visual Studio insert explicit namespace declarations into new pages, rather than implicitly affecting the namespace of any existing pages.
If it matters, my immediate motivation here is to try to run a static analysis tool (CAT.NET) on my web application; the tool seems to have a quirk or two with code in the global namespace.
Chris it may look silly as you are ready to pay for the refactoring tool to do it. But consider this:
The namespace is declared in the code-behind file by default as the project/application name. So if you project name is MyWeb the default namespace will be
namespace MyWeb
{
public partical class MyWebPage....
Now this is refered in ASPX page as follows:
<%Page ... CodeBehind="MyWebPage.aspx.cs" Inherits="MyWeb.MyWebPage"
You only need to change these two things to put into effect the namespace migration. And frankly this can be achieved using Find/Replace dialog. Please make a copy of your complete solution and try it!!
Cheers

Resources