C # generates WebService service-side code based on WSDL file

Source: Internet
Author: User
Tags wsdl

Transferred from: http://www.cnblogs.com/liyi93/archive/2012/01/30/2332320.html

Although it has now entered the era of the. NET FrameWork 4.0, WebService has been phased out and replaced by WCF.

However, it is unavoidable to encounter the need to work with older versions of the program and develop it in accordance with previous documentation.

Typically a webservice that has implemented functionality publishes its own WSDL file for the client to invoke to generate the proxy class.

But sometimes it is the interface definition (WSDL) file that the server interacts with the client, then the server and client write the program separately, one provides the Web service and one uses the Web service.

Recently, I have also encountered this problem. Since the business party provided only the WSDL file and determined its specification, we need to develop the service side for invocation.

1. Use the tools provided by VS2010 Wsdl.exe to generate CS files from a WSDL file

Use Wsdl.exe's/serverinterface option (or abbreviated/SI) to specify the input WSDL file (note that if you import other WSDL files in the WSDL file that you are converting, all files should be listed, including the XSD file that you use). The output will be a code file (the default is C #, if you need a different language, refer to the instructions for using Wsdl.exe in MSDN), which contains the interface for each WSDL binding.

Example: Suppose there is a serverinterfacesample.wsdl

Wsdl.exe/si serverinterfacesample.wsdl

If you use Service.xsd as the schema file, change

Wsdl.exe/si serverinterfacesample.wsdl service.xsd

The generated code is as follows:

------------------------------------------------------------------------------//<auto-generated>// This code is generated by the tool. Runtime version: 4.0.30319.239////changes to this file may result in incorrect behavior, and if//regenerate code, these changes will be lost. </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 is generated automatically by WSDL, version=4.0.30319.1. <remarks/>[system.codedom.compiler.generatedcodeattribute ("WSDL", "4.0.30319.1") [ System.Web.Services.WebServiceBindingAttribute (name= "Webservicesoap", namespace= "http://tempuri.org/")]public Interface Iwebservicesoap {//<remarks/> [System.Web.Services.WebMethodAttribute ()] [System.Web.Servi Ces. Protocols.soapdocumentmethodattribute ("Http://tempuri.org/sendSMS", requestnamespace= "http://tempuri.org/", Responsenamespace= "http://tempuri.org/", Use=system. Web.Services.Description.SoapBindingUse.Literal, parameterstyle= System.Web.Services.Protocols.SoapParameterStyle.Wrapped)] Long sendsms (infoheader header, string sessionId, String     sender, String smscontent, String receiverlist, String ProductCode, String pseudoflag);}

2, use the above generated files to modify, the implementation of your WebService method can be.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.