如何从Web服务中获取参数值 [英] How do I get the parameter values from in a web service

查看:179
本文介绍了如何从Web服务中获取参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Web服务(一个ASP.NET .asmx页),出于调试目的,我需要记录对Web服务的所有调用,包括传递给每个调用的所有参数的值.因此,基本上每个WebMethod要做的第一件事就是记录传递给它的所有参数值的详细信息的状态.

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.

到目前为止,一切都很好.复杂的是,我还需要一种自动获取参数值的方法-有很多具有不同签名的网络方法,其中一些具有多达30个参数,因此针对每个特定参数进行手动编码可能会容易出错.我希望能够调用一种方法来查看当前的Http上下文,并自动使用该方法来捕获和解析客户端传递的任何内容.

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.

但是我遇到了障碍.当我查看HttpContext.Current.Request时,发现Form和QueryString集合均为空.因此,如果传递给web方法的参数不在这些集合中的任何一个中,它们将在哪里?有人知道我如何找回它们吗?

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?

推荐答案

您可以将AOP技术用于此任务.考虑到 PostSharp ,您可以创建这样的自定义方面:

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)));
        }
    }
}

,然后将其应用于您的Web服务方法:

and then apply it to your web-service methods:

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

这篇关于如何从Web服务中获取参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