得到WebService的呼吁音信

一个曾经写好的门类中有八个WebService,由于事先从没记录乞求新闻的,一时候供给查错等急需找到当次的乞求音信,所以须求步入记录央浼新闻的功力。

首先想到的是在每几个分包WebMethod个性的议程里调用记录诉求消息的措施,那样可以记下新闻,不过太多带WebMethod本性的艺术了,于是想在全局中截留并抓获,于是想到了Global.asax

 

图片 1图片 2

public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {

        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            if (Request != null)
            {
                try
                {
                    if (".asmx".Equals(Request.CurrentExecutionFilePathExtension,StringComparison.OrdinalIgnoreCase) && Request.ContentLength > 0)
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            Request.InputStream.CopyTo(ms);
                            ms.Position = 0;
                            using (StreamReader reader = new StreamReader(ms))
                            {
                                LogHelper.Info(reader.ReadToEnd());
                            }
                        }

                    }

                }
                catch (Exception)
                {
                }
                finally
                {
                    Request.InputStream.Position = 0;
                }
            }
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }

View Code

 

 

图片 3图片 4

[WebMethod]
public string HelloWorld()
{
    return "Hello World";
}
[WebMethod]
public string QueryBalance(string username,string password)
{
    if (username == "test" && password == "abcd")
    {
        return "1000000";
    }
    else
    {
        return "用户名或密码错误";
    }
}

View
Code

这里运用了Log4Net将呼吁消息记录起来

图片 5

图片 6

 图片 7

 

图片 8

另一种调用情势是在另多少个体系中增加了Wer瑟维斯的援用,

图片 9图片 10

public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TestWebServiceSoapClient client = new TestWebServiceSoapClient();
            Response.Write(client.QueryBalance("test","abcd"));
        }
    }

View
Code

 

图片 11

 

图片 12

 

相关文章