最简单的调用当然是服务引用,但是我更想原生调用,所以希望能通过报文有如下研究

1.报文分析

  1. <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" name="DemoService" targetNamespace="http://tempuri.org/">
  2. <wsdl:types>
  3. <xsd:schema targetNamespace="http://tempuri.org/Imports">
  4. <xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
  5. <xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
  6. <xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo"/>
  7. <xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/DataTypes"/>
  8. <xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/System"/>
  9. </xsd:schema>
  10. </wsdl:types>
  11. <wsdl:message name="IDemoService_DoSomething_InputMessage">
  12. <wsdl:part name="parameters" element="tns:DoSomething"/>
  13. </wsdl:message>
  14. <wsdl:message name="IDemoService_DoSomething_OutputMessage">
  15. <wsdl:part name="parameters" element="tns:DoSomethingResponse"/>
  16. </wsdl:message>
  17. <wsdl:portType name="IDemoService">
  18. <wsdl:operation name="DoSomething">
  19. <wsdl:input wsaw:Action="http://tempuri.org/IDemoService/DoSomething" message="tns:IDemoService_DoSomething_InputMessage"/>
  20. <wsdl:output wsaw:Action="http://tempuri.org/IDemoService/DoSomethingResponse" message="tns:IDemoService_DoSomething_OutputMessage"/>
  21. </wsdl:operation>
  22. </wsdl:portType>
  23. <wsdl:binding name="BasicHttpBinding_IDemoService" type="tns:IDemoService">
  24. <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
  25. <wsdl:operation name="DoSomething">
  26. <soap:operation soapAction="http://tempuri.org/IDemoService/DoSomething" style="document"/>
  27. <wsdl:input>
  28. <soap:body use="literal"/>
  29. </wsdl:input>
  30. <wsdl:output>
  31. <soap:body use="literal"/>
  32. </wsdl:output>
  33. </wsdl:operation>
  34. </wsdl:binding>
  35. <wsdl:service name="DemoService">
  36. <wsdl:port name="BasicHttpBinding_IDemoService" binding="tns:BasicHttpBinding_IDemoService">
  37. <soap:address location="http://dev.xxx.io/Demo/DemoService.svc"/>
  38. </wsdl:port>
  39. </wsdl:service>
  40. </wsdl:definitions>

可以看到

1.wcf服务地址

  1. http://dev.xxx.io/Demo/DemoService.svc
  2.  
  3. 2.引用了4个命名空间
  1. Serialization
  1. Contracts.Models.Demo
  1. DataTypes
  1. System
  2.  
  3. 3.Service的名称及接口
  4.  
  5. DemoService,
    IDemoService
  6.  
  7. 4.服务接口的方法
    DoSomething
  8.  
  9. 再访问
  1. 来读取类型的具体字段
    http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3
  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/DataTypes" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/DataTypes">
  2. <xs:complexType name="Request">
  3. <xs:sequence>
  4. <xs:element minOccurs="" name="UserId" nillable="true" type="xs:string"/>
  5. </xs:sequence>
  6. </xs:complexType>
  7. <xs:element name="Request" nillable="true" type="tns:Request"/>
  8. <xs:complexType name="Response">
  9. <xs:sequence>
  10. <xs:element minOccurs="" name="ErrorCode" type="xs:int"/>
  11. <xs:element minOccurs="" name="Message" nillable="true" type="xs:string"/>
  12. <xs:element minOccurs="" name="Success" type="xs:boolean"/>
  13. </xs:sequence>
  14. </xs:complexType>
  15. <xs:element name="Response" nillable="true" type="tns:Response"/>
  16. </xs:schema>

http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd0

  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
  2. <xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo"/>
  3. <xs:element name="DoSomething">
  4. <xs:complexType>
  5. <xs:sequence>
  6. <xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo" minOccurs="" name="request" nillable="true" type="q1:SomeReq"/>
  7. </xs:sequence>
  8. </xs:complexType>
  9. </xs:element>
  10. <xs:element name="DoSomethingResponse">
  11. <xs:complexType>
  12. <xs:sequence>
  13. <xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo" minOccurs="" name="DoSomethingResult" nillable="true" type="q2:SomeResp"/>
  14. </xs:sequence>
  15. </xs:complexType>
  16. </xs:element>
  17. </xs:schema>

