.NET Forum / ASP.NET / Web Services / March 2005
How to mantain the state between 2 call of the same WebService?
|
|
Thread rating:  |
Alessandro Benedetti - 17 Mar 2005 12:13 GMT Hi. I'm calling two methods of a .NET Webservice (A) from another Webservice (B).
The A Webservice is made like this:
[WebService(Namespace="WebServiceA")] public class WSA: System.Web.Services.WebService { private int X = 0;
[WebMethod(EnableSession=true)] public void WM1() { X = 1; }
[WebMethod(EnableSession=true)] public string WM2() { return(X); } }
In B WebService I make only one instantion of the A Webservice's proxy class and then call first method (WM1) and then the second one (WM2) but WM2 always return 0. So, I've used Session to save my variable, but I would like to know if there is another way that doesn't use session that I don't like.
Thank you. Alessandr
Brock Allen - 17 Mar 2005 14:37 GMT WebServices don't inherently support tracking the correlation of discrete calls, as it's intended to be a disconnected messaging mechanism. So, if you want this, you need to build in the correlation yourself. Here's an example:
// My Order Service [WebMethod] string PlaceOrder( ... ) { ... return OrderID; } [WebMethod] string GetOrderStatusOrder( string OrderID ) { ... } [WebMethod] bool CancelOrder( string OrderID ) { ... }
Anyway, simple idea, but notice how the WebMethods track the user? The first call returns an OrderID. This is now the token that identifies the user and for all the other operations the user needs to present this information so the web service knows who it's talking to.
The idea is the same with HTML applications. When you go to amazon.com you need to pass some info in to "remind" the web server who you were so it can continue whatever conversation it was having with you...
-Brock DevelopMentor http://staff.develop.com/ballen
> Hi. I'm calling two methods of a .NET Webservice (A) from another > Webservice (B). [quoted text clipped - 26 lines] > Thank you. > Alessandro Alessandro Benedetti - 17 Mar 2005 15:33 GMT Hello Brock, thank you or your answer. Your solution is correct if I have to work only with primitive type. The problem is that I have to work with a custom class like this example:
[WebService(Namespace="WebServiceA")] public class WSA: System.Web.Services.WebService {
CL1 test;
public WSA() { this.test=new CL1; }
[WebMethod(EnableSession=true)] public void WM1() { test.SomeMethod(); }
[WebMethod(EnableSession=true)] public string WM2() { return(test.SomeValue); }
}
public class CL1 { ... }
> WebServices don't inherently support tracking the correlation of > discrete calls, as it's intended to be a disconnected messaging [quoted text clipped - 50 lines] >> Thank you. >> Alessandro Brock Allen - 17 Mar 2005 16:26 GMT > The problem is that I have to > work with a custom class like this example: So that's fine. I don't see what the problem is then? Every request in creates a new instance of the WSA class. This protocol is not stateful. That's why you're going to have to build in your own correlation mechanism as I mentioned before. This stuff isn't easy -- don't let the VS.NET wizards fool you.
Also, I'd beware of using things like Session, as that relies upon an out of band contract of using cookies from the client. The web service specs don't incorporate these like browsers do. If you do use Sessions/cookies, then what's a java client going to do? What about a perl script client? Where in the WSDL does it to say use cookies? If you have to explain to them outside of your WSDL file that cookies are required on their end, then you're no longer really doing web services... at least in the spirt of the web service specs. You've just built your own XML protocol that sorta looks like SOAP....
-Brock DevelopMentor http://staff.develop.com/ballen
Alessandro Benedetti - 17 Mar 2005 17:24 GMT Hello Brock, I don't know if I've understanding correctly your answer. You are telling me that I can return any object from a WebService?
So in my example I can do this?
[WebService(Namespace="WebServiceA")] public class WSA: System.Web.Services.WebService {
[WebMethod(EnableSession=true)] public CL1 WM1() { CL1 test; test.SomeMethod(); }
[WebMethod(EnableSession=true)] public string WM2(CL1 LocalTest) { return(LocalTest.SomeValue); }
}
public class CL1 { ... }
And if I can, how tha caller that doesn't know the object CL1 can send this parameter?
Thank you. Alessandro Benedetti
>> The problem is that I have to >> work with a custom class like this example: [quoted text clipped - 17 lines] > DevelopMentor > http://staff.develop.com/ballen Brock Allen - 17 Mar 2005 17:51 GMT No, WebServices are not about objects. This is the point I'm trying to make. The Wizards and VS.NET are trying to hide the details form you (which is good in some scenarios), but you still must understand at least the concepts and limitations of the underlying technology. Web Services are about passing XML messages. The fact that in your C# code the XML message is presented as an object is simply an artifact of the tool/framework you're using.
So... in a sense, yes you can send back objects, but realize those objects are just being mapped onto XML. And then the XML is sent back to the client. The XML is all they end up with (and it might get mapped into some programming language specific construct on their side, but that's their business). So, objects in WebServices in .NET are just there to map the state of the object onto XML and the XML onto the state of an object. There are many things you can build with objects that don't map correctly onto XML as used by Web Services.
So, yes, your toolkit (VS.NET) allows you to hand around object, but I'd suggest making simple objects to hold this data and be aware that that's the only purpose of the object. Here's a short sample (I hope the formatting isn't messed up... sorry if it is):
public class Stock { public int Shares; public string Ticker; }
public class Portfolio { public double TotalValue; public Stock[] Stocks; }
public class StockQuote : System.Web.Services.WebService { [WebMethod] public Portfolio GetPortfolio(string cutomer) { Portfolio p = new Portfolio(); p.TotalValue = 3456; p.Stocks = new Stock[1]; p.Stocks[0] = new Stock(); p.Stocks[0].Shares = 400; p.Stocks[0].Ticker = "WCOM"; return p; }
[ WebMethod ] public double GetQuote(string ticker) { switch (ticker) { case "MSFT": return 67.89; case "IBM": return 43.12; case "DELL": return 12.34; case "WCOM": return 0.000000645; default: return 0; } } }
So this example uses objects, but their purpose in life (as parameters and return values to/from the web service) is just to model the underlying XML. Like I said, this stuff isn't easy but once you understand where the framework relates to the real underlying technology then it becomes easier... HTH
-Brock DevelopMentor http://staff.develop.com/ballen
> Hello Brock, I don't know if I've understanding correctly your answer. > You are telling me that I can return any object from a WebService? [quoted text clipped - 47 lines] >> DevelopMentor >> http://staff.develop.com/ballen Kevin Spencer - 17 Mar 2005 15:32 GMT > is another way that doesn't use session that I don't like. You have the same intrinsic objec ts available from the HttpContext that you have with an ASP.Net app, which includes Application, Sessin, Server, etc. Take your pick.
I'm a bit curious, however, as to why you "don't like" Session. Kind of an odd statment coming from a programmer. Reminds me of a carpenter saying "I'd like to cut this plywood, but I don't like table saws, so I'll use a circular saw instead." Table saws and Session have their place.
 Signature HTH,
