Dynamically Adding a Web Service

Topics: General Discussion Forum, July and December Releases Forum
May 9, 2007 at 5:17 PM

I have a web service that I'd like users of my Service Factory to be able to add into a project along with some supporting code classes. To maintain the flexibility in the service factory and the possibility that the web service may be updated in the future, I would like to limit the hard coding to web service URL. I'd like to add an action that would take a web service URL and then discover the service and add all the necessary files.

I have found an action that lets me open VS's Add Web Reference dialogue box. But I was unable to see a way to enter a URL and a web reference name. I also found an action that allowed the addion of a web reference for a project that was part of the current solution (AddProjectReferenceAction).

Has anyone tried this? Is a custom Action necessary?

May 11, 2007 at 4:47 PM
Certainly you will need a new recipe and at least a custom action along with other built in actions as well.
You can start adding a template for the web project (like the one you already have for the Host project) and then add the required assemblies, that you can also include as part of the web template.
You may also try to use part of the functionality found in the ConnectToWCFService recipe where you have the AddWebReferenceEditor UI editor and UpdateClientConfigAction action that contains the "MetadataDiscovery" function to extract the schema and wsdl information from the specified URI. Then with the "ContractGenerator.CreateWsdlImporter()" you will get the WsdlImporter objtect with all the service information.
The typical sequence for the usage of these APIs will be something like this:

// Get the metadata from service URI
MetadataDiscovery discovery = new MetadataDiscovery(serviceUri);
// Initialize the ContratGenerator class
ContractGenerationOptions options = new ContractGenerationOptions();
ContractGenerator generator = new ContractGenerator(options);
// Get the Importer from the Metadata		
WsdlImporter importer = generator.CreateWsdlImporter(discovery.InspectMetadata());
// force an import to fire validation and get any exception
// gather any error and throw 
// Now the importer is ready to use and get all the serivce available information
I know this is not trivial work but is not rocket science as well, though it requires some experience with GAX and the WCF description API.
Of course that you can always include most of the static parts of your service on a template and just update the dynamic ones with actions and that will require less work than having a fully discoverable web service creation feature.