Click here to Skip to main content
Licence CPOL
First Posted 26 May 2007
Views 68,371
Downloads 1,361
Bookmarked 59 times

Dynamic Discovery and Invocation of Web Services

By | 26 May 2007 | Article
How to invoke web services without using add/Web reference
Screenshot - WsdlReader.jpg

Introduction

Generally when we use web service, at first we should add it in the web reference and then call its methods statically. Despite having a high speed, it isn't very flexible.

In order to do it dynamically by dynamic invocation of web service, it has a very good flexibility. For example, the source of programs will be compiled once.

We can use any web service that we need. We can watch all of the web service's methods and parameters, automatic assignment methods and parameters.

Generally programmers can do any work with this method. Like other methods, in this method to achieve the flexibility of parameters, the speed is lost. But spending time is more than getting benefits.

Using the Code

One of the communication ways with web service is setup. Giving the information about web service will be done through WSDL file. To do this work, we should produce a WebRequest and send it to the WSDL. All activities about the parts are done by WebRequest.

With calling GetRequest method in Web request, WebRequest will produce a stream that will be sent to the read method of service description object. Read method returns a service description object that contains the information about web service in WSDL. We can call web service methods knowing the explanation of the web and using a proxy class.

The goal of producing a proxy class is making similar code that will be put in Reference.cs. When the user adds web reference in Visual Studio and compiles it, he/she can use it through assembly file in the applied program. The first step is making one instance of service Description importer and its goal is giving the ability to read the information from WSDL and add it to the codeDom.CodeCompilement. After producing the service description importer, we call add service. Description method and send it to the service description object that contains information about the web service that we decide to call it. Now we have an assembly file whose methods and properties are from web service. All of the methods that are presented in proxy class are in the web method property in primary code. These class libraries have a method that returns an array of object from methodInfo to the program, that we can choose some methods and fetch their parameters via using parameterInfo class. For calling the web method, we need to produce an instance of proxy class that we give under type of web service information through Activator, createinstance class.

In order to do this work, we should send the list of parameters to the Invoke method which is expected.

private void DynamicInvocation()
{
Uri uri = new Uri(textBox1.Text);
WebRequest webRequest = WebRequest.Create(uri);
System.IO.Stream requestStream = webRequest.GetResponse().GetResponseStream();
// Get a WSDL file describing a service
ServiceDescription sd = ServiceDescription.Read(requestStream);
string sdName = sd.Services[0].Name;
// Initialize a service description servImport
ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
servImport.AddServiceDescription(sd, String.Empty, String.Empty);
servImport.ProtocolName = "Soap";
servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
CodeNamespace nameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(nameSpace);
// Set Warnings
ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit);
if (warnings == 0)
{
StringWriter stringWriter = new StringWriter
	(System.Globalization.CultureInfo.CurrentCulture);
Microsoft.CSharp.CSharpCodeProvider prov = new Microsoft.CSharp.CSharpCodeProvider();
prov.GenerateCodeFromNamespace(nameSpace, stringWriter, new CodeGeneratorOptions());
// Compile the assembly with the appropriate references
string[] assemblyReferences = new string[2] 
	{ "System.Web.Services.dll", "System.Xml.dll" };
CompilerParameters param = new CompilerParameters(assemblyReferences);
param.GenerateExecutable = false;
param.GenerateInMemory = true;
param.TreatWarningsAsErrors = false;
param.WarningLevel = 4;
CompilerResults results = new CompilerResults(new TempFileCollection());
results = prov.CompileAssemblyFromDom(param, codeCompileUnit);
Assembly assembly = results.CompiledAssembly;
service = assembly.GetType(sdName);
methodInfo = service.GetMethods();
foreach (MethodInfo t in methodInfo)
{
if (t.Name == "Discover")
break;
 
treeWsdl.Nodes[0].Nodes.Add(t.Name);
}
treeWsdl.Nodes[0].Expand();
}
}

Wsdl Files

On the internet, you could find too many wsdl files of web services, for example:

Thanks

  • to Faramaz Safi (my teacher) for guiding me with this
Screenshot - poem.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ehsan Golkar



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionmeaning, PinmemberFaisal Hafeez19:47 20 Oct '11  
GeneralMy vote of 4 PinmemberMember 40308890:59 7 Oct '10  
GeneralEXCEPTION HAS BEEN THROWN BY THE TARGET OF AN INVOCATION Pinmembermarco alfaro c11:43 24 Sep '10  
Generalhello Pinmemberggnbf5:19 7 Jun '10  
GeneralNumber of output in one WSDL PinmemberAngu Thangavel23:13 17 May '10  
GeneralDynamic Discovery and Invocation of Web services PinmemberMember 402965419:40 31 May '09  
GeneralRe: Dynamic Discovery and Invocation of Web services PinmemberEhsan Golkar18:31 19 Mar '10  
GeneralInvalid Operation Exception PinmemberTraderJack5:47 8 Dec '08  
QuestionRe: Invalid Operation Exception PinmemberMember 409661818:07 21 Dec '08  
AnswerRe: Invalid Operation Exception PinmemberMember 386121613:14 26 Jun '10  
GeneralThanks...!!! PinmemberSuditha Priyan22:26 27 Nov '08  
GeneralRe: Thanks...!!! PinmemberEhsan Golkar7:24 28 Nov '08  
GeneralRe: Thanks...!!! PinmemberSuditha Priyan19:29 30 Nov '08  
Generalthanks Pinmembertittatty8:52 15 May '08  
QuestionUnable to Import Binding from namespace Pinmemberasukuq20:42 9 Apr '08  
GeneralRuntime error... PinmemberSara_4619:00 3 Apr '08  
GeneralRe: Runtime error... PinmemberSara_4619:30 3 Apr '08  
AnswerRe: Runtime error... PinmemberEhsan Golkar18:30 3 Apr '08  
GeneralRe: Runtime error... Pinmembercawoodm5:20 11 Jun '08  
Questionweb serrvice returning user defined object... Pinmemberquantum5515:03 8 Nov '07  
QuestionAdd Soap Header? PinmemberGridlock18:18 13 Oct '07  
AnswerRe: Add Soap Header? PinmemberEhsan Golkar2:21 21 Oct '07  
GeneralWCF Service PinmemberNaidooy6:04 16 Aug '07  
QuestionHow to pass credentials to Web Service PinmemberRonakkumar Patel10:44 22 Jun '07  
AnswerRe: How to pass credentials to Web Service PinmemberEhsan Golkar9:04 3 Jul '07  
Thanks
 
It is so logical only works publicly available service.
 
For Authentication just add this command after definition webRequest
 
webRequest.Credentials = new NetworkCredential(string username,string Password);

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 26 May 2007
Article Copyright 2007 by Ehsan Golkar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid