I am trying to access a global variable that was set in my Global.asax section of my project. When include the namespace in my DAL and access the variable visual studio complains. Here is the code
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
Application["NHSessionFactory"] = CreateSessionFactory();
}
And here is my DAL logic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using NHibernate;
namespace Data.DAL
{
public class DB
{
private ISessionFactory session;
public DB()
{
ISessionFactory session = (ISessionFactory) Application["NHSessionFactory"]; //problems here
}
public ISessionFactory GetSession()
{
return session;
}
}
}
A screen shot of the error:
How would I access that variable that was set in Global.asax?
Yes you can use the Application variables same way as asp.net
Example:
System.Web.HttpContext.Current.Application.Lock();
System.Web.HttpContext.Current.Application["NHSessionFactory"] = "Something";
System.Web.HttpContext.Current.Application.UnLock();
Related
Solution Explorer
I can get current method name, but currently I can't get really current class name.
From reference: C# getting its own class name, accepted answer is this.GetType().Name.
Test.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication1
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string currentMethodName = MethodBase.GetCurrentMethod().Name; // "Page_Load"
string currentClassName1 = this.GetType().Name; // "test_test_aspx" // How to get "Test" only?
string currentClassName2 = this.GetType().FullName; // "ASP.test_test_aspx" // How to get "Test" only?
}
}
}
Try with this:
string className = MethodBase.GetCurrentMethod().DeclaringType.Name;
I have used System.DirectoryServices.ActiveDirectory but when I tried to use Domain.Name or Domain.GetDomain methods, I get an error
'Domain' does not contain a definition for 'GetDomain'.
Here's my code:
using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.DirectoryServices.ActiveDirectory;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DirectoryContext s = new DirectoryContext(DirectoryContextType.Domain, "my domain", "myuser", "mypass");
Domain domain = Domain.GetDomain(s);
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;
foreach (Domain objDomain in domains)
{
Response.Write(objDomain.Name.ToString());
Response.Write("<br/>");
}
}
}
What's the problem?
According to MSDN, GetDomain() is a static method, which means it cannot be accessed outside of a static context - that is why you're getting that particular error. To prove this was the case, I just copied and pasted your code into a console app, which by default will be run from a static void method called Main() :
using System;
using System.DirectoryServices.ActiveDirectory;
namespace LDAPTEST
{
public class Program
{
static void Main(string[] args)
{
DirectoryContext s = new DirectoryContext(DirectoryContextType.Domain, "DC=hq,DC=xxxxxxxxx,DC=com", "TestUsername", "TestPassword");
Domain domain = Domain.GetDomain(s);
Forest forest = domain.Forest;
DomainCollection domains = forest.Domains;
foreach (Domain objDomain in domains)
{
Console.Write("");
}
}
}
}
This will compile and run. However, since you're developing an Asp.Net web application, I would advise using the lower level System.DirectoryServices.Protocols or even the System.DirectoryServices.AccountManagement namespace to accomplish your task.
I am trying to set my config from code according to this post Changing .net Membership ConnectionString
In this post, it has this code:
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
// Get your new connnection string name and then overwrite the base here:
config.Item["connectionStringName"] = "MyNewConnectionStringName";
base.Initilize(name, config);
}
I made my class as such:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.Security;
using System.Collections.Specialized;
namespace WebUsers
{
internal class MembershipOverride : SqlMembershipProvider
{
public override void Initialize(string name, NameValueCollection configx)
{
// Get your new connnection string name and then overwrite the base here:
configx.Item["connectionStringName"] = "MyNewConnectionStringName";
base.Initilize(name, config);
}
}
}
I cannot get Item or Initialize to be recognized.
It is doing this:
Anyone see what I am doing wrong?
I cannot get Item or Initialize to be recognized.
There is a typo. You call base.Initilize instead of base.InitiAlize.
To access the collection element, try just
configx["connectionStringName"] = "MyNewConnectionStringName";
A bit rusty here with regards to WCF Services.
I have a custom class named cSecurity.cs which does some custom functions.
I want to use this custom class inside my Service:
This is the App.svc.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace AppServices
{
public class App : IApp
{
public cSecurity _csec;
public string GetItems(int agentID, string agentName)
{
// Need to use some functions from the cSecurity class here???
return _csec.getItems();
}
}
}
This is the cSecurity.cs class:
using System.Collections;
using System.Configuration;
using System.Net;
namespace AppServices
{
public class cSecurity
{
// Some functions defined here....
public string getItems(){
return string.Empty;
}
}
}
But I am getting an error on the line:
public cSecurity _csec;
"The type or namespace name 'cSecurity' could not be found."
This seems pretty trivial but I seem to be lost here.
Appreciate any insight. Thanks.
For some reason, the solution for this was for me to close and restart VS. Not sure what caused the class for it to be not referenced by VS.
In my demo solution, I have a two projects DataAccess and WebInterface.
In DataAccess project, I have one EF for Users and a class file UserDataAccessService.cs
Code in UserDataAccessService.cs:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Data.EntityClient;
using System.Data.SqlClient;
using System.Data;
using DataAccess.User;
namespace DataAccess.User
{
public sealed class UserDataAccessService
{
private string efConnectionString;
private string portalConnectionString;
public string ConnectionString
{
get { return efConnectionString; }
}
public UserDataAccessService(string portalConnectionString)
{
this.portalConnectionString = portalConnectionString;
this.efConnectionString = GetConnectionString(portalConnectionString);
}
private string GetConnectionString(string portalConnectionString)
{
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SqlClient";
entityBuilder.ProviderConnectionString = portalConnectionString;
entityBuilder.Metadata = "res://*/User.ProgramModel.csdl|res://*/User.UserModel.ssdl|res://*/User.UserModel.msl";
return entityBuilder.ToString();
}
public IEnumerable<User> GetUsers(int? userId, bool? status)
{
using (Users objectContext = new Users(efConnectionString))
{
return objectContext.GetUsers(null, null);
}
}
}
}
Now in WebInterface project, I simply have a ManageUsers.aspx page on which I am trying to fetch list of users by below code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using DataAccess.User;
namespace WebInterface
{
public partial class ManageUser : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserDataAccessService usrObj = new UserDataAccessService(ConfigurationManager.ConnectionStrings["portalConn"].ToString());
List<User> userLst = new List<User>();
userLst = usrObj.GetUsers(1, null).ToList();
}
}
}
Problem:-
When I am building the solution then I am getting the below error:-
Error 1
The type 'System.Data.Objects.DataClasses.EntityObject' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
D:\Practice\PTP\UI\WebInterface\User\ManageUser.aspx.cs
18 13
WebInterface
To fix it, I have included it System.Data.Entity.dll in reference folder of WebInterface project which has fixed the issue.
My Question:
If you look into my the code then you can that code related EF is all in DataAccess project which has all required dll reference in it required for classes related to EF.
and in WebInterface project I have included DataAccess.dll and in it .aspx page I am simply calling it function to the fetch data.
Then why it requires System.Data.Entity.dll in WebInterface project too?
Regards
Varun
I am only 99% sure cause you are using some old school EF :) and you didn't share User class but... I think it's the User class that is EntityFramework aware. Is it the case that User inherits from EntityObject?
You can avoid that by mapping data from entity classes to you UI classes. But that takes all the magic from using EF.