100 likes | 233 Views
Web Services I: Returning Data in XML Format. Service-Oriented Architecture (SOA). Old Model for Applications Executables running on individual computers Limited or no ability to communicate between applications Very dependent on platform, OS and/or programming language SOA
E N D
Service-Oriented Architecture (SOA) • Old Model for Applications • Executables running on individual computers • Limited or no ability to communicate between applications • Very dependent on platform, OS and/or programming language • SOA • Loosely coupled “Services” • Currently – Web Services • Can be called from multiple platforms, OS’s and languages
Web Service • A class with methods that can be accessed (via the Internet): • From different platforms (Hardware and OS) • By different programming languages • Information is returned from the Web Service in an XML format or JSON format
I. Creating a "Hello World" Web Service with VS 2010 • Within a Visual Studio 2010 Website • Website, Add New Item, Web Service • This will create a class with all of the unique web service stuff already in it – including a “Hello World” test method. • The web service by default will have two files (just like our web pages): • .asmx file (WebService.asmx) • This is what you point the users of the service at • It contains a link to the .cs file (below) • .cs class file (WebService.cs) • Lives in the App_Code folder • Browse .asxm file to test the Web Service
"Hello World" part 2 • Here is the code in the WebService.asmx file <%@ WebServiceLanguage="C#"CodeBehind="~/App_Code/WebService.cs"Class="WebService" %> This is the file that is browsed, It is essentially just a front-end to the .cs file behind it.
"Hello World" part 3 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the //following line. // [System.Web.Script.Services.ScriptService] public classWebService : System.Web.Services.WebService { public WebService () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public stringHelloWorld() { return "Hello World"; } } • Here is the code in the WebService.cs file
I. Providing a Web Service cont: Adding Methods • Adding a method to a Web Service is identical to what you’re used to, except: • [WebMethod] attribute must be immediately before the public keyword in the method declaration [WebMethod]public decimal CalculateYearlyBonus(decimal parYearlySales){ … }
WS_MusicCategories [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET //AJAX, uncomment the following line. // [System.Web.Script.Services.ScriptService] public classWebService : System.Web.Services.WebService { publicWS_MusicCategories () { //Uncomment the following line if using designed components //InitializeComponent(); } [WebMethod] public List<TO_MusicCategory>GetCategories() { DA_MusicCategoriescategoriesObject = new DA_MusicCategories(); returncategoriesObject.GetCategories()"; } }
WS_MusicCategories cont. • From the preceding slide, note the following: • The [ScriptService] line is uncommented • All the GetCategories web service method does is: • Create an instance of your DA_MusicCategories class • Calls the DA class method GetCategories • Returns the result of that call • The Web Service is just a wrapper around your original DA class that allows it to be used as a web service.