LondonPhantom
LondonPhantom

Reputation: 1897

How do I get the parameter values from in a web service

I have a web service (an ASP.NET .asmx page), and for debugging purposes I need to log all calls to the webservice, including values of all parameters passed into each call. So basically the first thing each WebMethod should do is log it's state with details of all the parameter values passed in to it.

So far so good. The complication is that I also want an automated way of getting the parameter values - there's quite a few webmethods with different signatures, and some of them have up to ~30 parameters, so manually coding against each specific parameter would likely be massively error-prone. I'd rather be able to call a method that looks at the current Http context and automatically uses that to grab and parse whatever has been passed in by the client.

But I hit a snag. When I look at HttpContext.Current.Request, it turns out that both the Form and QueryString collections are empty. So if the arguments passed to the webmethod aren't in either of those collections, where would they be? Anyone know how I can retrieve them?

Upvotes: 2

Views: 1641

Answers (3)

Lloyd
Lloyd

Reputation: 1324

You can use AOP techniques for this task. Considering PostSharp, you can create custom aspect like this:

[Serializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry(MethodExecutionArgs args)
    {
        Trace.WriteLine(string.Format("Entering {0}", args.Method.Name));

        for (int i = 0; i < args.Arguments.Count; i++)
        {
            Trace.WriteLine(string.Format("    {0}", args.Arguments.GetArgument(i)));
        }
    }
}

and then apply it to your web-service methods:

[WebMethod, Trace]
public string HelloWorld()
{
    return "Hello World";
}

Upvotes: 1

Vinod
Vinod

Reputation: 1952

SOAP Extentions is a better choice. Here is another example to retreive SOAP request and SOAP response as XML. All you do is parse the XML to retreive parameter name value pairs.

Upvotes: 0

Bala R
Bala R

Reputation: 108937

You could use SOAP extensions and follow the example in this post to log the request which would have the method name and parameters.

Upvotes: 1

Related Questions