Requirement: as a service provider, You need to monitor each client that calls WebService. The information to be monitored is roughly as follows: the client's IP address and the method of the class called by the client.
So I spent some time exploring the WebService machine of Asp.net.
Solution:
Compile a base class for all WebService interfaces in the interface project. In the construction method of this base class, obtain the desired information by analyzing httpcontext. Current. Request.
1. The IP address can be obtained through httpcontext. Current. Request. userhostaddress.
2. You can analyze httpcontext. Current. Request. inputstream to obtain the method and parameter that you call.
This is what I used belowCodeOutput inputstream content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Soap: envelope xmlns: Soap = "http://schemas.xmlsoap.org/soap/envelope"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: XSD = "http://www.w3.org/2001/XMLSchema">
<Soap: Body>
<Mytestmethod xmlns = "http://tempuri.org/">
<MSG> ABCD </MSG>
</Mytestmethod>
</Soap: Body>
</Soap: envelope>
See it? The called method is mytestmethod, the parameter is MSG, and the value is ABCD.
The test process is as follows:
Let's draw a conclusion:
Asp.netProgramWhen a reference to WebService is added, the client generates a proxy class.
The client call code is generally similar to the following:
WS. service1 S = new wsweb. ws. service1 ();
S. helloworld ();
S. mytestmethod ("sssssssssssssssttttt ");
1. When ws. service1 S = new wsweb. ws. service1 (); is run, the server-side constructor is not called, but the locally generated proxy class constructor is called.
2. Only when this line is run: S. helloworld (); will the method and parameter form soap, bind to HTTP, and send to the server side. In this case, call the server-side constructor and then call helloworld on the server.
This is also the case when you run row 3rd S. mytestmethod ("sssssssssssssttttt"). The server-side constructor is called first and then the server-side mytestmethod is called.
Test code:
Code
// Client call code
Write ( " Client build object starts " );
WS. service1 s = New Wsweb. ws. service1 ();
Write ( " Client end object built " );
Write ("The client starts to call helloworld.");
S. helloworld ();
Write ("The client has finished calling helloworld.");
Write ("The client starts to call mytestmethod.");
S. mytestmethod ("Sssssssssssssttttt");
Write ("The client has called mytestmethod.");
Code
Write ( " Access the server-side base class Constructor " );
system. io. fileinfo fi = New system. io. fileinfo (PATH);
If ( ! fi. exists)
fi. Create ();
httprequest req = httpcontext. current. request;
system. io. stream stream = req. inputstream;
stream. position = 0 ;
system. io. streamreader Sr = New system. io. streamreader (Stream);
Stringbuilder sbrequest = New Stringbuilder ();
String line;
// Read and display lines from the file until the end
// The file is reached.
While (Line = Sr. Readline ()) ! = Null )
{
Sbrequest. append (line + " \ N " );
}
Write (sbrequest. tostring ());
Write ("Construction Method for removing the server end base class");