问题
So I have redone some of the code to the previous question, however the same underlying issue remains. What i am trying to do is this: When my lightswitch application launch through Application.cs
i am using a method Application_LoggedIn()
to call a function in my Common Project to get all the location a user has access to (this data is stored in a table).
Client Project - Application.cs
partial void Application_LoggedIn()
{
Secure.Membership aa = new Membership();
aa.membership();
}
Common Project - After calling the method, i then run my queries in the membership() function to get all the location a user has access to. They are stored in three list: estateList, deptList, groupList;
public void membership() {
List<int> estateList = new List<int>();
List<int> deptList = new List<int>();
List<int> groupList = new List<int>();
var dws = Application.Current.CreateDataWorkspace().sspData;
String uName = Application.Current.User.Identity.Name;
try
{
var qryUser = (from a in dws.aspnet_Users
where a.UserName == uName
select a).Execute().Single();
int membershipId = qryUser.Payroll_MembershipGroup.Id;
var qryOrgMember = (from a in dws.PayrollOrg_MembershipGroups
where a.Payroll_MembershipGroup.Id == membershipId
select a).Execute().ToList();
foreach (var x in qryOrgMember)
{
if (!estateList.Contains(x.Payroll_Organisation.Estate1.ID))
{
estateList.Add(x.Payroll_Organisation.Estate1.ID);
}
if (!deptList.Contains(x.Payroll_Organisation.Estate1.ID))
{
deptList.Add(x.Payroll_Organisation.Department1.ID);
}
if (!groupList.Contains(x.Payroll_Organisation.Estate1.ID))
{
groupList.Add(x.Payroll_Organisation.Payroll_Group1.ID);
}
}
}
catch (Exception e)
{
Debug.WriteLine("***************" + e.InnerException);
}
}
What i would like after doing this is to be able to access the 3 lists (estate,dept,group) from inside my server project so i can use the list to perform filters on my entities as shown below.
partial void Employee_Employs_Filter(ref Expression<Func<Employee_Employ, bool>> filter)
{
filter = e => estateList.Contains(e.Estate1.ID) && deptList.Contains(e.Substantive_Department) && groupList.Contains(e.Payroll_Group1.ID);
}
This is what i want to do and i am having problem. Any suggestion would be welcomed. What i tried to do is to create a static class in my Common Project and set the values however as one user rightly pointed out, i cannot do that as it will be null when i try to call it from inside my Server Project. Please help me out here, this is a big issue i am having with this application and is the only remaining part. Thanks in advance.
************ Filter code on server side* @Duran this is what i had on the server side.
public List<User> membership() {
List<User> aList = new List<User>();
User aUser = new aUser();
var dws = Application.Current.CreateDataWorkspace().sspData;
String uName = Application.Current.User.Identity.Name;
try
{
var qryUser = (from a in dws.aspnet_Users
where a.UserName == uName
select a).Execute().Single();
int membershipId = qryUser.Payroll_MembershipGroup.Id;
var qryOrgMember = (from a in dws.PayrollOrg_MembershipGroups
where a.Payroll_MembershipGroup.Id == membershipId
select a).Execute().ToList();
foreach (var x in qryOrgMember)
{
aUser.estateId = x.estateId
auser.deptId = x.deptId;
aUser.groupId = x.groupId;
aList.add(aUser);
}
}
catch (Exception e)
{
Debug.WriteLine("***************" + e.InnerException);
}
return aList;
}
public class User{
public estateId{set;get:}
public deptId{set;get:}
public groupId{set;get:}
}
Now in my filter i would call the method membership() that returns a list<User>
partial void Employee_Employs_Filter(ref Expression<Func<Employee_Employ, bool>> filter)
{
var x = membership();
estId[int] = new [x.count()];
deptId[int] = new [x.count()];
groupId[int] = new [x.count()];
for (int i=0; i<x.count();i++){
estId[i] = x.elementAt(i).estateId;
deptId[i] = x.elementAt(i).deptId;
groupId[i] = x.elementAt(i).groupId;
}
filter = e => estId.Contains(e.Estate1.ID) && deptId.Contains(e.Substantive_Department) && groupId.Contains(e.Payroll_Group1.ID);
}
I have to put a filter similar to the one above on ALL my tables, thats when i get the stackoverflow error. It works when its on ONE table alone but not when it is on all the tables. What am i doing wrong?
回答1:
Even though the code in the Common project is "shared" between the Client & Server projects, any variables created there have their own separate instances, one in the Client project, & one in the Server project.
So you're not really accessing a single shared variable at all, which is why when you set a value on one tier, the value on the other tier doesn't reflect that change in value, because they're actually two distinct instances of the variable.
来源:https://stackoverflow.com/questions/21170173/access-variables-set-in-common-project-from-server-project-in-lightswitch