I think that the original question was mis-understood. I have two variables defined at the same level as the webmethods:
public int32 var1, var2;
[webmethod] // first web method
...
[webmethod] // second web method
I want to pass data between web methods, where input is supplied to the first webmethod, but massaged and/or retrieved via the second webmethod by the client software.
[WebMethod(EnableSession=true)] publicint
HelloWorld3(){ int
var3 = (int)(Session["Var1"]); int
var4 = (int)(Session["Var2"]); return
var3 * var4;}
This works great if I just debug/run the web service code. However, when I publish and then try to run the client code, I get the following error message:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at WebService2.Service1.HelloWorld3() in C:\Users\a0171964\Documents\Visual Studio 2010\Projects\WebService2\WebService2\Service1.asmx.cs:line 51
It's pointing to the assignment of var3 in the second web method.
rjmyers
Member
2 Points
15 Posts
Can you have global variables passing data between a web services' two webmethods?
Jan 17, 2013 06:06 PM|LINK
I am needing to pass data from one web method to another in the same web service application code. Is this possible?
thanks.
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 17, 2013 06:49 PM|LINK
Yes. Add parameter to the web method - this will be the "data". Or put into user Session / or in Application/Cache /static variable.
rjmyers
Member
2 Points
15 Posts
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 17, 2013 07:31 PM|LINK
Can you give an example of this?
I've defined "public int var1, var2;" before I start my [WebMethod] codings, and I'm not sure how to add the parameter to the web method.
Thanks,
Bob
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 18, 2013 09:09 AM|LINK
Just add between the paranthesis of the function that represents the web method.
rjmyers
Member
2 Points
15 Posts
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 18, 2013 04:48 PM|LINK
I think that the original question was mis-understood. I have two variables defined at the same level as the webmethods:
public int32 var1, var2;
[webmethod] // first web method
...
[webmethod] // second web method
I want to pass data between web methods, where input is supplied to the first webmethod, but massaged and/or retrieved via the second webmethod by the client software.
How can I do this?
-Bob
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 19, 2013 07:19 AM|LINK
You can put into Session of the User
or
add as parameters to the webmethod.
rjmyers
Member
2 Points
15 Posts
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 21, 2013 06:07 PM|LINK
I've coded it up as:
[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;}
This works great if I just debug/run the web service code. However, when I publish and then try to run the client code, I get the following error message:
System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at WebService2.Service1.HelloWorld3() in C:\Users\a0171964\Documents\Visual Studio 2010\Projects\WebService2\WebService2\Service1.asmx.cs:line 51
It's pointing to the assignment of var3 in the second web method.
What am I doing wrong?
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 22, 2013 02:25 PM|LINK
What's here?
rjmyers
Member
2 Points
15 Posts
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 22, 2013 08:10 PM|LINK
It's the line in the second Method:
int var3 = (int)Session["Var1"];
-Bob
ignatandrei
All-Star
134832 Points
21599 Posts
Moderator
MVP
Re: Can you have global variables passing data between a web services' two webmethods?
Jan 23, 2013 07:33 AM|LINK
and the caller of WebServices preserves and transmits the cookies to be authernticated as same user?( cookie container)?