I've posted this issue in another forum, but I believe that this is the real place to discuss it.
I am trying to share variables between multiple WebMethods in a single Web Service Application. I have the following code in my WebService right now:
[WebMethod(EnableSession=true)]
publicvoid
HelloWorld2(int
thirdNum, intforthNum)
{Session["Var1"]
= thirdNum;
Session["Var2"]
= forthNum;
return;
}
[WebMethod(EnableSession=true)]
publicint
HelloWorld3()
{ int
var3 = (int)Session["Var1"]; int
var4 = (int)Session["Var2"];
return
var3 * var4;
}
When I debug/trace this code, it works great, however it fails when I publish the code. What happens is that I get an unhandled exception on the line that defines var3 -> System.Web>Services.Protocols.SoapException: Server was unable to process request.
---> System.NullReferenceException:Object reference not set to an instance of an object.
Could someone please explain to me what I am doing wrong?
This is strange. My web service looks rather much like yours, and is targeting .Net Framework 3.5, yet it doesn't work properly when I call it with code that is similar to your call-out (I'm using the Console Application Template).
Can you try writing it with the Console application template and give it a go?
I'll see what happens when I try the WCF.
Update: I tried using the WCF Application template and constructed a similar design as your client application. I still get the same error as I did when trying a Console Application template. This is frustrating.
I found out what my problem was -- I brought my webservice in as a Service Reference instead of a Web Reference. This mistake made me not able to utilize cookies and store the Session variables.
I know i'm chiming in late here, but the correctness of session is something that's quite
debatable for browser based applications, let alone web services. IMO, if you need to rely upon session state for state managemt in a service then you're design is wrong.
rjmyers
Member
2 Points
15 Posts
How to use Session variables in a Web Service having multiple WebMethods?
Jan 22, 2013 08:05 PM|LINK
I've posted this issue in another forum, but I believe that this is the real place to discuss it.
I am trying to share variables between multiple WebMethods in a single Web Service Application. I have the following code in my WebService right now:
[WebMethod(EnableSession=true)]
public void HelloWorld2(int thirdNum, intforthNum)
{Session["Var1"] = thirdNum;
Session["Var2"] = forthNum;
return;
}
[WebMethod(EnableSession=true)]
public int HelloWorld3()
{
int var3 = (int)Session["Var1"];
int var4 = (int)Session["Var2"];
return var3 * var4;
}
When I debug/trace this code, it works great, however it fails when I publish the code. What happens is that I get an unhandled exception on the line that defines var3 -> System.Web>Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException:Object reference not set to an instance of an object.
Could someone please explain to me what I am doing wrong?
Thanks, Bob
abamboria
Member
149 Points
92 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 01:08 PM|LINK
Hey Bob,
When I publish your code on iis7. it's run. do you want your code publish on iis or another server. let me know. if i am going different direction.
Mark as Answer:- if the post helped you...
rjmyers
Member
2 Points
15 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 02:23 PM|LINK
I'm trying right now to run also on IIS7. How did you get it to run? (I'm new at this, so please bear with me).
right now, my client code call out for the second webmethod is a line like:
int var3 = webservice.HelloWorld3();
This line is highlighted with the error message that points to my second webmethod's assignment.
I'm confused...
abamboria
Member
149 Points
92 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 02:41 PM|LINK
Ok, I am sharing my code with you.
Default.aspx.cs :- public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string num1 = TextBox1.Text; string num2 = TextBox2.Text; int thirdNum = Convert.ToInt16(num1); int forthNum = Convert.ToInt16(num2); Service ser = new Service(); ser.HelloWorld2(thirdNum, forthNum); int result = ser.HelloWorld3(); TextBox3.Text = result.ToString(); } } Default.aspx:- <body> <form id="form1" runat="server"> <p> First Number <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> </p> <p> Second Number<asp:TextBox ID="TextBox2" runat="server" style="margin-left: 17px"></asp:TextBox> </p> <p> Result <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </p> <p> </p> <p> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" style="margin-left: 119px" Text="Button" Width="100px" /> </p> <p> </p> </form> </body> Service.cs:- public class Service : System.Web.Services.WebService { public Service () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod(EnableSession=true)] public void HelloWorld2(int thirdNum, int forthNum) { Session["Var1"] = thirdNum; Session["Var2"] = forthNum; return; } [WebMethod(EnableSession=true)] public int HelloWorld3() { int var3 = (int)Session["Var1"]; int var4 = (int)Session["Var2"]; return var3 * var4; } }Mark as Answer:- if the post helped you...
abamboria
Member
149 Points
92 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 02:50 PM|LINK
Just copy the code. It will run. If you satisfy, Please marked as answer my post.
Mark as Answer:- if the post helped you...
rjmyers
Member
2 Points
15 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 04:44 PM|LINK
This is strange. My web service looks rather much like yours, and is targeting .Net Framework 3.5, yet it doesn't work properly when I call it with code that is similar to your call-out (I'm using the Console Application Template).
Can you try writing it with the Console application template and give it a go?
I'll see what happens when I try the WCF.
Update: I tried using the WCF Application template and constructed a similar design as your client application. I still get the same error as I did when trying a Console Application template. This is frustrating.
-Bob
rjmyers
Member
2 Points
15 Posts
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 23, 2013 06:32 PM|LINK
I found out what my problem was -- I brought my webservice in as a Service Reference instead of a Web Reference. This mistake made me not able to utilize cookies and store the Session variables.
Thanks for your help!
BrockAllen
All-Star
28052 Points
4996 Posts
MVP
Re: How to use Session variables in a Web Service having multiple WebMethods?
Jan 24, 2013 01:11 AM|LINK
I know i'm chiming in late here, but the correctness of session is something that's quite debatable for browser based applications, let alone web services. IMO, if you need to rely upon session state for state managemt in a service then you're design is wrong.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/