Given wsdl + xds type file, how do I create a stub WCF webservice?

五迷三道 提交于 2019-12-09 11:29:41

问题


I understand this is a basic topic but never done this before starting from wsdl.

I am being handed a wsdl file and a bunch of xsd with the types definitions. I don't have a clue if they were created from a WCF service (I guess so because of the split out format) but I do need to create a WCF service that implements the contract.

Question: How do I get the service contract interface?

I know about wsdl.exe and svcutil.exe - but not too familiar with what's what. I guess after that all that's left is implementing the service contract.

Any help appreciated!

P.S. I had another question about this but I tried to put too much stuff in the same question - so let's keep it simple for now.


回答1:


You have two choices:

Option 1: Use the svcutil.exe utility on the command line. It should be installed in your C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin directory (or something similar, depending on machine and OS you have)

Use svcutil -? for the list of all the many parameters. Basically, in its most simple form, use:

svcutil  (name of your service).wsdl (name of your datafile).xsd

and that will create a corresponding (name of your service).cs C# file with the service and data contracts, and a sample config file.

The resulting *.cs file (or *.vb, if you want VB.NET) will contain the service contract (the methods, resulting from the WSDL) and the data contracts (the data portion, coming from the XSD) for your service.

Option 2: Use the "Add Service Reference" dialog in Visual Studio (on the "References" node in your Solution Explorer) and just enter the file name of your WSDL file:

This will create a service reference, which is basically the same as the output from the svcutil.exe utility - plus a few helper classes and files for Visual Studio.

Unfortunately, in both cases, the import will create a horribly overloaded config file which is probably one of the reasons lots of programmers think WCF is awfully complicated - it's really not, but these two import tools just do a horrendously bad job on creating the basic config for you.... don't let that scare you away!

If the Add Service Reference for the WSDL doesn't automatically convert all relevant and necessary XSD files, you might need to add those to your project, and then use something like XSD2Code to convert those to C# (or VB.NET) classes for you.

The wsdl.exe is the deprecated utility to convert a WSDL file into a ASMX (ASP.NET webservice) stub - don't use that anymore, use svcutil.exe or Visual Studio's Add Service Reference for WCF.

As for how to create a proper and minimal WCF config, check out the DotNet Rocks TV Show #122 with Miguel Castro entitled Extreme WCF. Miguel presents a great way to structure your WCF projects, and to create just as much config as is really needed (and thus can be understood a lot better than the generated mess by svcutil).



来源:https://stackoverflow.com/questions/2123430/given-wsdl-xds-type-file-how-do-i-create-a-stub-wcf-webservice

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