wcf第3步之报文分析及原生调用

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

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/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/Imports">
<xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd0" namespace="http://tempuri.org/"/>
<xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
<xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo"/>
<xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/DataTypes"/>
<xsd:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/System"/>
</xsd:schema>
</wsdl:types>
<wsdl:message name="IDemoService_DoSomething_InputMessage">
<wsdl:part name="parameters" element="tns:DoSomething"/>
</wsdl:message>
<wsdl:message name="IDemoService_DoSomething_OutputMessage">
<wsdl:part name="parameters" element="tns:DoSomethingResponse"/>
</wsdl:message>
<wsdl:portType name="IDemoService">
<wsdl:operation name="DoSomething">
<wsdl:input wsaw:Action="http://tempuri.org/IDemoService/DoSomething" message="tns:IDemoService_DoSomething_InputMessage"/>
<wsdl:output wsaw:Action="http://tempuri.org/IDemoService/DoSomethingResponse" message="tns:IDemoService_DoSomething_OutputMessage"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="BasicHttpBinding_IDemoService" type="tns:IDemoService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="DoSomething">
<soap:operation soapAction="http://tempuri.org/IDemoService/DoSomething" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="DemoService">
<wsdl:port name="BasicHttpBinding_IDemoService" binding="tns:BasicHttpBinding_IDemoService">
<soap:address location="http://dev.xxx.io/Demo/DemoService.svc"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

可以看到

1.wcf服务地址

http://dev.xxx.io/Demo/DemoService.svc

2.引用了4个命名空间
Serialization
Contracts.Models.Demo
DataTypes
System

3.Service的名称及接口

DemoService,IDemoService

4.服务接口的方法 DoSomething

再访问
 
来读取类型的具体字段http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3
<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">
<xs:complexType name="Request">
<xs:sequence>
<xs:element minOccurs="0" name="UserId" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Request" nillable="true" type="tns:Request"/>
<xs:complexType name="Response">
<xs:sequence>
<xs:element minOccurs="0" name="ErrorCode" type="xs:int"/>
<xs:element minOccurs="0" name="Message" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="Success" type="xs:boolean"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Response" nillable="true" type="tns:Response"/>
</xs:schema>

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

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://tempuri.org/" elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo"/>
<xs:element name="DoSomething">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo" minOccurs="0" name="request" nillable="true" type="q1:SomeReq"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DoSomethingResponse">
<xs:complexType>
<xs:sequence>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/Contracts.Models.Demo" minOccurs="0" name="DoSomethingResult" nillable="true" type="q2:SomeResp"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

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

<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">
<xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd4" namespace="http://schemas.datacontract.org/2004/07/System"/>
<xs:import schemaLocation="http://dev.xxx.io/Demo/DemoService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/DataTypes"/>
<xs:complexType name="SomeReq">
<xs:complexContent mixed="false">
<xs:extension xmlns:q1="http://schemas.datacontract.org/2004/07/DataTypes" base="q1:Request">
<xs:sequence>
<xs:element minOccurs="0" name="Date" type="xs:dateTime"/>
<xs:element xmlns:q2="http://schemas.datacontract.org/2004/07/System" minOccurs="0" name="DateUTC" type="q2:DateTimeOffset"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="SomeReq" nillable="true" type="tns:SomeReq"/>
<xs:complexType name="SomeResp">
<xs:complexContent mixed="false">
<xs:extension xmlns:q3="http://schemas.datacontract.org/2004/07/WHTR.DataTypes" base="q3:Response">
<xs:sequence>
<xs:element minOccurs="0" name="Date" type="xs:dateTime"/>
<xs:element minOccurs="0" name="DateStr" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ResultDate" nillable="true" type="xs:string"/>
<xs:element minOccurs="0" name="ResultDateUTC" nillable="true" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="SomeResp" nillable="true" type="tns:SomeResp"/>
</xs:schema>

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

