The file Service1.asmx.cs contains some code in C#. All we have to do is to add the code of each method of our Web Service to the Service1.asmx.cs file. So we could add the method:
0001: [WebMethod] 0002: public double ToFahrenheit(double pCentigrade) 0003: { 0004: return 32 + pCentigrade*9/5; 0005: }As shown above, we have to supply a WebMethod attribute for each method of the class that we wish to be accessible through the Web Service.
Such methods are declared in a class that is derived from the class WebService of the System.Web.Services namespace. It is also usual to include a WebService attribute:
0006: [WebService] 0007: public class Service1 : System.Web.Services.WebService 0008: { 0009: ... 0010: }
Note: if this class provides other methods that do not have a WebMethod attribute, they will not be (directly) accessible by an external site.
Here is an example of the code that is generated by Visual Studio.NET's wizard. I only typed in lines 24-25 and lines 61-65:
0011: using System; 0012: using System.Collections; 0013: using System.ComponentModel; 0014: using System.Data; 0015: using System.Diagnostics; 0016: using System.Web; 0017: using System.Web.Services; 0018: 0019: namespace ServerConvert 0020: { 0021: /// <summary> 0022: /// Summary description for Service1. 0023: /// </summary> 0024: [WebService(Namespace="http://www.a.com/webservices/", 0025: Description="This Web Service provides temperature conversion services.")] 0026: public class Service1 : System.Web.Services.WebService 0027: { 0028: public Service1() 0029: { 0030: //CODEGEN: This call is required by the ASP.NET Web Services Designer 0031: InitializeComponent(); 0032: } 0033: 0034: #region Component Designer generated code 0035: 0036: //Required by the Web Services Designer 0037: private IContainer components = null; 0038: 0039: /// <summary> 0040: /// Required method for Designer support - do not modify 0041: /// the contents of this method with the code editor. 0042: /// </summary> 0043: private void InitializeComponent() 0044: { 0045: } 0046: 0047: /// <summary> 0048: /// Clean up any resources being used. 0049: /// </summary> 0050: protected override void Dispose( bool disposing ) 0051: { 0052: if(disposing && components != null) 0053: { 0054: components.Dispose(); 0055: } 0056: base.Dispose(disposing); 0057: } 0058: 0059: #endregion 0060: 0061: [WebMethod(Description="This converts from Centigrade to Fahrenheit.")] 0062: public double ToFahrenheit(double pCentigrade) 0063: { 0064: return 32 + pCentigrade*9/5; 0065: } 0066: } 0067: }
The above code gives more complicated forms of the WebMethod and WebService attributes. For example, they provide information about the services that are being offered.