如何从 C# 网络服务生成 WSDL 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2340797/
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
How to generate a WSDL file from a C# webservice
提问by Stef Heyenrath
I've created a WebService like this:
我创建了一个这样的 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...
}
}
Is it possible to generate a WSDL file for this code without connecting to a running service? I searched and I found information about SvcUtil.exe& wsdl.exe, but these work only when retrieving the WSDL from a running WebService.
是否可以在不连接到正在运行的服务的情况下为此代码生成 WSDL 文件?我搜索,我发现有关信息svcutil.exe的&Wsdl.exe用,但这些工作中检索从运行WebService的WSDL只有当。
(For java, there is a tool called java2wsdl, is there a equivalent for c#?)
(对于java,有一个名为java2wsdl的工具,是否有与c#等效的工具?)
:Update:
The context for this question is that I want to add new CustomWebService to SharePoint which should deployed using WSPBuilder in the _vti_bin folder on SharePoint.
See also my poston SharePoint.SE.
:更新:
这个问题的上下文是我想将新的 CustomWebService 添加到 SharePoint,它应该使用 WSPBuilder 部署在 SharePoint 上的 _vti_bin 文件夹中。另请参阅我在 SharePoint.SE 上的帖子。
And I want to automatically generate (using msbuild commands) the 'MyServicewsdl.aspx'& 'MyServicedisco.wsdl'which must be placed in the _vti_bin folder.
我想自动生成(使用 msbuild 命令)必须放在 _vti_bin 文件夹中的“MyServicewsdl.aspx”和“MyServicedisco.wsdl”。
Maybe I'm missing some things?
Output from svcutil.exe is:
也许我错过了一些东西?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.
采纳答案by Stef Heyenrath
I've created a tool which can generate a WSDL file from a compiled c# assembly (dll) which contains one or more WebServices. Normally you require a running service (IIS or other) which hosts the .asmx so that you can retrieve the WSDL using /MyWebService.asmx?wsdl
我创建了一个工具,它可以从包含一个或多个 Web 服务的已编译 c# 程序集 (dll) 生成 WSDL 文件。通常,您需要一个托管 .asmx 的运行服务(IIS 或其他),以便您可以使用 /MyWebService.asmx?wsdl 检索 WSDL
This tool generate a WSDL file using reflection to retrieve all information from an assembly (dll).
此工具使用反射生成 WSDL 文件以从程序集 (dll) 中检索所有信息。
Download can be found at https://github.com/StefH/WSDLGenerator
回答by John Saunders
See svcutil /?
看 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)
回答by jpsstavares
Svcutil.exe will definitely generate the WSDL with the service down. The correct usage is svcutil your.executable.dll(exe). I'm using this a lot so I'm sure it will generate the WSDL.
Svcutil.exe 肯定会在服务关闭时生成 WSDL。正确的用法是 svcutil your.executable.dll(exe)。我经常使用它,所以我确信它会生成 WSDL。