[Serializable]
    [DataContract(Name = "Response", Namespace = "http://schemas.datacontract.org/2004/07/DataType")]

    public class Response
    {
       [DataMember]
        public int ErrorCode { get; set; }
      [DataMember]
        public string Message { get; set; }
      [DataMember]
        public bool Success { get; set; }
    }

    /// <summary>
    /// SomeResponse
    /// </summary>
    [DataContract(Name = "SomeResp", Namespace = "http://schemas.datacontract.org/2004/07/DataType")]

    [Serializable]
    public sealed class SomeResp : Response
    {
        /// <summary>
        /// 时间格式的Date
        /// </summary>
       [DataMember]
        public DateTime Date { get; set; }

        /// <summary>
        /// 格式成字符串的时间
        /// </summary>
       [DataMember]
        public string DateStr { get; set; }

        /// <summary>
        /// 返回传入的Date
        /// </summary>
       [DataMember]
        public string ResultDate { get; set; }

        /// <summary>
        /// 返回传入的Date
        /// </summary>
      [DataMember]
        public string ResultDateUTC { get; set; }
    }

    [Serializable]

    [DataContract(Name = "Request", Namespace = "http://schemas.datacontract.org/2004/07/Contracts.Models.Demo")]
    public class Request
    {
        [DataMember]
        public string UserId { get; set; }
    }

    /// <summary>
    /// SomeRequest
    /// </summary>
    [DataContract(Name = "SomeReq", Namespace = "http://schemas.datacontract.org/2004/07/Contracts.Models.Demo")]
    [Serializable]

    public sealed class SomeReq : Request
    {
        /// <summary>
        /// 时间
        /// </summary>
        [DataMember]
        public DateTime Date { get; set; }

        /// <summary>
        /// Data
        /// </summary>
        [DataMember]
        public DateTimeOffset DateUTC { get; set; }

    }

    /// <summary>
    /// IDemoService
    /// </summary>
    [ServiceContract ]

    public interface IDemoService
    {
        /// <summary>
        /// Does something.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        [OperationContract ]
        SomeResp DoSomething(SomeReq request);
    }

具体调用代码如下:

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

EndpointAddress address = new EndpointAddress("http://dev.xxx.io/Demo/DemoService.svc?wsdl");
            BasicHttpBinding binding = new BasicHttpBinding();
            ChannelFactory<IDemoService> factory = new ChannelFactory<IDemoService>(binding, address);
            IDemoService channel = factory.CreateChannel();

            var result = channel.DoSomething(new SomeReq()
            {
                Date = DateTime.Now
            });  
时间: 2024-08-28 14:58:52

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

http报文分析

http报文分析 一.什么是报文 用于HTTP协议交互的信息被称为报文. 请求端(客户端)的HTTP报文叫做请求报文,响应端(服务器端)的叫做响应报文. HTTP报文本身是由多行数据构成的字符串文本. HTTP报文大致上可分为报文首部和报文主体两块,两者由最初出现的空行划分. 5. 通常,并不一定要有报文主体. 二.组成 起始行:报文的第一行就是起始行,在请求报文中用来说明要做些什么,在响应报文中说明出现了什么情况 报文头:起始行后面有零个或多个字段.每个字段都包含一个名字和一个值,为了便于解析

深入浅出Alljoyn——实例分析之远程调用(Method)篇

深入浅出就是很深入的学习了很久,还是只学了毛皮,呵呵! 服务端完整代码: 1 #include <qcc/platform.h> 2 3 #include <assert.h> 4 #include <signal.h> 5 #include <stdio.h> 6 #include <vector> 7 8 #include <qcc/String.h> 9 10 #include <alljoyn/BusAttachment

IOS调用WCF提供的服务方法,但是方法的参数是WCF那边自定义的对象,这样有办法调用么,如果可以IOS应该怎么传参呢?请问有了解的么,

最近做一个项目后端使用WCF接收Android手机拍照并带其它参数保存到服务器里:刚好把最近学习的WCF利用上,本以为是个比较简单的功能应该很好实现,没想到其中碰到不少问题,在网上搜索很久一直没有想到的解决方案,最后实现对数据流的分段写入然后后端再来解析流实现的此功能:后端运用WCF中的REST来接收数据:REST还是比较简单的知识,若是不懂可以简单网上了解一下:下面我们先了解一些本次运用到的理论知识: 一:理论知识 由于低层协议特性限制,WCF的流模式只支持如下四种:1:BasicHttpBi

