web service

using  System;
 2 using  System.Web;
 3 using  System.Web.Services;
 4 using  System.Web.Services.Protocols;
 5
 6 [WebService(Namespace  =   " http://tempuri.org/ " )]
 7 [WebServiceBinding(ConformsTo  =  WsiProfiles.BasicProfile1_1)]
 8 public   class  Service : System.Web.Services.WebService
 9 {
10    public Service () {
12        //Uncomment the following line if using designed components 
13        //InitializeComponent(); 
14    }

15
16    [WebMethod]
17    public string HelloWorld() {
18        return "Hello World";
19    }

20
21    [WebMethod]
22    public int GetInt()
23    {
24        return 1;
25    }

26}

这个类应该是运行在另外一台机器上的,一般用做数据库的操作等方面。
在客户端,我们可以用WSDL工具或微软的IDE给一个Project添加Web引用,不给出具体的操作。
添加好之后,我们看看生成的本地代理类。
  1 // ------------------------------------------------------------------------------
  2 //  <auto-generated>
  3 //      This code was generated by a tool.
  4 //      Runtime Version:2.0.50727.42
  5 //
  6 //      Changes to this file may cause incorrect behavior and will be lost if
  7 //      the code is regenerated.
  8 //  </auto-generated>
  9 // ------------------------------------------------------------------------------
 10
 11 //  
 12 //  This source code was auto-generated by Microsoft.VSDesigner, Version 2.0.50727.42.
 13 //  
 14 #pragma warning disable  1591
 15
 16 namespace  CourtMediaUI.TestService  {
 17    using System.Diagnostics;
 18    using System.Web.Services;
 19    using System.ComponentModel;
 20    using System.Web.Services.Protocols;
 21    using System;
 22    using System.Xml.Serialization;
 23    
 24    
 25    /// <remarks/>
 26    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]
 27    [System.Diagnostics.DebuggerStepThroughAttribute()]
 28    [System.ComponentModel.DesignerCategoryAttribute("code")]
 29    [System.Web.Services.WebServiceBindingAttribute(Name="ServiceSoap", Namespace="http://tempuri.org/")]
 30    public partial class Service : System.Web.Services.Protocols.SoapHttpClientProtocol {
 31        
 32        private System.Threading.SendOrPostCallback HelloWorldOperationCompleted;
 33        
 34        private System.Threading.SendOrPostCallback GetIntOperationCompleted;
 35        
 36        private bool useDefaultCredentialsSetExplicitly;
 37        
 38        /// <remarks/>
 39        public Service() {
 40            this.Url = global::CourtMediaUI.Properties.Settings.Default.CourtMediaUI_TestService_Service;
 41            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
 42                this.UseDefaultCredentials = true;
 43                this.useDefaultCredentialsSetExplicitly = false;
 44            }

 45            else {
 46                this.useDefaultCredentialsSetExplicitly = true;
 47            }

 48        }

 49        
 50        public new string Url {
 51            get {
 52                return base.Url;
 53            }

 54            set {
 55                if ((((this.IsLocalFileSystemWebService(base.Url) == true
 56                            && (this.useDefaultCredentialsSetExplicitly == false)) 
 57                            && (this.IsLocalFileSystemWebService(value) == false))) {
 58                    base.UseDefaultCredentials = false;
 59                }

 60                base.Url = value;
 61            }

 62        }

 63        
 64        public new bool UseDefaultCredentials {
 65            get {
 66                return base.UseDefaultCredentials;
 67            }

 68            set {
 69                base.UseDefaultCredentials = value;
 70                this.useDefaultCredentialsSetExplicitly = true;
 71            }

 72        }

 73        
 74        /// <remarks/>
 75        public event HelloWorldCompletedEventHandler HelloWorldCompleted;
 76        
 77        /// <remarks/>
 78        public event GetIntCompletedEventHandler GetIntCompleted;
 79        
 80        /// <remarks/>
 81        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
 82        public string HelloWorld() {
 83            object[] results = this.Invoke("HelloWorld"new object[0]);
 84            return ((string)(results[0]));
 85        }

 86        
 87        /// <remarks/>
 88        public void HelloWorldAsync() {
 89            this.HelloWorldAsync(null);
 90        }

 91        
 92        /// <remarks/>
 93        public void HelloWorldAsync(object userState) {
 94            if ((this.HelloWorldOperationCompleted == null)) {
 95                this.HelloWorldOperationCompleted = new System.Threading.SendOrPostCallback(this.OnHelloWorldOperationCompleted);
 96            }

 97            this.InvokeAsync("HelloWorld"new object[0], this.HelloWorldOperationCompleted, userState);
 98        }

 99        
100        private void OnHelloWorldOperationCompleted(object arg) {
101            if ((this.HelloWorldCompleted != null)) {
102                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
103                this.HelloWorldCompleted(thisnew HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
104            }

105        }

106        
107        /// <remarks/>
108        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetInt", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
109        public int GetInt() {
110            object[] results = this.Invoke("GetInt"new object[0]);
111            return ((int)(results[0]));
112        }

113        
114        /// <remarks/>
115        public void GetIntAsync() {
116            this.GetIntAsync(null);
117        }

118        
119        /// <remarks/>
120        public void GetIntAsync(object userState) {
121            if ((this.GetIntOperationCompleted == null)) {
122                this.GetIntOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetIntOperationCompleted);
123            }

124            this.InvokeAsync("GetInt"new object[0], this.GetIntOperationCompleted, userState);
125        }

126        
127        private void OnGetIntOperationCompleted(object arg) {
128            if ((this.GetIntCompleted != null)) {
129                System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
130                this.GetIntCompleted(thisnew GetIntCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
131            }

132        }

133        
134        /// <remarks/>
135        public new void CancelAsync(object userState) {
136            base.CancelAsync(userState);
137        }

138        
139        private bool IsLocalFileSystemWebService(string url) {
140            if (((url == null
141                        || (url == string.Empty))) {
142                return false;
143            }

144            System.Uri wsUri = new System.Uri(url);
145            if (((wsUri.Port >= 1024
146                        && (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {
147                return true;
148            }

149            return false;
150        }

151    }

152    
153    /// <remarks/>
154    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]
155    public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e);
156    
157    /// <remarks/>
158    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]
159    [System.Diagnostics.DebuggerStepThroughAttribute()]
160    [System.ComponentModel.DesignerCategoryAttribute("code")]
161    public partial class HelloWorldCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
162        
163        private object[] results;
164        
165        internal HelloWorldCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
166                base(exception, cancelled, userState) {
167            this.results = results;
168        }

169        
170        /// <remarks/>
171        public string Result {
172            get {
173                this.RaiseExceptionIfNecessary();
174                return ((string)(this.results[0]));
175            }

176        }

177    }

178    
179    /// <remarks/>
180    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]
181    public delegate void GetIntCompletedEventHandler(object sender, GetIntCompletedEventArgs e);
182    
183    /// <remarks/>
184    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services""2.0.50727.42")]
185    [System.Diagnostics.DebuggerStepThroughAttribute()]
186    [System.ComponentModel.DesignerCategoryAttribute("code")]
187    public partial class GetIntCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
188        
189        private object[] results;
190        
191        internal GetIntCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) : 
192                base(exception, cancelled, userState) {
193            this.results = results;
194        }

195        
196        /// <remarks/>
197        public int Result {
198            get {
199                this.RaiseExceptionIfNecessary();
200                return ((int)(this.results[0]));
201            }

202        }

203    }

204}

205
206 #pragma warning restore  1591

你可能感兴趣的:(web Service)