Click here to Skip to main content
11,484,941 members (77,385 online)
Click here to Skip to main content

Dynamic Discovery and Invocation of Web Services

, 26 May 2007 CPOL 99.1K 3K 74
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Ehsan Golkar

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

 
QuestionAlso a way to do this with REST/ODATA services? Pin
LiQuick28-Jul-14 3:53
memberLiQuick28-Jul-14 3:53 
QuestionProblem Pin
Mptre DAsda DSASDA13-Sep-12 14:22
memberMptre DAsda DSASDA13-Sep-12 14:22 
Questionmeaning, Pin
Faisal Hafeez20-Oct-11 20:47
memberFaisal Hafeez20-Oct-11 20:47 
GeneralMy vote of 4 Pin
Member 40308897-Oct-10 1:59
memberMember 40308897-Oct-10 1:59 
GeneralEXCEPTION HAS BEEN THROWN BY THE TARGET OF AN INVOCATION Pin
marco alfaro c24-Sep-10 12:43
membermarco alfaro c24-Sep-10 12:43 
GeneralRe: EXCEPTION HAS BEEN THROWN BY THE TARGET OF AN INVOCATION Pin
Brijesh Shah20-Jul-12 20:49
memberBrijesh Shah20-Jul-12 20:49 
Generalhello Pin
ggnbf7-Jun-10 6:19
memberggnbf7-Jun-10 6:19 
GeneralNumber of output in one WSDL Pin
Angu Thangavel18-May-10 0:13
memberAngu Thangavel18-May-10 0:13 
HI, I have gone through this code and stuck in two places.

1. Method name with space in between. eg. if i have a method name as "Multiply Factor" it doesnt work and throws out an error
An unhandled exception of type 'System.NullReferenceException' occurred in New WSDL.exe Additional information: Object reference not set to an instance of an object.


2. I have number of outputs for a particular WSDL. and i cannot access it them. I always get my type in the output tree

Please try to solve and send the solution.

Thanks
GeneralDynamic Discovery and Invocation of Web services Pin
Member 402965431-May-09 20:40
memberMember 402965431-May-09 20:40 
GeneralRe: Dynamic Discovery and Invocation of Web services Pin
Ehsan Golkar19-Mar-10 19:31
memberEhsan Golkar19-Mar-10 19:31 
GeneralInvalid Operation Exception Pin
TraderJack8-Dec-08 6:47
memberTraderJack8-Dec-08 6:47 
QuestionRe: Invalid Operation Exception Pin
Member 409661821-Dec-08 19:07
memberMember 409661821-Dec-08 19:07 
AnswerRe: Invalid Operation Exception Pin
Member 386121626-Jun-10 14:14
memberMember 386121626-Jun-10 14:14 
GeneralThanks...!!! Pin
Suditha Priyan27-Nov-08 23:26
memberSuditha Priyan27-Nov-08 23:26 
GeneralRe: Thanks...!!! Pin
Ehsan Golkar28-Nov-08 8:24
memberEhsan Golkar28-Nov-08 8:24 
GeneralRe: Thanks...!!! Pin
Suditha Priyan30-Nov-08 20:29
memberSuditha Priyan30-Nov-08 20:29 
Generalthanks Pin
tittatty15-May-08 9:52
membertittatty15-May-08 9:52 
QuestionUnable to Import Binding from namespace Pin
asukuq9-Apr-08 21:42
memberasukuq9-Apr-08 21:42 
GeneralRuntime error... Pin
Sara_4613-Apr-08 10:00
memberSara_4613-Apr-08 10:00 
GeneralRe: Runtime error... Pin
Sara_4613-Apr-08 10:30
memberSara_4613-Apr-08 10:30 
AnswerRe: Runtime error... Pin
Ehsan Golkar3-Apr-08 19:30
memberEhsan Golkar3-Apr-08 19:30 
GeneralRe: Runtime error... Pin
cawoodm11-Jun-08 6:20
membercawoodm11-Jun-08 6:20 
Questionweb serrvice returning user defined object... Pin
quantum558-Nov-07 16:03
memberquantum558-Nov-07 16:03 
QuestionAdd Soap Header? Pin
Gridlock13-Oct-07 19:18
memberGridlock13-Oct-07 19:18 
AnswerRe: Add Soap Header? Pin
Ehsan Golkar21-Oct-07 3:21
memberEhsan Golkar21-Oct-07 3:21 
GeneralWCF Service Pin
Naidooy16-Aug-07 7:04
memberNaidooy16-Aug-07 7:04 
QuestionHow to pass credentials to Web Service Pin
Ronakkumar Patel22-Jun-07 11:44
memberRonakkumar Patel22-Jun-07 11:44 
AnswerRe: How to pass credentials to Web Service Pin
Ehsan Golkar3-Jul-07 10:04
memberEhsan Golkar3-Jul-07 10:04 
GeneralRe: How to pass credentials to Web Service Pin
JackParker11-Jun-08 5:13
memberJackParker11-Jun-08 5:13 
GeneralRe: How to pass credentials to Web Service Pin
cawoodm11-Jun-08 6:17
membercawoodm11-Jun-08 6:17 
NewsStrong-typed dynamic invocation Pin
pascal_recchia29-May-07 0:55
memberpascal_recchia29-May-07 0:55 
GeneralRe: Strong-typed dynamic invocation Pin
Ehsan Golkar11-Jun-07 5:08
memberEhsan Golkar11-Jun-07 5:08 
Generalvery interesting Pin
BlaiseBraye26-May-07 22:37
memberBlaiseBraye26-May-07 22:37 

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.

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