Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I have a .Net assembly, written in C#, that is doing radically different things when it is asked to populate a reference type in a console app vs. an ASP.Net web page.

The code in either case looks like this:

//-------In assembly

//thing is NOT IDisposable
class Thing
{
public int x; 
};

class C
{
public void foo(string s, out Thing thing)
{
thing = new Thing();
thing.x = blah(s); //cleverly turn English to integers, e.g. "three" -> 3, etc. 
}

//--------Client code, can be either C# or ASP.Net

List<thing> things = new List<thing>(); 
string[] string_array = new string_array{"zero", "one", "two",... , "five"};
C c = new C();
Thing thing = null; 
foreach(string s in string_array)
{
    c.foo(s, out thing); 
    things.Add(thing);
    thing = null;
}

So I am populating a list of unique 'things' by calling a fxn.

When I run as a console app in C#, all works as I would expect: I get a list of 'things' with x = 0,1,2 ... etc.

When I call the same assembly from ASP.Net, with this code embedded in a web page, I get a list of 'things' with x always set to the last value in the array. In other words, the assembly seems to save a reference to the object and overwrites early values with late ones.

I have tried setting 'thing' to NULL, etc. etc. to no avail. Something deep is happening here ... but my brain is set on shallow ...

The Big Question is, why is this different in ASP vs. C#?

Eric
Posted
Updated 4-Apr-11 14:51pm
v5

You are probably not storing the list between post backs.

Once a page is rendered and sent to the client, it (and all it's properties and variables) cease to exist.

Try storing it in Session state.
 
Share this answer
 
v2
Comments
EricFowler 4-Apr-11 22:05pm    
All of the code (except the assembly) is in a single button handler, so I doubt it is a postback issue, unless there is something about postback I don't understand. Meantime I will try Session state, as soon as I figure out what that is.

Thanks

Eric
Sergey Alexandrovich Kryukov 5-Apr-11 17:49pm    
Exactly. Each postback is essentially a separate runtime. As I understand, the piece of posted code is run in a single run-time, right?..
--SA
Steve Wellens 4-Apr-11 23:45pm    
I couldn't tell from the context that it was all in the same post back...so your conclusion is correct: It shouldn't be an issue.
Sergey Alexandrovich Kryukov 5-Apr-11 17:49pm    
Something is not shown to us, that's it.
--SA
I cannot see anything which can be wrong with ASP.NET specifically. I think your problem is incorrect or incomplete reporting of the problem. I should remind you that you can run your project under debugger even in ASP.NET.

Your code is incomplete and looks not very good. Here is what I see:

1) What's the purpose of the class C? This code would fit well as a member of Thing.

2) You by reference parameter makes no sense at all. What's the return parameter for? You use void return anyway. Why?! Should be:

public Thing foo(string s)
{
    thing = new Thing();
    thing.x = blah(s); 
    return thing;
}


3) Instead of having the method blah (are you really using such cheesy names :-)?), you should better make it a constructor of Thing.

4) If blah is not using "this", it should be static, as well as foo. If there is nothing else if C, it should be a static class.

5) The method foo should throw exception. Not every string s could be interpreted as English numeric, right?

I would say you're not comfortable enough in simple programming tasks to go if for ASP.NET. At the same time, I cannot see anything specific to ASP.NET which can be "specifically" wrong here. Maybe, you need to show more if your code — you're maybe doing something wrong in other part of the code, something I cannot even imagine…

—SA
 
Share this answer
 
v3
Comments
EricFowler 4-Apr-11 22:05pm    
Thanks for the advice. I have been programming in C and C++ for nearly twenty years, and in C# for a couple of years, but am a relative newcomer to ASP and web coding. This is of course an abstracted and simplified version of a real-world problem. I don't really name functions, types and objects 'C' and 'Thing' and 'foo' and 'blah'. Changing the signature of foo() is not really an option - it already returns a status variable.

Eric
Sergey Alexandrovich Kryukov 5-Apr-11 13:13pm    
I understand. "Changing the signature of foo() is not really an option - it already returns a status variable" -- is not good. Bad signature, makes no sense. If you're asking for an advice, get ready to change, otherwise the question makes little sense.
--SA
EricFowler 5-Apr-11 16:55pm    
I don't see how object lifetime issues are going to go away by making that change. If I declared foo() to return a Thing I would still have the same problem, wouldn't I?
Sergey Alexandrovich Kryukov 5-Apr-11 17:10pm    
One step at a time. It does not solve the problem, no.
I don't see anything that could be specific to ASP.NET. Perhaps showing more code will help to understand it.
--SA
EricFowler 5-Apr-11 17:39pm    
From the original post:

"So I am populating a list of unique 'things' by calling a fxn.

When I run as a console app in C#, all works as I would expect: I get a list of 'things' with x = 0,1,2 ... etc.

When I call the same assembly from ASP.Net, with this code embedded in a web page, I get a list of 'things' with x always set to the last value in the array. In other words, the assembly seems to save a reference to the object and overwrites early values with late ones.

....

The Big Question is, why is this different in ASP vs. C#?"
I have solved this problem and will post the answer so others can Google it. The assembly was holding a reference to the Thing that was causing the issue. I still don't know why this did not fail in the standard Windows app.

Thanks to all who replied.

Eric
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900