收获WebService的伏乞音讯

2个已经写好的花色中有八个WebService,由于事先未曾记录请求信息的,有时候要求查错等急需找到当次的呼吁新闻,所以须要进入记录请求音讯的功效。

澳门1495娱乐,多个已经写好的连串中有五个Web瑟维斯,由于以前未曾记录请求消息的,有时候须要查错等急需找到当次的呼吁新闻,所以必要参加记录请求消息的效益。

率先想到的是在每一个带有WebMethod天性的点子里调用记录请求音信的点子,那样能够记录消息,可是太多带WebMethod个性的法门了,于是想在全局中截留并抓获,于是想到了Global.asax

首先想到的是在每三个包罗WebMethod天性的办法里调用记录请求消息的点子,那样能够记录新闻,但是太多带WebMethod本性的主意了,于是想在全局中阻止并抓获,于是想到了Global.asax

 

澳门1495娱乐 1澳门1495娱乐 2

澳门1495娱乐 3澳门1495娱乐 4

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.Position = 0;                            using (StreamReader reader = new StreamReader                            {                                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)        {        }    }
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澳门1495娱乐 5澳门1495娱乐 6

View Code

[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将呼吁新闻记录起来

澳门1495娱乐 7澳门1495娱乐 8

澳门1495娱乐 9

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

澳门1495娱乐 10

View
Code

澳门1495娱乐 11

此地运用了Log4Net将请求消息记录起来

澳门1495娱乐 12

澳门1495娱乐 13

另一种调用格局是在另1个门类中添加了WerService的引用,

澳门1495娱乐 14

澳门1495娱乐 15澳门1495娱乐 16

 澳门1495娱乐 17

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

澳门1495娱乐 18

澳门1495娱乐 19

另一种调用方式是在另1个体系中添加了WerService的引用,

澳门1495娱乐 20

澳门1495娱乐 21澳门1495娱乐 22

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

 

澳门1495娱乐 23

 

澳门1495娱乐 24

 

相关文章