由WSDL文件生成WEB service server端C#程序(转)

时间:2022-08-30 12:58:29

一般一个已经实现功能的WEB Server会发布自己的WSDL文件,供客户端生成代理类。

但有时是先有的server与client交互的接口定义(WSDL)文件,然后由server和client端分别写程序,一个提供web服务,一个使用web服务。

以下介绍如何由已有的WSDL文件在VS2005中生成server端代码。

1)使用VS2005提供的工具wsdl.exe由WSDL文件生成cs文件:

使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转换的wsdl文件中import了其他wsdl文件,则所有文件都应列出,包括使用到的xsd文件也应列出)。输出将是 一个代码文件(默认是C#的,如果需要别的语言,参考MSDN中wsdl.exe的使用说明),其中包含每个 wsdl 绑定的接口。

示例:假设ServerInterfaceSample.wsdl文件中import了importedSample.wsdl,并使用Service.xsd作为schema文件;

wsdl.exe /si ServerInterfaceSample.wsdl importedSample.wsdl Service.xsd

生成代码如下:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;

// 
// This source code was auto-generated by wsdl, Version=2.0.50727.42.
//

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.42")]
[System.Web.Services.WebServiceBindingAttribute(Name="org.csapi.cs.IpChargingManagerSOAPBinding", Namespace="http://www.csapi.org/cs/wsdl")]
public interface IOrgcsapicsIpChargingManagerSOAPBinding {     //重要,后面需要同此处一致

/// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    orgcsapicsTpChargingSessionID createChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapiTpAddress user, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);

/// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.cs.IpChargingManager.createSplitChargingSession", RequestNamespace="http://www.csapi.org/cs/schema", ResponseElementName="org.csapi.cs.IpChargingManager.createSplitChargingSessionResult", ResponseNamespace="http://www.csapi.org/cs/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    [return: System.Xml.Serialization.XmlElementAttribute("return", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    orgcsapicsTpChargingSessionID createSplitChargingSession([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appChargingSession, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] string sessionDescription, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpMerchantAccountID merchantAccount, [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] orgcsapiTpAddress[] users, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] orgcsapicsTpCorrelationID correlationID);

/// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallback", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    void setCallback([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface);

/// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("", RequestElementName="org.csapi.IpService.setCallbackWithSessionID", RequestNamespace="http://www.csapi.org/osa/schema", ResponseElementName="org.csapi.IpService.setCallbackWithSessionIDResult", ResponseNamespace="http://www.csapi.org/osa/schema", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    void setCallbackWithSessionID([System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] EndpointReferenceType appInterface, [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] int sessionID);
}

代码中有一个接口类,并给出了接口函数声明.

此功能的优点是将实现代码和协定代码(如接口所定义的那样)分开。 如果更改 wsdl,然后重新生成接口,则不会丢失任何实现代码。 若要遵循建议的设计指南,则不应将任何代码放置在更改 wsdl 协定的实现中。 同样,不应使用影响运行时行为的代码来更改接口。 协定和实现的详细信息应该分开。

2) 在VS IDE中新建一个web service工程,加入新生成的**Interface.cs文件.
将自动生成的Service类改为从加入的Interface接口类派生,并实现所有的接口函数.(注意命名空间一致,实现的函数也只能是接口内的函数,否则会出现找不到服务名的问题)

[WebService(Namespace = "http://www.csapi.org/cs/wsdl")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : IOrgcsapicsIpChargingManagerSOAPBinding //由public interface IOrgcsapicsIpChargingManagerSOAPBinding  //重要,后面需要同此处一致 处派生
{
public orgcsapicsTpChargingSessionID createChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress user,orgcsapicsTpCorrelationID correlationID)
{
return new orgcsapicsTpChargingSessionID();
} public orgcsapicsTpChargingSessionID createSplitChargingSession(EndpointReferenceType appChargingSession, string sessionDescription, orgcsapicsTpMerchantAccountID merchantAccount, orgcsapiTpAddress[] users, orgcsapicsTpCorrelationID correlationID)
{
return new orgcsapicsTpChargingSessionID();
} public void setCallback(EndpointReferenceType appInterface)
{ } public void setCallbackWithSessionID(EndpointReferenceType appInterface, int sessionID)
{ } } 

大功告成,在实现函数中加入处理代码就可以了.

注意:

1.注意命名空间一定要一致。

2.出现SOAPAction相同问题

<textarea cols="50" rows="15" name="code" class="c-sharp">解决办法: 第一种: 在类前面加上 [System.Web.Services.Protocols.SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)] 第二种:用不同的soapAction值 </textarea>

