Introduction
This article provides the syntax and shows how to download the metadata of a service. We are getting the service's metadata by adding a service reference into the project using Microsoft Visual Studio and Export metadata of the service.
Before getting started, I would like to explain to you how we are getting the service metadata and when it is stored in our application when adding a service into our project. When we are add a reference of a service to our project, Microsoft Visual Studio creates a proxy and exports the metadata details of the service into your project location. Microsoft Visual Studio stores service metadata details in the path, like Drive:/Project folder/ServiceRefernce/folder contains the servicename you have given.
Download Metadata
Svcutil.exe is used to download the metadata from a running service and save the metadata to local files. To download metadata, you explicitly specify the /t:metadata option. Otherwise, the client code will be generated. For the HTTP and HTTPS URL schemes svcutil.exe will try to retrieve the metadata using the WS-Metadata Exchange and DISCO. For all other URL schemes svcutil.exe will only try the WS-Metadata Exchange.
By default, svcutil.exe uses the bindings defined in the System.ServiceModel.Description.metadataExchangebindings class.
To configure the binding used for the WS-Metadata Exchange you must define a client endpoint in the config that uses the IMetadataExchange contract. This can be defined either in svcutil.exe's config file or in another config file specified using the /svcutilConfig option.
- Syntax: svcutil.exe[/t:metadata] [/serviceName:,serviceConfigName>] [/r:File Path] <`1\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll>*
- Excample: : svcutil.exe/t:metadata/serviceName:,serviceConfigName/r:C:Reference~`1\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll
ServiceConfigName: The config name of a service to download. If this option is used, an executable assembly with an associated config file must be passed as input. Svcutil will search through all the associated config files for the service configuration. If the config files contain any extension type, the assemblies containing these types must either be in the GAC or explicitly provided using the /r option.
A file path can be specified, in other words you can specify the path of where you want to download the metadata to since Visual Studio downloads metadata into your project location. Short form /:r
CollectionType
You can specify the collection type also as in Microsoft Visual Studio we are selection the collection type. It should be the fully-qualified or assembly-qualified name of the type to use as a collection data type when the code is generated from schemas. Short form: /ct
- Syntax: svcutil.exe[/t:metadata] [/serviceName:,serviceConfigName>] [/ct:collectionType] <`1\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll>*
-
- Example: svcutil.exe/t:code/serviceName:,serviceConfigName/ct:System.Collections.ObjectModel.ObservableCollection’1/r:C:Reference~`1\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll
Exporting Metadata
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 as in the following:
- Syntax: svcutil.exe [/t:metadata] [/serviceName:,serviceConfigName>] [/dataContractOnly] <assemblyPath>*
ServiceConfigName: The config name of a service to download. 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 an extension type, the assemblies containing these types must either be in the GAC or explicitly provided using the /r option.
dataContractOnly: Operate on Data Contract type only. Service contracts will not be processed. Short form /dconly . Example: Generates code for data contract types only. Service contract type will not be generated.
AssemblyPath: Adds the specified assembly to the set of assemblies used for resolving type references. If you are exporting or validating a service that uses third-party extensions (Behaviors, Bindings and BindingElements) registered in the config then use this option to locate the extension assemblies that are not in the GAC. (Short Form: /r)
Validating Service
You can also validate a service by a Microsoft Visual Studio Developer Command Prompt. Validation is useful to detect errors in service implementations without hosting the service. You must use the /serviceName option to indicate the service you would like to validate.
- Syntax: svcutil.exe /validate /serviceName:<serviceConfigName> <assemblyPath>*
Parameters
assemblyPath: The path to an assembly containing service types to be validated. The assembly must have an associated config file to provide service configuration. Standard command-line wildcards can be used to provide multiple assemblies.
Optional Parameters
/validate: Validate a service implementation. To validate a service, you must use the /serviceName option to indicate the service you would like to validate. If this option is used, an executable assembly with an associated config file must be passed as input. (Short Form: /v)
/serviceName: The config name of a service to validate. To validate a service this option must be provided. Svcutil will search through the associated config files of all input assemblies for the service configuration. If the associated configuration file contains any extension types then 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 third-party extensions (Behaviors, Bindings and BindingElements) registered in the config then 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 service type to exclude from validation. Short Form: /et
Using the Command Prompt, a serialization format can also be specified. Pre-generated C# serialization code is required for types that can be serialized using the XmlSerializer. It will only generate code for types used by Service Contracts found in the input assemblies. About serialization, I will explain that in the next content.
Examples
Generate client code from a running service or online metadata documents:
Generate client code from local metadata documents:
- svcutil *.wsdl *.xsd /language:C#
Generate Data Contract types in VisualBasic from local schema documents:
- svcutil /dconly *.xsd /language:VB
Generate metadata documents for Service Contracts and associated types in an assembly:
- svcutil /t:metadata http:
Generate metadata documents for a service and all associated Service Contracts and data types in an assembly:
- svcutil myServiceHost.exe /serviceName:myServiceName
Generate metadata documents for data types in an assembly:
- svcutil myServiceHost.exe /dconly
Verify service hosting:
- svcutil /validate /serviceName:myServiceName myServiceHost.exe
Generate serialization types for XmlSerializer types used by any Service Contracts in the assembly:
- svcutil /t:xmlserializer myContractLibrary.exe
Summary
In the preceding we came to understand how to download the metadata information of a service using a Microsoft Visual Studio Development Command Prompt.