如何从C# webservice生成WSDL文件

18

我创建了一个像这样的 WebService:

[WebService(Namespace = "http://ns")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GroupManagerService : WebService
{
    public GroupManagerService()
    {
    }

    [WebMethod]
    public bool MyMethod(string loginname, string country)
    {
        // code here...
    }
}

是否可以在不连接到正在运行的服务的情况下为此代码生成WSDL文件?我搜索并找到了关于SvcUtil.exewsdl.exe的信息,但这些仅适用于从正在运行的Web服务检索WSDL。

(对于Java,有一个名为java2wsdl的工具,是否有相当于C#的工具?)



:更新:
这个问题的背景是,我想向SharePoint添加新的CustomWebService,该服务应使用WSPBuilder在SharePoint的_vti_bin文件夹中部署。 请参见我的帖子在SharePoint.SE上。

并且我想自动生成(使用msbuild命令)必须放置在_vti_bin文件夹中的'MyServicewsdl.aspx''MyServicedisco.wsdl'



也许我错过了一些东西? svcutil.exe的输出为:

bin\Debug>SvcUtil.exe MyWebService.dll
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
Copyright (c) Microsoft Corporation.  All rights reserved.

Generating metadata files...
Warning: No metadata files were generated. No service contracts were exported.
 To export a service, use the /serviceName option. To export data contracts, spe
cify the /dataContractOnly option. This can sometimes occur in certain security
contexts, such as when the assembly is loaded over a UNC network file share. If
this is the case, try copying the assembly into a trusted environment and runnin
g it.

1
你是否被困在使用.NET 2.0的境地?为什么还在创建新的ASMX Web服务? - John Saunders
你必须使用 ASMX 服务着陆页面的 URL,而不是程序集 DLL,以获取代理文件。 - vapcguy
3个回答

15

我创建了一个工具,可以从一个包含一个或多个 WebServices 的编译好的 C# 程序集(dll)生成 WSDL 文件。 通常需要运行服务(IIS 或其他),托管 .asmx 文件,以便您可以使用 /MyWebService.asmx?wsdl 检索 WSDL。

该工具使用反射从程序集(dll)中检索所有信息生成 WSDL 文件。

下载链接:https://github.com/StefH/WSDLGenerator


1
WSDLGenerator也适用于WCF吗?我正在寻找一个解决方案,以回答这个问题。链接 - Alberto De Caro

5
请见svcutil /?
                          -= METADATA EXPORT =-

Description: svcutil.exe can export metadata for services, contracts and data types in compiled assemblies. To
    export metadata for a service, you must use the /serviceName option to indicate the service you would like
    to export. To export all Data Contract types within an assembly use the /dataContractOnly option. By
    default metadata is exported for all Service Contracts in the input assemblies.

Syntax: svcutil.exe [/t:metadata] [/serviceName:<serviceConfigName>] [/dataContractOnly] <assemblyPath>*

 <assemblyPath> - The path to an assembly that contains services, contracts or Data Contract types to be
                  exported. Standard command-line wildcards can be used to provide multiple files as input.

Options:

 /serviceName:<serviceConfigName> - The config name of a service to export. If this option is used, an
                                    executable assembly with an associated config file must be passed as
                                    input. Svcutil will search through all associated config files for the
                                    service configuration. If the config files contain any extension types,
                                    the assemblies containing these types must either be in the GAC or
                                    explicitly provided using the /r option.
 /reference:<file path>           - Add the specified assembly to the set of assemblies used for resolving
                                    type references. If you are exporting or validating a service that uses
                                    3rd-party extensions (Behaviors, Bindings and BindingElements) registered
                                    in config use this option to locate extension assemblies that are not in
                                    the GAC.  (Short Form: /r)
 /dataContractOnly                - Operate on Data Contract types only. Service Contracts will not be
                                    processed. (Short Form: /dconly)
 /excludeType:<type>              - The fully-qualified or assembly-qualified name of a type to exclude from
                                    export. This option can be used when exporting metadata for a service or a
                                    set of service contracts to exclude types from being exported. This option
                                    cannot be used with the /dconly option. (Short Form: /et)

2

Svcutil.exe会生成服务的WSDL。正确的用法是svcutil your.executable.dll(exe)。我经常使用这个命令,所以我确定它可以生成WSDL。


svcutil.exe 显示错误 "警告:未生成元数据文件"。 - Stef Heyenrath

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接