C# 如何从 wsdl 生成 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1394930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 16:02:38  来源:igfitidea点击:

how to generate web service out of wsdl

c#wsdlasmx

提问by alice7

Client provided me the wsdl to generate the web service.But when I used the wsdl.exe command it generated the .cs class out of it. I consumed that class in my web service and when I provided the wsdl to client it didn't match their schema. Actually I want the .asmx to be automatically generated from the wsdl so that I could fill in the web method. So that it will exactly match their schema. Hope it make sense.

客户端为我提供了 wsdl 来生成 Web 服务。但是当我使用 wsdl.exe 命令时,它从中生成了 .cs 类。我在我的 Web 服务中使用了该类,当我向客户端提供 wsdl 时,它与他们的架构不匹配。实际上,我希望从 wsdl 自动生成 .asmx,以便我可以填写 web 方法。这样它将完全匹配他们的架构。希望这是有道理的。

回答by marc_s

How about using the wsdl /serveror wsdl /serverinterfaceswitches? As far as I understand the wsdl.exe command line properties, that's what you're looking for.

使用wsdl /serverwsdl /serverinterface开关怎么样?据我了解 wsdl.exe 命令行属性,这就是您要查找的内容。

- ADVANCED -

/server

/服务器

Server switch has been deprecated. Please use /serverInterface instead.
Generate an abstract class for an xml web service implementation using
ASP.NET based on the contracts. The default is to generate client proxy
classes.

On the other hand: why do you want to create obsolete technology solutions? Why not create this web service as a WCF service. That's the current and more modern, much more flexible way to do this!

另一方面:您为什么要创建过时的技术解决方案?为什么不将此 Web 服务创建为 WCF 服务。这是当前的、更现代、更灵活的方式来做到这一点!

Marc

马克



UPDATE:

更新:

When I use wsdl /serveron a WSDL file, I get this file created:

当我wsdl /server在 WSDL 文件上使用时,我创建了这个文件:

[WebService(Namespace="http://.......")]
public abstract partial class OneCrmServiceType : System.Web.Services.WebService 
{
    /// <remarks/>
    [WebMethod]
    public abstract void OrderCreated(......);
}

This is basically almost exactly the same code that gets generated when you add an ASMX file to your solution (in the code behind file - "yourservice.asmx.cs"). I don't think you can get any closer to creating an ASMX file from a WSDL file.

这基本上与将 ASMX 文件添加到解决方案时生成的代码几乎完全相同(在文件隐藏代码中 - “yourservice.asmx.cs”)。我认为您无法更接近从 WSDL 文件创建 ASMX 文件。

You can always add the "yourservice.asmx" manually - it doesn't really contain much:

您始终可以手动添加“yourservice.asmx” - 它实际上并不包含太多内容:

<%@ WebService Language="C#" CodeBehind="YourService.asmx.cs" 
      Class="YourServiceNamespace.YourServiceClass" %>

回答by John Saunders

You cannot guarantee that the automatically-generated WSDL will match the WSDL from which you create the service interface.

您不能保证自动生成的 WSDL 将与您创建服务接口的 WSDL 相匹配。

In your scenario, you should place the WSDL file on your web site somewhere, and have consumers use that URL. You should disable the Documentationprotocol in the web.config so that "?wsdl" does not return a WSDL. See <protocols>Element.

在您的场景中,您应该将 WSDL 文件放在您的网站上的某个位置,并让消费者使用该 URL。您应该Documentation在 web.config 中禁用该协议,以便“?wsdl”不会返回 WSDL。请参阅<protocols>元素

Also, note the first paragraph of that article:

另外,请注意该文章的第一段:

This topic is specific to a legacy technology. XML Web services and XML Web service clients should now be created using Windows Communication Foundation (WCF).

本主题特定于传统技术。现在应该使用Windows Communication Foundation (WCF)创建 XML Web services 和 XML Web services 客户端 。

回答by p.campbell

There isn't a magic bullet solution for what you're looking for, unfortunately. Here's what you can do:

不幸的是,对于您正在寻找的内容,没有灵丹妙药的解决方案。您可以执行以下操作:

  • create an Interface class using this command in the Visual Studio Command Prompt window:

    wsdl.exe yourFile.wsdl /l:CS /serverInterface
    Use VB or CS for your language of choice. This will create a new .csor .vbfile.

  • Create a new .NET Web Service project. Import Existing File into your project - the file that was created in the step above.

  • In your .asmx.csfile in Code-View, modify your class as such:

  • 在 Visual Studio 命令提示符窗口中使用此命令创建接口类:

    wsdl.exe yourFile.wsdl /l:CS /serverInterface
    使用 VB 或 CS 作为您选择的语言。这将创建一个新的.cs.vb文件。

  • 创建一个新的 .NET Web 服务项目。将现有文件导入您的项目 - 在上述步骤中创建的文件。

  • .asmx.cs代码视图中的文件中,修改您的类:

 

 

 public class MyWebService : System.Web.Services.WebService, IMyWsdlInterface
 {    
     [WebMethod]
     public string GetSomeString()
     {
         //you'll have to write your own business logic 
         return "Hello SOAP World";
     }
 }

