Last post Oct 14, 2010 03:12 PM by DeanoReno
Dec 12, 2007 03:49 PM|karan@dotnet|LINK
Just wanted to ask if we can access the request object in a webservice.
In my normal aspx page i would use
but if I use the same in my web service web method even the intellisense wont show request object?
anything i am missing?
Dec 12, 2007 03:52 PM|joteke|LINK
web service can access raw Request via HttpContext.Current.Request.xxx but keep in mind that basically a web service (has a web method which) is abstraction over the HTTP layer, when basically what the method needs is in the parameters the web
method expects (if it's about parameters here). It should have no need to access raw Request directly in normal cases (you might have an exception that here, of course)
Dec 12, 2007 04:01 PM|karan@dotnet|LINK
But what if I store something in my request object or say in my querystring.
And I want to access that particular field in my web method of my web service how would I access that.
and I want to get the name field,how would I proceed doing that.
Dec 12, 2007 04:31 PM|joteke|LINK
As I said, normally these would be defined as params for the web method (calling a web service when it's called with HTTP GET would map given qs params to method parameters with same name). Accessing Request works fine, it's just weird-looking if web method
expects parameters which aren't defined in the web method's own parameters (e.g in the public interface).
Say you have a web service (WebService1.asmx) with a web method as follows:
public int CalculateInts(int i1,int i2)
return i1 + i2;
Now you could call it via HTTP GET like this:
Now, you could change the web method to look for i3
public int CalculateInts(int i1,int i2)
int i3 = Int32.Parse(HttpContext.Current.Request.QueryString["i3"]);
return i1 + i2 + i3;
and call it via HTTP GET like:
(would return 10 of course)
Problem is that if you a) look for web service's documentation page, it states nothing about the i3 parameter. b) it makes the web method aware of the HTTP , and in this case would force to use QueryString to pass data (of course using Request.Params allows
GET, POST etc). But still, it wouldn't be callable via SOAP since SOAP contains params etc wrapped in the SOAP envelope, e.g it's XML, So you'd lose using SOAP protocol which web service inftrastucture is also capable to map to the web method when it follows
the format specified by the WSDL.
c) doesn't it seem weird that i1 and i2 are accessed via the method arguments but i3 totally otherwise?
I think it's more natural to think the web method as a standard method in an object, not something which is called via HTTP. THis way the logic also stays reusable and stays out of the implementation (wire-level) details.
Dec 12, 2007 04:45 PM|karan@dotnet|LINK
yes thanks for that information.
Actually my scenario is different ,actually I should have specified it more correctly.
I have a web page that calls webservice .
Page : http://localhost/List.aspx?&id=7&name=abc
Now that webservice has a web method where I need to get the id and name param from my web page.
So I have to get Id and Name from my query string.
Is this possible or I am going the wrong way.
Its just that the way my application is configured , my web method expects a param which I want to get from the query string.
And using Httpcontext.current.request.querystring returns me null.
Dec 12, 2007 04:54 PM|joteke|LINK
Ah, the web page should pass the params to the web service. Yes, they are separate requests (request to web page and the web service) when HttpContext wouldn't even be the same.
Basically web page can make programmatically a request to the web service (in code) for example with HTTP GET (in code using WebRequest object - in System.Net namespace) OR you'd have a reference to the web service (web reference in the project) and call
it so that web app acts as the client. This is if they aren't part of the same application and you must do the request either manually or with the ws infrastructure
However if they are in the same application (or project in VS), web service is actually just like a local class to the web page and the page could just instantiate the class and call the method.
Dec 13, 2007 09:59 AM|karan@dotnet|LINK
Yes they are in the same application.
But say my fields that I want to access are in the querystring of my webpage
and at this point my web service is called.
Now how would I get the id and name value from my aspx querystring into my web service.
Dec 16, 2007 09:28 PM|Xun Ye - MSFT|LINK
Based on my understanding, there is a web form page and a web service in your project, the user passes some
query string values to the page, and then you want to pass the values from the page to the web service.
Please feel free to correct me if I have misunderstood your concern.
In my opinion, you can get the query string in the web form page easily, and then you can create a web
service instance in the page, invoking the method of the instance, passing the query strings by the
parameters of the method.
If the Web service class name is CWebServiceDemo, there is a web method in the service like:
Public int Method_demo(string id, string name);
You can invoke the method in the web form page code-behind like:
CWebServiceDemo obj_demo = new CWebServiceDemo();
obj_demo. Method_demo(“id”, “name”);
Oct 14, 2010 03:12 PM|DeanoReno|LINK
I have the same problem and I don't think this guy answered it at all.