Kevin Spencer Microsoft MVP .Net Developer What You Seek Is What You Get.
> Hi. I'm calling two methods of a .NET Webservice (A) from another > Webservice (B). [quoted text clipped - 24 lines] > Thank you. > Alessandro Alessandro Benedetti - 17 Mar 2005 15:53 GMT Hello Kevin,
>> is another way that doesn't use session that I don't like. >> [quoted text clipped - 7 lines] > I'll use a circular saw instead." Table saws and Session have their > place. Well, session variables are perfect for many application but they use a lot of memory and have a session timeout. The webservice that I've to consume have to do long operation (a lot of minutes). Since I don't know how much time the webservice work I can't use session variables because I can't set the correct timeout. Moreover to use sessions I have to implement System.Net.CookieContainer, but if I want to call the Webservice from another language like Java or ASP or PHP I don't know to do it. So I think that I can use Table saws and I have to find my circular saw ;-)
Thank you.
Alessandro
PS: Sorry for my bad english, I'm from Italy.
> Kevin Spencer > Microsoft MVP [quoted text clipped - 29 lines] >> Thank you. >> Alessandro Kevin Spencer - 17 Mar 2005 16:16 GMT Hi Alessandro,
Let me tell you about a Web Service client app that we are building that use a SWF for the front end, and a Web Service for the back end. It keeps track of who is logged on using Session. Now, I don't know where you got the idea that "they use a lot of memory" but that is not true. Does a variable use a lot of memory? The answer is, it uses as much memory as you store in it. Same thing goes for Session. Now, our app uses Session, but it doesn't store a whole lot there. To handle the Timeout issue, we created a WebMethod called "Ping" which simply passes the user id to the server, and keeps their Session alive. The beauty part is, if they close their browser or navigate away, the Session cleans everything up automaticallly when it ends. And it ends 20 minutes after the last Request.
 Signature HTH,
