csharp showtime by j2ZmtbA

VIEWS: 8 PAGES: 5

									Consuming C# Web Server from C#.NET Client to show the server time.
1. Create a new project, choose ASP.NET Web Service Application. Here we name the project
name as ServiceTime.




Open the Service1.asmx, add the following codes after “helloworld” method.
       [WebMethod]
       public DateTime GetServerTime()           {
               return DateTime.Now;
       }
Save and run the project. Visual Studio will get you a port and the show the methods you built on
the website. Like this: http://localhost:64518/Service1.asmx




Then add a new project from the current solution.
Choose Windows Forms Application, here we name it ServiceTimeClient.
Then drag the corresponding components and format it like this:




Right click on the project -> Add Service Reference.
Copy and paste the address of Service1.asmx.
In my computer, it is: http://localhost:64518/Service1.asmx
Click Go, Name the Namespace “TimeServiceReference”, Click OK




Add the code:
using ServiceTimeClient.TimeServiceReference;


Then build a new instance of Service1SoapClient:
Service1SoapClient proxy = new Service1SoapClient();


Double click the “Get Time” button on the [Design] side, add the following code:
               label1.Text = proxy.HelloWorld();
               textBox1.Text = proxy.GetServerTime().ToString();


You will get the result like this:
Before click the button:




After clicking the button:

								
To top