回答by Neilb

You can generate the WS proxy classes using WSCF (Web Services Contract First) tool from thinktecture.com. So essentially, YOU CAN create webservices from wsdl's. Creating the asmx's, maybe not, but that's the easy bit isn't it? This tool integrates brilliantly into VS2005-8 (new version for 2010/WCF called WSCF-blue). I've used it loads and always found it to be really good.

您可以使用 thinktecture.com 上的 WSCF(Web 服务契约优先)工具生成 WS 代理类。所以本质上,您可以从 wsdl 创建 web 服务。创建 asmx,也许不是,但这很容易,不是吗?该工具与 VS2005-8(2010/WCF 的新版本称为 WSCF-blue)完美集成。我已经大量使用它,并且总是发现它非常好。

回答by Red Swan

This may be very late in answering. But might be helpful to needy: How to convert WSDL to SVC :

这可能会很晚才回答。但可能对有需要的人有帮助: How to convert WSDL to SVC :

  1. Assuming you are having .wsdl file at location "E:\" for ease in access further.
  2. Prepare the command for each .wsdl file as: E:\YourServiceFileName.wsdl
  3. Permissions: Assuming you are having the Administrative rights to perform permissions. Open directory : C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
  4. Right click to amd64 => Security => Edit => Add User => Everyone Or Current User => Allow all permissions => OK.
  5. Prepare the Commands for each file in text editor as: wsdl.exe E:\YourServiceFileName.wsdl /l:CS /server.
  6. Now open Visual studio command prompt from : C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts\VS2013 x64 Native Tools Command Prompt.
  7. Execute above command.
  8. Go to directory : C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64, Where respective .CS file should be generated.

    9.Move generated CS file to appropriate location.

  1. 假设您在位置“E:\”有 .wsdl 文件以便于进一步访问。
  2. 为每个 .wsdl 文件准备命令:E:\YourServiceFileName.wsdl
  3. 权限:假设您具有执行权限的管理权限。打开目录:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
  4. 右键单击 amd64 => 安全性 => 编辑 => 添加用户 => 所有人或当前用户 => 允许所有权限 => 确定。
  5. 在文本编辑器中为每个文件准备命令:wsdl.exe E:\YourServiceFileName.wsdl /l:CS /server。
  6. 现在从以下位置打开 Visual Studio 命令提示符:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\Shortcuts\VS2013 x64 Native Tools Command Prompt。
  7. 执行上面的命令。
  8. 转到目录:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64,其中应生成相应的 .CS 文件。

    9.将生成的CS文件移动到合适的位置。

回答by abdul rehman kk

step-1

第1步

open -> Visual Studio 2017 Developer Command Prompt

step-2

第2步

WSDL.exe  /OUT:myFile.cs WSDLURL  /Language:CS /serverInterface
  • /serverInterface (this to create interface from wsdl file)
  • WSDL.exe (this use to create class from wsdl. this comes with .net
  • /OUT: (output file name)
  • /serverInterface(用于从 wsdl 文件创建接口)
  • WSDL.exe(用于从 wsdl 创建类。这随 .net
  • /OUT:(输出文件名)

step-2

第2步

create new "Web service Project"

创建新的“Web 服务项目”

step-3

步骤 3

add -> web service

添加 ->网络服务

step-4

第四步

copy all code from myFile.cs(generated above) except "using classes"eg:

myFile.cs(上面生成的)复制所有代码,除了“使用类”,例如:

 /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.1055.0")]
    [System.Web.Services.WebServiceBindingAttribute(Name="calculoterServiceSoap",Namespace="http://tempuri.org/")]

public interface ICalculoterServiceSoap {

    /// <remarks/>
    [System.Web.Services.WebMethodAttribute()]
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/addition", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    string addition(int firtNo, int secNo);
}

step-4

第四步

past it into your webService.asmx.cs(inside of namespace) created above in step-2

将它传递到上面在第 2 步中创建的webService.asmx.cs(在namespace 内

step-5

步骤 5

inherit the interface class with your web service class eg:

使用您的 Web 服务类继承接口类,例如:

public class WebService2 : ICalculoterServiceSoap