Kevin Spencer Microsoft MVP .Net Developer What You Seek Is What You Get.
> Hello Kevin, > [quoted text clipped - 59 lines] >>> Thank you. >>> Alessandro Alessandro Benedetti - 17 Mar 2005 17:21 GMT Hello Kevin, as Brock Allen replied me, Session variable is not the correct way. I paste his comment:
" Also, I'd beware of using things like Session, as that relies upon an out of band contract of using cookies from the client. The web service specs don't incorporate these like browsers do. If you do use Sessions/cookies, then what's a java client going to do? What about a perl script client? Where in the WSDL does it to say use cookies? If you have to explain to them outside of your WSDL file that cookies are required on their end, then you're no longer really doing web services... at least in the spirt of the web service specs. You've just built your own XML protocol that sorta looks like SOAP.... "
... moreover I have to add that Session protocol relies on text files (cookies) and I think (but it is only my opinion) that session variables occupies more memory because the data have to be transformed in text data (like base64 I think). Not only, as Brock wrote, if you use Session variables you have the data in server memory and in client too (in cookie file). At least if client doesn't accept cookies (don't ask me why some people are scared by cookies ;-) ), he can't use the webservice.
Your solution is perfect for your example, but I have to do with a lot of webservice request (like thousand per hour) and every request can take from a few seconds to more than 3 hours, so I have to generate too many traffic using a ping solution.
Thanks. Alessandro Benedetti
> Hi Alessandro, > [quoted text clipped - 83 lines] >>>> Thank you. >>>> Alessandro Manohar Kamath - 17 Mar 2005 17:48 GMT Allesandro,
Why not simply do a:
[WebMethod(EnableSession=true)] public MyClass WM1() { //Assuming X is an object of type MyClass return X; }
The MyClass object will be serialized and sent across.
 Signature Manohar Kamath Editor, .netWire www.dotnetwire.com
> Hi. I'm calling two methods of a .NET Webservice (A) from another Webservice > (B). [quoted text clipped - 27 lines] > Thank you. > Alessandro Alessandro Benedetti - 17 Mar 2005 18:07 GMT Hello Manohar, yours is the same solution as Brock Allen i think. I didn't know that it is possibile to do that with any type of object. I will try (and I'll read documentation about serialization because I don't know nothing about it). My question is, if the client is a Java application for example, the app have to save the result in a generic object?
Thank you Alessandro Benedetti
> Allesandro, > [quoted text clipped - 50 lines] >> Thank you. >> Alessandro Manohar Kamath - 17 Mar 2005 18:19 GMT As long as these custom classes have fundamental types, you should be able to build a proxy in Java, and talk to a .NET web service. Although I haven't worked on such architectures, I am guessing types like DataSet probably won't be as easy.
 Signature Manohar Kamath Editor, .netWire www.dotnetwire.com
> Hello Manohar, yours is the same solution as Brock Allen i think. I didn't > know that it is possibile to do that with any type of object. I will try [quoted text clipped - 60 lines] > >> Thank you. > >> Alessandro Brock Allen - 17 Mar 2005 18:38 GMT Correct Alessandro. A DataSet (by default) serializes as a <xs:any> to XML schema, which makes it essentially useless to a client programming against the WSDL (which is the contract of the XML data passed back and forth). A strongly typed dataset works better though... Personally, I stay away from those and build my own classes (as in my previous post) specifically for the web service.
-Brock DevelopMentor http://staff.develop.com/ballen
> As long as these custom classes have fundamental types, you should be > able to build a proxy in Java, and talk to a .NET web service. [quoted text clipped - 67 lines] >>>> Thank you. >>>> Alessandro
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|