若wsdl的位置发生改变需要修改(如由:http://localhost:3222/WebService1.asmx改成了:http://localhost:3222/myWebService/WebService1.asmx):

1.修改客户端的Web.config的<appSettings>

由<add key="localhost.WebService1" value="http://localhost:3222/WebService1.asmx"/>改成

<add key="localhost.WebService1" value="http://localhost:3222/myWebService/WebService1.asmx"/>

2.修改App_WebReferences内带.discomap,  .disco,   .wsdl后缀文件的所有含http://localhost:3222/WebService1.asmx都改成http://localhost:3222/myWebService/WebService1.asmx

由WSDL文件生成WEB service server端C#程序(转)的更多相关文章

  1. Eclipse&plus;Axis使用WSDL文件生成Web Service服务端&sol;客户端

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...

  2. 使用Eclipse自带Web Service插件&lpar;Axis1&period;4&rpar;生成Web Service服务端&sol;客户端

    创建一个名字为math的Java web工程,并将WSDL文件拷入该工程中 将Axis所需的jar包拷贝至WebRoot\WEB-INF\lib目录下,这些jar包会自动导入math工程中 一,生成W ...

  3. 使用Eclipse自带的Axis1插件生成Web Service服务端客户端

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) WSDL文件的创建过程见http://blog.csdn.net/a19881029/ ...

  4. VS2010 根据WSDL文件&lpar;java Web Service&rpar;生成&period;cs文件

    我们添加webService引用,一般是通过 添加服务引用完成的,其实 添加服务引用 在背后为我们生成了代理类. 我们手动生成代理类方法: 1.通过java Web Service,生成wsdl文件: ...

  5. Eclipse&plus;Axis自动生成Web Service WSDL文件

    JDK版本:1.5.0_22 Eclipse版本:Helios Service Release 2(3.6.2) 首先创建一个web工程,创建过程如下: 如果选择Apache Tomcat v5.5, ...

  6. C&num;根据WSDL文件生成WebService服务端代码

    转自:http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html 虽然现在已经进入了.NET FrameWork 4.0的时代,WebS ...

  7. 根据wsdl文件,Web工程自动生成webservice客户端调用

    根据wsdl文件,Web工程自动生成webservice客户端调用 1,工具:带有webservice插件的eclips 2,步骤: (1),新建一个Web工程:WSDLTest (2),浏览器访问W ...

  8. axis2框架用wsdl文件生成的服务端MessageReceiveInOut文件注意事项

    在用axis2生成服务端文件和客户端文件,当客户端文件调用服务端文件时,都是通过wsdl文件生成的 配置文件进行相互的调用. 在一开始做开发测试的时候,通过soapUI进行调用接口的时候,可以调用成功 ...

  9. Linux下用gSOAP开发Web Service服务端和客户端程序

    网上本有一篇流传甚广的C版本的,我参考来实现,发现有不少问题,现在根据自己的开发经验将其修改,使用无误:另外,补充同样功能的C++版本,我想这个应该更有用,因为能用C++,当然好过受限于C. 1.gS ...

随机推荐

  1. ValidateRequest问题

    1,在出现该错误的页面头部的page中加入ValidateRequest="false",那么该页面的任何一次Post提交都不会再验证提交内容的安全性.如: <%@ Page ...

  2. 调试Android USB遇到的令人费解的问题

    上周参照网上代码,做了USB的初步探测程序,工作正常 .今天从硬件部拿到了一段例程,原本打算参考它来完善自己的程序.但运行之后总是报错,逐步跟进错误,进而发现了一个匪疑所思的问题.调试一天也未发现原因 ...

  3. &lbrack;ES6&rsqb; Array -- Destructuring and Rest Parameters &amp&semi;&amp&semi; for &period;&period;of &amp&semi;&amp&semi; Arrat&period;find&lpar;&rpar;

    We can use the destructing and rest parameters at the same time when dealing with Array opration. Ex ...

  4. BZOJ1013 &lbrack;JSOI2008&rsqb;球形空间产生器sphere(高斯消元)

    1013: [JSOI2008]球形空间产生器sphere Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 4846  Solved: 2525[Subm ...

  5. ESPlatform 支持的三种群集模型 —— ESFramework通信框架 4&period;0 进阶(09)

    对于最多几千人同时在线的通信应用,通常使用单台服务器就可以支撑.但是,当同时在线的用户数达到几万.几十万.甚至百万的时候,我们就需要很多的服务器来分担负载.但是,依据什么规则和结构来组织这些服务器,并 ...

  6. 【python进阶】Garbage&Tab;collection垃圾回收1

    前言 GC垃圾回收在python中是很重要的一部分,同样我将分两次去讲解Garbage collection垃圾回收,此篇为Garbage collection垃圾回收第一篇,下面开始今天的说明~~~ ...

  7. GoLang simple-project-demo-02

    GoLang 有很多种数据类型:字符型,整型,浮点型,布尔型.下面是基础例子: package main import "fmt" func main() { fmt.Printl ...

  8. dp专题练习

    顺便开另外一篇放一些学过的各种dp dp总结:https://www.cnblogs.com/henry-1202/p/9194066.html 开坑先放15道题,后面慢慢补 目标50道题啦~~,目前 ...

  9. &lbrack;leetcode&rsqb;252&period; Meeting Rooms会议室有冲突吗

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  10. 20145335郝昊《网络攻防》Exp4 Adobe阅读器漏洞攻击

    20145335郝昊<网络攻防>Exp4 Adobe阅读器漏洞攻击 实验内容 初步掌握平台matesploit的使用 有了初步完成渗透操作的思路 本次攻击对象为:windows xp sp ...