c# + soap 谁用过?帮忙给个思路?

hello001 2004-03-31 05:44:38
c# + soap 谁用过?帮忙给个思路?(关于数据传输)
...全文
92 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
elite2018 2004-04-01
  • 打赏
  • 举报
回复
<%@ WebService Language="C#" Class="data" %>

using System;
using System.Web.Services;

public class Order
{
public int OrderID;
public double Price;
}
public class data {

[WebMethod]
public Order GetOrder()
{
Order myOrder = new Order();

myOrder.Price=34.5;
myOrder.OrderID = 323232;
return myOrder;
}
}
现再用wsdl工具将web serivce的wsdl转换成client的代理程序,如:wsdl /protocol:Soap /namespace:D http://localhost/data1.asmx
(假设data1.asmx是放在iis服务器的根目录下(c:\inetpub\wwwroot)).
产生一个客户端的代理程序data.cs:

namespace D {
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="dataSoap", Namespace="http://tempuri.org/")]
public class data : System.Web.Services.Protocols.SoapHttpClientProtocol {

/// <remarks/>
public data() {
this.Url = "http://localhost/data1.asmx";
}

/// <remarks/>
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetOrder", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
public Order GetOrder() {
object[] results = this.Invoke("GetOrder", new object[0]);
return ((Order)(results[0]));
}

/// <remarks/>
public System.IAsyncResult BeginGetOrder(System.AsyncCallback callback, object asyncState) {
return this.BeginInvoke("GetOrder", new object[0], callback, asyncState);
}

/// <remarks/>
public Order EndGetOrder(System.IAsyncResult asyncResult) {
object[] results = this.EndInvoke(asyncResult);
return ((Order)(results[0]));
}
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public class Order {

/// <remarks/>
public int OrderID;

/// <remarks/>
public System.Double Price;
}
}
然后再将之编译成dll.如:csc /t:library /out:c:\inetpub\wwwroot\bin\data.dll data.cs.完成以后就可以使用.Net的client程序进行调用(如winform ,cs or aspx).
下面是用test.aspx实现client的调用的.
<%@ Import Namespace="D" %>
<html>
<style>
div
{
font: 8pt verdana;
background-color:cccccc;
border-color:black;
border-width:1;
border-style:solid;
padding:10,10,10,10;
}
</style>

<script language="C#" runat="server">

public void Page_Load(Object sender, EventArgs E)
{
data datatype =new data();
Order or=datatype.GetOrder();
Response.Write("order -==="+or.OrderID+"<br>");
}
</script>
</body>
</html>
运行这个程序,将在浏览器打印"order=======323232".

我们来研究client 同server到底传输了什么.
当我们执行test.aspx时,它调用到了data.cs的GetOrder()方法,然后序列化信息,并包装成:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetOrder xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope
ermachao 2004-04-01
  • 打赏
  • 举报
回复
用SOAP通讯在性能上是比用Binary要差的,但会稳定些,这就是WebService和HTTP Remoting的差别。
success_victory 2004-03-31
  • 打赏
  • 举报
回复
这与你使用的传输方式、通道有关。
比如,你使用net remoting进行数据传输,则
如果使用http通道,默认就是soap的
而使用tcp/ip通道,默认就是bin的
当然,你可以自定义发送器、接受器来改变其传输方式。
msdn上有详细的描述
thinkingforever 2004-03-31
  • 打赏
  • 举报
回复
没有用过
lzyang 2004-03-31
  • 打赏
  • 举报
回复
呵呵,也只是知道一点,可以自定义soap 头,soap 消息。

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