http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd2

  1. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo" elementFormDefault="qualified" targetNamespace="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo">
  2. <xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/System"/>
  3. <xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/DataTypes"/>
  4. <xs:complexType name="SomeReq">
  5. <xs:complexContent mixed="false">
  6. <xs:extension xmlns:q1="http://schemas.datacontract.org/2004/07/DataTypes" base="q1:Request">
  7. <xs:sequence>
  8. <xs:element minOccurs="" name="Date" type="xs:dateTime"/>
  9. <xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/System" minOccurs="" name="DateUTC" type="q2:DateTimeOffset"/>
  10. </xs:sequence>
  11. </xs:extension>
  12. </xs:complexContent>
  13. </xs:complexType>
  14. <xs:element name="SomeReq" nillable="true" type="tns:SomeReq"/>
  15. <xs:complexType name="SomeResp">
  16. <xs:complexContent mixed="false">
  17. <xs:extension xmlns:q3="http://schemas.datacontract.org/2004/07/WHTR.DataTypes" base="q3:Response">
  18. <xs:sequence>
  19. <xs:element minOccurs="" name="Date" type="xs:dateTime"/>
  20. <xs:element minOccurs="" name="DateStr" nillable="true" type="xs:string"/>
  21. <xs:element minOccurs="" name="ResultDate" nillable="true" type="xs:string"/>
  22. <xs:element minOccurs="" name="ResultDateUTC" nillable="true" type="xs:string"/>
  23. </xs:sequence>
  24. </xs:extension>
  25. </xs:complexContent>
  26. </xs:complexType>
  27. <xs:element name="SomeResp" nillable="true" type="tns:SomeResp"/>
  28. </xs:schema>

根据以上信息,可以生成的最简单的 Contract代码如下:

  1. [Serializable]
  2. [DataContract(Name = "Response", Namespace = "http://schemas.datacontract.org/2004/07/DataType")]
  3.  
  4. public class Response
  5. {
  6. [DataMember]
  7. public int ErrorCode { get; set; }
  8. [DataMember]
  9. public string Message { get; set; }
  10. [DataMember]
  11. public bool Success { get; set; }
  12. }
  13.  
  14. /// <summary>
  15. /// SomeResponse
  16. /// </summary>
  17. [DataContract(Name = "SomeResp", Namespace = "http://schemas.datacontract.org/2004/07/DataType")]
  18.  
  19. [Serializable]
  20. public sealed class SomeResp : Response
  21. {
  22. /// <summary>
  23. /// 时间格式的Date
  24. /// </summary>
  25. [DataMember]
  26. public DateTime Date { get; set; }
  27.  
  28. /// <summary>
  29. /// 格式成字符串的时间
  30. /// </summary>
  31. [DataMember]
  32. public string DateStr { get; set; }
  33.  
  34. /// <summary>
  35. /// 返回传入的Date
  36. /// </summary>
  37. [DataMember]
  38. public string ResultDate { get; set; }
  39.  
  40. /// <summary>
  41. /// 返回传入的Date
  42. /// </summary>
  43. [DataMember]
  44. public string ResultDateUTC { get; set; }
  45. }
  46.  
  47. [Serializable]
  48.  
  49. [DataContract(Name = "Request", Namespace = "http://schemas.datacontract.org/2004/07/Contracts.Models.Demo")]
  50. public class Request
  51. {
  52. [DataMember]
  53. public string UserId { get; set; }
  54. }
  55.  
  56. /// <summary>
  57. /// SomeRequest
  58. /// </summary>
  59. [DataContract(Name = "SomeReq", Namespace = "http://schemas.datacontract.org/2004/07/Contracts.Models.Demo")]
  60. [Serializable]
  61.  
  62. public sealed class SomeReq : Request
  63. {
  64. /// <summary>
  65. /// 时间
  66. /// </summary>
  67. [DataMember]
  68. public DateTime Date { get; set; }
  69.  
  70. /// <summary>
  71. /// Data
  72. /// </summary>
  73. [DataMember]
  74. public DateTimeOffset DateUTC { get; set; }
  75.  
  76. }
  77.  
  78. /// <summary>
  79. /// IDemoService
  80. /// </summary>
  81. [ServiceContract ]
  82.  
  83. public interface IDemoService
  84. {
  85. /// <summary>
  86. /// Does something.
  87. /// </summary>
  88. /// <param name="request">The request.</param>
  89. /// <returns></returns>
  90. [OperationContract ]
  91. SomeResp DoSomething(SomeReq request);
  92. }

具体调用代码如下:

using System.Runtime.Serialization;
using System.ServiceModel;

  1. EndpointAddress address = new EndpointAddress("http://dev.xxx.io/Demo/DemoService.svc?wsdl");
  2. BasicHttpBinding binding = new BasicHttpBinding();
  3. ChannelFactory<IDemoService> factory = new ChannelFactory<IDemoService>(binding, address);
  4. IDemoService channel = factory.CreateChannel();
  5.  
  6. var result = channel.DoSomething(new SomeReq()
  7. {
  8. Date = DateTime.Now
  9. });

