How to generate WCF service with SvcUtil.exe

穿精又带淫゛_ 提交于 2019-11-28 19:54:41

First of all to generate proxy class we need to have our service up and running. So before using this utility make sure that your service is running without any issue.

After verifying the service status go to Visual Studio Command Prompt and run the following command.

svcutil http://localhost/MyService/ClassName.svc /Language=c#
/t:Code /out:ClassNameProxy.cs /config:ClassNameProxy.config

In above command you should replace the service URL ( http://localhost/MyService/Service1.svc) with the URL of your service. Since my services is developed in c#.net so I choose to generate the proxies in the same language by using /Language=c# flag.

/t:code will specify that the out put should be generated as code.

/out:ClassNameProxy.cs /config:ClassNameProxy.config parameters will tell the utility to name the files as specified in these parameter values. After you run the command, tool will generate the output file and config file.

After that just include the ClassNameProxy.cs file into your project and open the ClassNameProxy.config file and copy the entries to your web.config file. You may also need to update the ClassNameProxy.vb file and update the namespace as per the one that you are using in your project. After that you can easily reference the service in your code and call the operations.

Some examples from tool how it can be used

svcutil http://service/metadataEndpoint - Generate client code from a running service or online metadata documents.

svcutil *.wsdl *.xsd /language:C# - Generate client code from local metadata documents.

svcutil /dconly *.xsd /language:VB - Generate Data Contract types in VisualBasic from local schema documents.

svcutil /t:metadata http://service/metadataEndpoint - Download metadata documents from running services

svcutil myAssembly.dll - Generate metadata documents for Service Contracts and associated types in an assembly

svcutil myServiceHost.exe /serviceName:myServiceName - Generate metadata documents for a service, and all associated Service Contracts and data types in an assembly

svcutil myServiceHost.exe /dconly - Generate metadata documents for data types in an assembly

svcutil /validate /serviceName:myServiceName myServiceHost.exe - Verify service hosting

svcutil /t:xmlserializer myContractLibrary.exe - Generate serialization types for XmlSerializer types used by any Service Contracts in the assembly

For anyone still looking for the answer and could not get the 2012 version working, Visual Studio 2015 and .Net 4.5 have updated the svcutil.exe tool to use /serviceContract switch to generate a class that can then be implemented as a .svc service. You may need to provide /syncOnly /wrapped /messageContract switches as well depending on the original XSD's

I think the .NET 4.5 Contract First Tool, integrated into Visual Studio 2012 as a build task, will help you generate the service files you need.

Service contracts often need to be created from existing services. In .NET Framework 4.5, data contract classes can be created automatically from existing services using the contract-first tool. To use the contract-first tool, the XML schema definition file (XSD) must be downloaded locally; the tool cannot import remote data contracts via HTTP.

http://msdn.microsoft.com/en-us/library/hh674270(v=vs.110).aspx

Rajeev Tiwari

Svcutil.exe generates the service client proxy based on the Web Service Description Language (WSDL) from the service.

Open the visual studio command prompt and run the command

svcutil http://localhost/MyService/Service.svc  /Language=c#  /t:Code  /out:C:\Service\ServiceProxy.cs /config:C:\Service\ServiceProxy.config

it generates two files in C:\Service folder, the proxy file and config file,

More details here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!