160 likes | 282 Vues
This comprehensive guide covers the essential topics for creating, testing, and referencing web services in Visual Studio. Starting with project setup as an ASP.NET Web Service, it details how to add functionality through methods and properties, and how to expose them for web access. Learn to test web services through the built-in testing pages, utilize client projects effectively by adding service references, and manage dependencies in a single solution. This guide ensures you harness the full potential of web services in your development workflow.
E N D
Introduction to Web Services—Topics • Creating a Web Service • Testing Web Services • Referencing a Web Service in a Visual Studio Project • Support for Web Services Solutions in Visual Studio
Creating a Web Service • Create a new VS projectof type “ASP.Net WebService” • You automatically have“Service1.asmx” as thedefault service. • Rename this or delete it and add a new service giving it a meaningful name. • You may have many web service files in a single solution • Break them up by functionality
Adding Code to the Web Service • Add whatever methods, properties, etc., that you need to your web service • Precede subroutines and functions that you want to expose to the web with the line:<WebMethod(Description:="Returns a hard-coded test string")> _ Public Function HelloWorld() As String Return "Hello World“ End Function • Procedures lacking this prefix will be used internally within the service Note Line Continuation Character
Testing Web Services • Web services can be tested without a consumer application • Navigate to the services .asmx page • Select the public method you wish to test
Testing Web Services (cont.) • The CalcTotalfunction requirestwo parameters • Test provides simpletext boxes for input • “Invoke” buttontests the service
Testing Web Services (cont.) • The result screen shows the XML-encapsulated result of the web service method/function <WebMethod(Description:="Calculation example with two numeric inputs.")> _ Public Function CalcTotal(ByVal sglPrice As Single, _ ByVal intQty As Integer) As Single Return sglPrice * intQty * 1.06 End Function
Testing Web Services (cont.) • You can also set the web services solution as the start up project in Visual Studio’s Solution Explorer • Running the solution brings up the test page
Referencing a Web Service in a VB Project • The VB client project (both Windows and ASP.Net) must be ‘aware’ of the capabilities of a web service you want it to use • Available methods and properties • Data types of parameters and return values • This is done with a proxy class that contains this information
Adding Web Reference • Right click project and select“Add Service Reference…”
Adding Web Reference (cont.) • Click Advanced…
Add Web Reference (cont.) • Click "Add Web Reference…"
Adding Web Reference (cont.) • Find your web service in this solution
Add a Web Reference (cont.) • Select the desired web service
Creating a Web Reference (cont.) Click Add Reference Capabilities Name you will reference in code
Special Note • It is very, very important that you complete the web service before you add the reference to it in another web or Windows project • The Web Reference freezes the interface of the web service at the time the reference was added to the project • I have had a great deal of difficulty getting these references to recognize changes to the interface • You can change coding within the web exposed functions and subs and other procedures with no problem
Support for Web Services in Visual Studio • Web services providersand consumers are multiple independentprograms • Visual Studio lets you work with both in onesolution • Debug across programs • See code in both • Etc. • Set Startup Project