WCF安全性认证:SoapHeader(二)使用HTTP Request调用

在前面第一篇介绍的WinFrom Client 端程序为了送出request时要产生SoapHeader﹐而另外撰写了一个ClientHeader类库﹐并且必须在组态加入对应的设定﹐手续看起来有些复杂.实际上﹐有时会希望是以WebRequest的原生方式自行组合Soap 格式数据来进行数据的交换. 在开始之前需要先知道一些数据的规则﹐首先由wsdl规格中可以找到SOAPAction. 前面的介绍过程中在使用Fiddler观察数据时﹐可以看到送出的标准Soap数据格式﹐这是现在在程序中要自行组合的

常用http响应报文分析

这是我在使用Asp.Net的时候,整理的的一些关于Http响应报文的分析笔记,零零散散的记录, 现在贴出来,抛砖引玉,如果有什么不对或者不严谨的地方,请各位大神不吝赐教. 一.HTTP响应码响应码由三位十进制数字组成,它们出现在由HTTP服务器发送的响应的第一行. 响应码分五种类型,由它们的第一位数字表示: 1xx:信息,请求收到,继续处理 2xx:成功,行为被成功地接受.理解和采纳 3xx:重定向,为了完成请求,必须进一步执行的动作 4xx:客户端错误,请求包含语法错误或者请求无法实现 5xx

TCP三次握手报文分析

1.TCP客户进程发送请求连接SYN报文[SYN=1,ACK=1](不携带数据但是要消耗一个序号,所以确认报文ACK=1) 2.TCP服务进程同意建立连接,发送确认SYN连接接受报文(SEQ=0 ACK=1) 3.TCP客户进程再次确认(ACK报文段[SYN=0,ACK=1]不携带数据则不消耗序号,所以数据传送接到TCP客户进程SEQ仍未1) 第一次数据传送(1bytes~153bytes,则确认报文段ACK=154) 第一次确认  第二.三次数据传送 第二次确认 TCP连接释放四次挥手分析:正

DHCP协议报文分析

DHCP协议分析 一.DHCP简介 DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)用来为网络设备动态地分配 IP地址等网络配置参数. DHCP 采用客户端/服务器通信模式,由客户端向服务器提出请求分配网络配置参数的申请,服务器返回为客户端分配的 IP 地址等配置信息,以实现 IP 地址等信息的动态配置. 二.DHCP工作过程 1.IP 地址动态获取过程 2.主要通过四个阶段进行: A.发现阶段,即 DHCP 客户端寻找 DHCP 服务器的阶段

golang实现dns域名解析(三):响应报文分析

前面说了构造请求发送报文,接下来我们好好研究下如何解析服务器端发回来的应答信息. 首先还是用前面的程序代码发一个请求,用抓包工具看看应答的内容有哪些: 截图的第一部分是返回信息的统计,表明这个返回的包数据包含一个问题,5个权威应答,5个附加信息.第二部分是问题的内容,第三部分是权威应答的内容,第四部分是附加信息的内容.再往下面就是接收到的原始数据的展示,这里需要提及的一点就是为了减小报文,域名系统使用一种压缩方法来消除报文中域名的重复.使用这种方法,后面重复出现的域名或者labels被替换为指向

Linux网络编程——原始套接字实例:MAC 头部报文分析

通过<Linux网络编程——原始套接字编程>得知,我们可以通过原始套接字以及 recvfrom( ) 可以获取链路层的数据包,那我们接收的链路层数据包到底长什么样的呢? 链路层封包格式 MAC 头部(有线局域网) 注意:CRC.PAD 在组包时可以忽略 链路层数据包的其中一种情况: 1 unsigned char msg[1024] = { 2 //--------------组MAC--------14------ 3 0xb8, 0x88, 0xe3, 0xe1, 0x10, 0xe6,