wcf第3步之报文分析及原生调用的更多相关文章

  1. 一次http完整的请求tcp报文分析

    一次http请求的报文分析 数据包如下: 第一个包113.31的主机(下边称之为客户端)给114.80的主机(下边称之为服务器)发送一个syn包请求建立连接 第二个包服务器回复客户端syn+ack表示 ...

  2. Wireshark安装使用及报文分析(图文详解)

    Wireshark是世界上最流行的网络分析工具.这个强大的工具可以捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与很多其他网络工具一样,Wireshark也使用pcapnetwork l ...

  3. HTTP 请求报文和响应报文分析和解刨!!

    http请求和响应报文分析 一>http请求报文主要包括三个部分:1.请求行:2.请求头:3;请求体: 1,请求行一般包括三个部分:请求方式:请求url : http协议版本. 请求方法:大部分 ...

  4. http协议请求报文与响应报文分析

    什么是HTTP协议: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到 不断地完善和扩展.目前在WWW中使用 ...

  5. Wireshark安装使用及报文分析

    先看链接!!! Wireshark使用教程:https://jingyan.baidu.com/article/93f9803fe902f7e0e56f5553.html Wireshark过滤规则筛 ...

  6. Spring源码分析之`BeanFactoryPostProcessor`调用过程

    前文传送门: Spring源码分析之预启动流程 Spring源码分析之BeanFactory体系结构 本文内容: AbstractApplicationContext#refresh前部分的一点小内容 ...

  7. Atitit main函数的ast分析  数组参数调用的ast astview解析

    Atitit main函数的ast分析  数组参数调用的ast astview解析 1.1. Xxcls.main(new String[]{"","bb"}) ...

  8. python通过http请求发送soap报文进行webservice接口调用

    最近学习Python调用webservice 接口,开始的时候主要采用suds 的方式生产client调用,后来发现公司的短信接口采用的是soap报文来调用的,然后开始了谷歌,最后采用httplib ...

  9. 【协议】AAA Radius协议的常用报文分析

    写在前面的话 RADIUS:Remote Authentication Dial In User Service,远程用户拨号认证系统由RFC2865,RFC2866定义,是应用最广泛的AAA协议. ...

随机推荐

  1. 从零自学Hadoop(09):使用Maven构建Hadoop工程

    阅读目录 序 Maven 安装 构建 示例下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,Source ...

  2. mysql 安装问题

    针对免安装版的mysql: 1.启动CMD,在mysql安装目录下  ~\bin\目录下,输入: mysqld -install   安装msyql服务: 2.启动MySQL服务,输入: net st ...

  3. 烂泥:Postfix邮件服务器搭建之虚拟用户配置

    virtual_gid_maps = static: virtual_transport = dovecot dovecot_destination_recipient_limit = 1 注意:po ...

  4. python MySQLdb 对mysql基本操作方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- import MySQLdb conn = MySQLdb.connect(host=',db='host') ...

  5. [转]Python 字符串操作实现代码(截取/替换/查找/分割)

    原文地址:http://www.jb51.net/article/38102.htm ps:好久没更新python代码了,这次用到了字符串,转来看看 Python 截取字符串使用 变量[头下标:尾下标 ...

  6. 理解CSS相对定位和固定定位

    × 目录 [1]相对定位 [2]固定定位 前面的话 一般地,说起定位元素是指position不为static的元素,包括relative.absolute和fixed.前面已经详细介绍过absolut ...

  7. JS入门学习,写一个时钟~

    <!-- 耽搁了几天,于是又继续回到JS的学习了~~ 各种头大,加油吧... --> <!doctype html><html><head> <t ...

  8. Fast RCNN 训练自己的数据集(3训练和检测)

    转载请注明出处,楼燚(yì)航的blog,http://www.cnblogs.com/louyihang-loves-baiyan/ https://github.com/YihangLou/fas ...

  9. ubuntu系统升级记录

    之前在openstack中安装了ubuntu 12.04虚拟机,版本较低,需要升级为高版本.下面分享下升级过程: ubuntu系统升级操作:$ cat /etc/issueUbuntu 12.04.5 ...

  10. jQuery美化下拉菜单插件dropkick

    dropkick是一款基于jquery库的美化下拉框下拉菜单的插件,它通过定制HTML插入可使丑陋无聊的<select>下拉列表变得美丽. name属性是唯一一个必需的填写的,不过你也应该 ...