![]() |
Web Development »
Web Services »
General
Intermediate
License: The Code Project Open License (CPOL)
Dynamic Discovery and Invocation of Web servicesBy Ehsan GolkarHow invocation of web services Without use add/Web reference |
C# 2.0, C# 3.0, .NET, WinXP, Win2003, Vista, ASP.NET, WebForms, VS2005, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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(); } }
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 to Faramaz Safi ( my teacher) to guide this.
Thanks to Homa for translate this article.

General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 26 May 2007 Editor: |
Copyright 2007 by Ehsan Golkar Everything else Copyright © CodeProject, 1999-2009 Web20 | Advertise on the Code Project |