views:

627

answers:

1

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.

+2  A: 

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:

alt text

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).

marc_s
thanks for helping! - out of guesswork I had already tried with the add reference way, to my surprise no interface appeared, just a bunch of classes (as per spec I got with the wsdl). Also - with regards to the config file - can I just throw it away and start a new service implementing the service contract and including the data types?
JohnIdol
@JohnIdol: sure, you can toss the config and then recreate just what's needed yourself. Not a bad idea at all :-) Typically, when you Add Service Ref., you should get a bunch of classes, but in one of the file, there ought to be an interface somewhere, which you can base your serviec implementation on.
marc_s
@marc_s: I think I went with 'add web reference' (used the localhost) - and not 'add service reference' when I tried last time. Would that make a difference in terms of the interface not showing up? ;)
JohnIdol
Yes: add Web Reference is used for the deprecated ASP.NET/ASMX webservice stuff. For WCF, by all means use Add Service Reference!
marc_s
Cool! thanks for the pointers and support - will try everything out then report back :)
JohnIdol
@marc_s: about this - is there cmd line arg for importing only the interfaces using svcutil? the source file being created is huge and hugely decorated - it would be nice at least to have a way of splitting up in different files. Also strange things such as request/response objects being wrapped in other objects with decorated names (such as "[className]" + "1")
JohnIdol
I think it's fair to open another question - just to keep concerns separated --> http://stackoverflow.com/questions/2132274/help-with-svcutil-service-contract-and-data-contract-generation (and to put some more rep at stake)
JohnIdol