Click here to Skip to main content
6,632,253 members and growing! (18,448 online)
Email Password   helpLost your password?
Web Development » Web Services » General     Intermediate License: The Code Project Open License (CPOL)

Dynamic Discovery and Invocation of Web services

By Ehsan Golkar

How invocation of web services Without use add/Web reference
C# 2.0, C# 3.0, .NET, WinXP, Win2003, Vista, ASP.NET, WebForms, VS2005, Dev
Posted:26 May 2007
Views:37,136
Bookmarked:47 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
19 votes for this article.
Popularity: 5.20 Rating: 4.07 out of 5
1 vote, 5.3%
1
1 vote, 5.3%
2

3
5 votes, 26.3%
4
12 votes, 63.2%
5
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 it has a high speed, it isn't much flexible.

In order to do dynamically by dynamic invocation 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 methodLike another ways, in this way to achieve the flexibility of parameters, the speed is lost.But spending the time is more than the 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 pars 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 with knowing the explanation of the web and using a proxy class.

The goal of produce 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 applied program. The first step is making one instance of service Description importer and it's goal is giving ability to read the information from WSDL and add it to the codeDom.CodeCompilement, after produce 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 that its methods and properties are from web service. All of the methods that presented in proxy class are in the web method property in primary code. These class libraries has 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 of parameterInfo Class. For calling the web method, we need to produce an instance of proxy class that we give it under type of web service information through Activator, creatinstance 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

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

"MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">"http://api.google.com/GoogleSearch.wsdlhttp://www.xmlme.com/WSShakespeare.asmx?WSDL">http://api.google.com/GoogleSearch.wsdl "http://www.xmlme.com/WSShakespeare.asmx?WSDL">http://www.xmlme.com/WSShakespeare.asmx?WSDL

"MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">"http://www.esynaps.com/WebServices/SearchWS.asmx?WSDL">http://www.esynaps.com/WebServices/SearchWS.asmx?WSDL

"MsoNormal" style="MARGIN: 0in 0in 0pt; TEXT-ALIGN: justify">"http://www.webservicex.com/stockquote.asmx?WSDL">http://www.webservicex.com/stockquote.asmx?WSDL

Thanks

Thanks to Faramaz Safi ( my teacher) to guide this.

Thanks to Homa for translate this article.

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


Member

Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Web Services articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralDynamic Discovery and Invocation of Web services PinmemberMember 402965420:40 31 May '09  
GeneralInvalid Operation Exception PinmemberTraderJack6:47 8 Dec '08  
QuestionRe: Invalid Operation Exception PinmemberMember 409661819:07 21 Dec '08  
GeneralThanks...!!! PinmemberSuditha Priyan23:26 27 Nov '08  
GeneralRe: Thanks...!!! PinmemberEhsan Golkar8:24 28 Nov '08  
GeneralRe: Thanks...!!! PinmemberSuditha Priyan20:29 30 Nov '08  
Generalthanks Pinmembertittatty9:52 15 May '08  
QuestionUnable to Import Binding from namespace Pinmemberasukuq21:42 9 Apr '08  
GeneralRuntime error... PinmemberSara_46110:00 3 Apr '08  
GeneralRe: Runtime error... PinmemberSara_46110:30 3 Apr '08  
AnswerRe: Runtime error... PinmemberEhsan Golkar19:30 3 Apr '08  
GeneralRe: Runtime error... Pinmembercawoodm6:20 11 Jun '08  
Questionweb serrvice returning user defined object... Pinmemberquantum5516:03 8 Nov '07  
QuestionAdd Soap Header? PinmemberGridlock19:18 13 Oct '07  
AnswerRe: Add Soap Header? PinmemberEhsan Golkar3:21 21 Oct '07  
GeneralWCF Service PinmemberNaidooy7:04 16 Aug '07  
GeneralHow to pass credentials to Web Service PinmemberRonakkumar Patel11:44 22 Jun '07  
AnswerRe: How to pass credentials to Web Service PinmemberEhsan Golkar10:04 3 Jul '07  
GeneralRe: How to pass credentials to Web Service PinmemberJackParker5:13 11 Jun '08  
GeneralRe: How to pass credentials to Web Service Pinmembercawoodm6:17 11 Jun '08  
NewsStrong-typed dynamic invocation Pinmemberpascal_recchia0:55 29 May '07  
GeneralRe: Strong-typed dynamic invocation PinmemberEhsan Golkar5:08 11 Jun '07  
Generalvery interesting PinmemberBlaiseBraye22:37 26 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 26 May 2007
Editor:
Copyright 2007 by Ehsan Golkar
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project