This tutorial gives an introduction into Web Services and will show how we can create a self-contained Web Service with three simple Web Methods. C# version.
This basic tutorial will serve as an introduction to Web Services. In it, we will see how we can create a simple Web Service with multiple methods.
Similar to ASP.NET web applications, Web Services can be created in Visual Studio.NET in two ways - either in one file, with the header and class code in the same document, or a code-behind file can be utilized. This tutorial will use a code-behind.
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
Web Services can essentially be created in notepad, but Visual Studio makes it so much easier, that it hides unnecessary and repetitive code from the developer. Usually, a Web Service is consumed by a web application or windows form and the Web Methods are called remotely. For demonstration purposes, in this tutorial we will create a self-contained Web Service. This means that when we compile and run it, we will have direct access to the Web Methods.
The first thing we do is start a new Web Site in Visual Studio, and choose ASP.NET Web Service. We are presented with a .ASMX file with the following:
| <%@ WebService Language="C#" CodeBehind="~/App_Code/Service.cs" Class="Service" %> |
Visual Studio will also create the web.config for us, and will look something like this:
| <?xml version="1.0"?> <!-- Note: As an alternative to hand editing this file you can use the -->web admin tool to configure settings for your application. Use the Website->Asp.Net Configuration option in Visual Studio. A full list of settings and comments can be found in machine.config.comments usually located in \Windows\Microsoft.Net\Framework\v2.x\Config <configuration> <configSections> </configuration> <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> </configSections><sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> </sectionGroup><section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> </sectionGroup><sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="Everywhere"/> </sectionGroup><section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication"/> <appSettings/> <connectionStrings/> <system.web> <!-- </system.web>Set compilation debug="true" to insert debugging -->symbols into the compiled page. Because this affects performance, set this value to true only during development. <compilation debug="true"> <assemblies> </compilation><add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> </assemblies><add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add assembly="System.Data.DataSetExtensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <add assembly="System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> <!-- The <authentication> section enables configuration -->of the security authentication mode used by ASP.NET to identify an incoming user. <authentication mode="Windows"/> <!-- The <customErrors> section enables configuration of what to do if/when an unhandled error occurs during the execution of a request. Specifically, it enables developers to configure html error pages to be displayed in place of a error stack trace. <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"> <error statusCode="403" redirect="NoAccess.htm" /> </customErrors><error statusCode="404" redirect="FileNotFound.htm" /> --> <pages> <controls> </pages><add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </controls><add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <httpHandlers> <remove verb="*" path="*.asmx"/> </httpHandlers><add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> <httpModules> <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> </httpModules><system.codedom> <compilers> </system.codedom><compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> </compilers><providerOption name="CompilerVersion" value="v3.5"/> </compiler><providerOption name="WarnAsError" value="false"/> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" warningLevel="4" type="Microsoft.VisualBasic.VBCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <providerOption name="CompilerVersion" value="v3.5"/> </compiler><providerOption name="OptionInfer" value="true"/> <providerOption name="WarnAsError" value="false"/> <!-- The system.webServer section is required for running ASP.NET AJAX under Internet -->Information Services 7.0. It is not necessary for previous version of IIS. <system.webServer> <validation validateIntegratedModeConfiguration="false"/> </system.webServer><modules> <remove name="ScriptModule"/> </modules><add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <handlers> <remove name="WebServiceHandlerFactory-Integrated"/> </handlers><remove name="ScriptHandlerFactory"/> <remove name="ScriptHandlerFactoryAppServices"/> <remove name="ScriptResource"/> <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> </runtime><dependentAssembly> </assemblyBinding><assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/> </dependentAssembly><bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> <dependentAssembly> <assemblyIdentity name="System.Web.Extensions.Design" publicKeyToken="31bf3856ad364e35"/> </dependentAssembly><bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/> |
We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.
To create a self-contained Web Service, all we need to do is write the methods in the code-behind. When we compile and run it, Visual Studio will provide its default Web Service page, displaying the methods. From this page, we can test the web methods by clicking on each one. If the web method requires paraments, these will be in the form of textboxes, which we will be able to fill in and submit.
The three web methods we are going to write will be very simple: One will be the Hello World, with a parameter for a name. This will display "Helo World, #name", where #name is replaced by the user-submitted parameter. The second web method will return the result of two numbers being added together. The two numbers will be supplied by the user via two textboxes. The third will simply return the server time.
The web methods will look something like this:
| using System; using System.Linq; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Xml.Linq; [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 class Service : System.Web.Services.WebService { public Service () } { //Uncomment the following line if using designed components }//InitializeComponent(); [WebMethod] public string HelloWorld(string theName) { return "Hello World, " + theName; }[WebMethod] public decimal AddTwoNumbers(decimal theNumber1, decimal theNumber2) { return theNumber1 + theNumber2; }[WebMethod] public DateTime GetTimeOnServer() { return DateTime.Now; } |
No comments:
Post a Comment