Use Web Service Thought Message --- SoapClient
Posted on 2006-06-28 12:34 idior 阅读(3704) 评论(3) 编辑 收藏 举报在本次实践中,我们将直接使用SOAP 消息来调用Web Service并从返回的SOAP 消息中获得结果。通过这种更底层的操作,你将了解到Web Service面向消息的本质,并加深对WS-Addressing规范的理解。
在本例中, Web Service仍旧和上次实践中的一样,是一个和数学运算有关的Asmx类型的Web service。变化的只是Client端,通常情况下,我们都是通过添加Web引用生成Proxy类的方法来访问Web Service, 这里我们将直接采用发送并接受SOAP消息的方法。
在WSE中提供了SoapClient这么一个抽象类,通过继承它,我们可以方便的向Service发送一个SOAP消息,从而触发Service端执行相应的操作(方法),并将结果返回。
下面就是为了使用MathService, 从SoapClient派生出来的MathClient。
2 {
3 public MathClient(Uri destination):base(destination){}
4
5 [SoapMethod("http://idior.cnblogs.com/mathservice/Add")]
6 public double Add(double a,double b)
7 {
8 SoapEnvelope sendEnvelope = CreateMessage("Add",a,b);
9 // sendEnvelope.Context.Addressing.Action = "http://idior.cnblogs.com/mathservice/Add";
10
11 SoapEnvelope receiveEnvelope = base.SendRequestResponse("Add", sendEnvelope);
12 double result = XmlConvert.ToDouble(receiveEnvelope.InnerText);
13 return result;
14 }
15
16 private SoapEnvelope CreateMessage(string op,double a,double b)
17 {
18 // create SOAP message
19 SoapEnvelope envelope = new SoapEnvelope();
20 envelope.CreateBody(); // must create body before setting it
21 envelope.Body.InnerXml =string.Format(
22 @"<{0} xmlns='http://idior.cnblogs.com/mathservice/'>
23 <a>{1}</a>
24 <b>{2}</b>
25 </{0}>",op,a,b);
26 return envelope;
27 }
28 }
2 {
3 static void Main()
4 {
5 MathClient mathClient = new MathClient(new Uri("http://localhost:1274/MathService/MathService.asmx"));
6 double result = mathClient.Add(12,10);
7 Console.WriteLine(result);
8 }
9 }
如我在 WS-Addressing Message Addressing Properties 一文中所说,Soap消息中Message Addressing Properties的To以及Action属性决定了消息如何寻址到Service中的某个特定的方法。
在Line3中通过设定SoapClient的destination,间接的设定了利用该SoapClient发送的消息的To的值,而在Line5中我们为Add方法声明了它的Action属性的值,并且在Line11中SendRequestResponse方法的第一个参数中指定了使用Add方法,通过它和Line6中的声明,该消息的Action属性被确定下来。当然你也可以使用Line9的方法显式的设定消息的Action属性。
以下是最终产生的Request Message和Response Message,可以发现它们和使用Proxy访问Web Service基本一致。
Request Message:
<?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"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsse="http://.../oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://.../oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsa:Action>http://idior.cnblogs.com/mathservice/Add</wsa:Action>
<wsa:MessageID>urn:uuid:84081759-3558-4718-b3ba-b08217a9d50f</wsa:MessageID>
<wsa:ReplyTo>
<wsa:Address>http://.../addressing/role/anonymous</wsa:Address>
</wsa:ReplyTo>
<wsa:To>http://localhost:1274/MathService/MathService.asmx</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-ba231e59-2ac9-44f0-ba86-441dea7aebb6">
<wsu:Created>2006-06-27T04:06:44Z</wsu:Created>
<wsu:Expires>2006-06-27T04:11:44Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<Add xmlns="http://idior.cnblogs.com/mathservice/">
<a>12</a>
<b>10</b>
</Add>
</soap:Body>
</soap:Envelope>
Response Message:
<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"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsse="http://.../oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://.../oasis-200401-wss-wssecurity-utility-1.0.xsd">
<soap:Header>
<wsa:Action>http://idior.cnblogs.com/mathservice/AddResponse</wsa:Action>
<wsa:MessageID>urn:uuid:020a58d0-cdbf-437c-8cd1-dbbbbc6443f7</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:84081759-3558-4718-b3ba-b08217a9d50f</wsa:RelatesTo>
<wsa:To>http://.../addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-a95201b2-d3d1-40a9-9cc4-8632b10b438e">
<wsu:Created>2006-06-27T04:06:47Z</wsu:Created>
<wsu:Expires>2006-06-27T04:11:47Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<AddResponse xmlns="http://idior.cnblogs.com/mathservice/">
<AddResult>22</SubResult>
</AddResponse>
</soap:Body>
</soap:Envelope>
参考资料:
WS-Addressing Message Addressing Properties
备注:
你可以通过下面的属性Line9,在代码中显式(explicit)设定Service中某个方法的SoapAction,并影响到最终的WSDL.不过由于该属性不属于WSE,所以你只能修改Input Ation.而无法修改Output Action, 而在WSE中有一个SoapMethod的属性可以同时设定两者。
2 Name = "MathService",
3 Namespace = "http://idior.cnblogs.com/mathservice/",
4 ConformsTo = WsiProfiles.BasicProfile1_1,
5 EmitConformanceClaims = true)]
6 public interface IMathService
7 {
8 [WebMethod()]
9 [SoapDocumentMethod("http://idior.cnblogs.com/mathservice/Add")]
10 double Add(double a, double b);
11
12 [WebMethod()]
13 double Sub(double a, double b);
14 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一文彻底搞懂 MCP:AI 大模型的标准化工具箱
· 电商平台中订单未支付过期如何实现自动关单?
· 用 .NET NativeAOT 构建完全 distroless 的静态链接应用
· 为什么构造函数需要尽可能的简单
· 探秘 MySQL 索引底层原理,解锁数据库优化的关键密码(下)
· 短信接口被刷爆:我用Nginx临时止血
· 聊聊智商税:AI知识库
· .NET 平台上的开源模型训练与推理进展
· Google发布A2A开源协议:“MCP+A2A”成未来标配?
· C# 多项目打包时如何将项目引用转为包依赖