⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_27772.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:27772 发表者:Toney520 发表日期:2003-01-15 09:53:15
<br>主题:Web Service 的调用
<br>内容:有谁知道在C#中如何动态调用WebSevice?<BR><BR>请指教! <BR>&nbsp;&nbsp; 谢谢!
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:雷神 回复日期:2003-01-15 10:26:11
<br>内容:Web Service是靠SOAP协议进行数据传输的。而SOAP是基于XML技术之上的。SOAP协议是连接客户和服务器的桥梁。而SOAP协议本身没有异步功能,需要在客户端实现异步调用。我们以一个简单的Web Service的例子来说明这一点。<BR><BR>一、MathService.asmx<BR><BR>&lt;%@ WebService Language="C#" Class="MathService" %&gt;<BR><BR><BR>using System;<BR><BR>using System.Web.Services;<BR><BR><BR>[WebService]<BR><BR>public class MathService : WebService {<BR><BR><BR>[WebMethod]<BR><BR>public float Add(float a, float b)<BR><BR>{<BR><BR>return a + b;<BR><BR>}<BR><BR><BR>[WebMethod]<BR><BR>public double Subtract(double a, double b)<BR><BR>{<BR><BR>return a - b;<BR><BR>}<BR><BR><BR>[WebMethod]<BR><BR>public float Multiply(float a, float b)<BR><BR>{<BR><BR>return a * b;<BR><BR>}<BR><BR><BR>[WebMethod]<BR><BR>public float Divide(float a, float b)<BR><BR>{<BR><BR>if (b==0) return -1;<BR><BR>return a / b;<BR><BR>}<BR><BR>}<BR><BR>这是个实现了加,减,乘,除的Web Service,任何客户端程序都可以调用它。下面我们用wsdl(微软公司提供)工具产生一个MathService.asmx 的客户代理程序:wsdl /n:MyMath http://localhost/mathservice.asmx (假设MathService.asmx放在IIS服务器的根目录) ,产生一个MathService.cs代理程序,默认是SOAP协议。<BR><BR>二、MathService.cs:<BR><BR><BR>namespace MyMath{<BR><BR>using System.Diagnostics;<BR><BR>using System.Xml.Serialization;<BR><BR>using System;<BR><BR>using System.Web.Services.Protocols;<BR><BR>using System.ComponentModel;<BR><BR>using System.Web.Services;<BR><BR><BR>[System.Diagnostics.DebuggerStepThroughAttribute()]<BR><BR>[System.ComponentModel.DesignerCategoryAttribute("code")]<BR><BR>[System.Web.Services.WebServiceBindingAttribute(Name="MathServiceSoap", Namespace="http://tempuri.org/")]<BR><BR>public class MathService : System.Web.Services.Protocols.SoapHttpClientProtocol {<BR><BR><BR>public MathService() {<BR><BR>this.Url = "http://localhost/mathservice.asmx";<BR><BR>}<BR><BR><BR>[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Add", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]<BR><BR>public System.Single Add(System.Single a, System.Single b) {<BR><BR>object[] results = this.Invoke("Add", new object[] {<BR><BR>a,<BR><BR>b});<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR><BR>public System.IAsyncResult BeginAdd(System.Single a, System.Single b, System.AsyncCallback callback, object asyncState) {<BR><BR>return this.BeginInvoke("Add", new object[] {<BR><BR>a,<BR><BR>b}, callback, asyncState);<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.Single EndAdd(System.IAsyncResult asyncResult) {<BR><BR>object[] results = this.EndInvoke(asyncResult);<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Subtract", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]<BR><BR>public System.Double Subtract(System.Double a, System.Double b) {<BR><BR>object[] results = this.Invoke("Subtract", new object[] {<BR><BR>a,<BR><BR>b});<BR><BR>return ((System.Double)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.IAsyncResult BeginSubtract(System.Double a, System.Double b, System.AsyncCallback callback, object asyncState) {<BR><BR>return this.BeginInvoke("Subtract", new object[] {<BR><BR>a,<BR><BR>b}, callback, asyncState);<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.Double EndSubtract(System.IAsyncResult asyncResult) {<BR><BR>object[] results = this.EndInvoke(asyncResult);<BR><BR>return ((System.Double)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Multiply", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]<BR><BR>public System.Single Multiply(System.Single a, System.Single b) {<BR><BR>object[] results = this.Invoke("Multiply", new object[] {<BR><BR>a,<BR><BR>b});<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.IAsyncResult BeginMultiply(System.Single a, System.Single b, System.AsyncCallback callback, object asyncState) {<BR><BR>return this.BeginInvoke("Multiply", new object[] {<BR><BR>a,<BR><BR>b}, callback, asyncState);<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.Single EndMultiply(System.IAsyncResult asyncResult) {<BR><BR>object[] results = this.EndInvoke(asyncResult);<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Divide", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]<BR><BR>public System.Single Divide(System.Single a, System.Single b) {<BR><BR>object[] results = this.Invoke("Divide", new object[] {<BR><BR>a,<BR><BR>b});<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.IAsyncResult BeginDivide(System.Single a, System.Single b, System.AsyncCallback callback, object asyncState) {<BR><BR>return this.BeginInvoke("Divide", new object[] {<BR><BR>a,<BR><BR>b}, callback, asyncState);<BR><BR>}<BR><BR><BR>/// &lt;remarks/&gt;<BR><BR>public System.Single EndDivide(System.IAsyncResult asyncResult) {<BR><BR>object[] results = this.EndInvoke(asyncResult);<BR><BR>return ((System.Single)(results[0]));<BR><BR>}<BR><BR>}<BR><BR>}<BR><BR>之后我们用csc /t:library MathService.cs编译并产生一个MathService.dll.<BR><BR>现在我们可以写任何的客户程序去调用服务器上的MathService.asmx。<BR><BR>如:WinForm, C#,ASPX等。<BR><BR>下面我们写一个test.cs去测试异步调用:<BR><BR><BR>三、test.cs:<BR><BR>using System;<BR><BR><BR>public class test{<BR><BR>public static void Main(){<BR><BR>MyMath.MathService math = new MyMath.MathService();<BR><BR>IAsyncResult result1 = math.BeginAdd(10,20,null,null);<BR><BR>Object result=math.EndAdd(result1);<BR><BR>Console.WriteLine("result =========="+result);<BR><BR>}<BR><BR>}<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Toney520 回复日期:2003-01-15 12:02:35
<br>内容:谢谢你雷神!还有一点问题。<BR><BR>这样对于Sevice 来说它是在一个固定的IIS服务器上。<BR>我现在的情况是处于开发阶段,Service是在我(我公司)的机器上,比如:192.168.7.35<BR>将来把程序交给用户后IIS服务器就是另一个IP了:比如:192.168.153.60<BR><BR>那么我现所做的客户端程序还能访问Service吗?<BR>如何做到Service可以变到服务器?
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:雷神 回复日期:2003-01-15 15:27:47
<br>内容:你可以用组件的思想来理解Web Service(不好意思,最近满头COM组件)<BR><BR>Web Service是一个能够使用XML消息通过网络来访问的Interface, 这个Interface描述了一组可访问的操作。组件是由SOAP+WSDL包装的Object适应松散耦合的网络环境,可通过Web访问,手段是SOAP Message服务的行为、输入/输出都可使用WSDL描述。<BR><BR>你只需要告诉客户端你的WSDL。就象组件的IDL。
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Toney520 回复日期:2003-01-17 08:41:30
<br>内容:<BR>我如何做才能做到现在开发的Serviece 和 客户端将来不受影响呢?<BR>我不太理解这个,烦请指教!谢谢!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:雷神 回复日期:2003-01-17 08:49:21
<br>内容:interface保持不变呀。<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
<hr size=1>
<blockquote><p>
回复者:Toney520 回复日期:2003-01-17 17:40:55
<br>内容:雷神,你好.我该如何在客户端(远程调用)动态的加载所生成的Service呢?谢谢!
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -