2010-02-26 71 views
15

我創建一個WebService這樣的WSDL文件:如何生成從C#web服務

[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.exe & wsdl.exe,但是這些僅在從運行的WebService中檢索WSDL時才起作用。

(對於的Java,有一個叫的Java2WSDL工具,是有C#等效?)



:更新:
的上下文這個問題是我想將新的CustomWebService添加到應該使用WSPBuilder在SharePoint上的_vti_bin文件夾中部署的SharePoint。 另請參閱SharePoint.SE上的my post

並且我想要(使用的msbuild命令)自動生成「MyServicewsdl.aspx」 & 「MyServicedisco.wsdl」必須放置在_vti_bin文件夾中。



也許我錯過了一些東西?從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服務? – 2010-02-26 10:16:30

+0

查看已更新的問題文本... – 2010-02-26 11:28:22

+0

您必須使用ASMX服務登錄頁面的URL而不是程序集DLL來獲取代理文件。 – vapcguy 2015-09-10 23:56:14

回答

15

我已經創建了一個工具,它可以從包含一個或多個WebServices的已編譯的c#程序集(dll)生成WSDL文件。 通常您需要它承載的.asmx,這樣就可以使用/MyWebService.asmx?wsdl

該工具使用反射來檢索程序集的所有信息生成WSDL文件檢索WSDL正在運行的服務(IIS或其他) (DLL)。

下載可以在https://github.com/StefH/WSDLGenerator

+1

WSDLGenerator是否也與WCF一起使用?我正在搜索[這個問題]的解決方案(http://stackoverflow.com/questions/11099335/bind-a-custom-wsdl-to-an-existing-wcf-service)。 – 2012-06-25 20:00:52

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。

+0

svcutil.exe顯示錯誤「警告:沒有生成元數據文件」 – 2010-02-26 11:58:59