Hi Friends this time I want to share , How to Invoke Webservice Dyanamicly and get Response
I Created a Class and Create a method to get all the webmethods in the given serivce Url
I Created a Class and Create a method to get all the webmethods in the given serivce Url
using System.Net; using System; using System.Web.Services.Description; using System.CodeDom; using System.IO; using System.CodeDom.Compiler; using System.Reflection; using System.Xml.Serialization; public class DyanamicProxyGeneration { #region Members List_serviceMethods; Uri uri; String Url; WebRequest webRequest; Stream requestStream; ServiceDescription sd; ServiceDescriptionImporter servImport; MethodInfo[] methodInfo; CodeCompileUnit codeCompileUnit; StringWriter stringWriter; CodeNamespace nameSpace; Type service; CSharpCodeProvider prov; Assembly assembly; CompilerResults results; CompilerParameters param; String[] assemblyReferences; #endregion public void DynamicInvocation(string Url) { try { uri = new Uri(Url + "?WSDL"); webRequest = WebRequest.Create(uri); requestStream = webRequest.GetResponse().GetResponseStream(); // Get a WSDL file describing a service sd = ServiceDescription.Read(requestStream); string sdName = sd.Services[0].Name; // Initialize a service description servImport servImport = new ServiceDescriptionImporter(); servImport.AddServiceDescription(sd, String.Empty, String.Empty); servImport.ProtocolName = "Soap"; servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties; nameSpace = new CodeNamespace(); codeCompileUnit = new CodeCompileUnit(); codeCompileUnit.Namespaces.Add(nameSpace); // Set Warnings ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit); if (warnings == 0) { stringWriter = new StringWriter(System.Globalization.CultureInfo.CurrentCulture); prov = new Microsoft.CSharp.CSharpCodeProvider(); prov.GenerateCodeFromNamespace(nameSpace, stringWriter, new CodeGeneratorOptions()); // Compile the assembly with the appropriate references assemblyReferences = new string[2] { "System.Web.Services.dll", "System.Xml.dll" }; param = new CompilerParameters(assemblyReferences); param.GenerateExecutable = false; param.GenerateInMemory = true; param.TreatWarningsAsErrors = false; param.WarningLevel = 4; results = new CompilerResults(new TempFileCollection()); results = prov.CompileAssemblyFromDom(param, codeCompileUnit); assembly = results.CompiledAssembly; service = assembly.GetType(sdName); methodInfo = service.GetMethods(); foreach (MethodInfo t in methodInfo) { if (t.Name == "Discover") break; _serviceMethods.Add(t.Name); } } } catch (Exception ex) { } } } public object GetResponse(MethodInfo[] methodInfoList) { Object response = null; ParameterInfo [] paramters; foreach (MethodInfo t in methodInfo) { //Invoke Method paramters = new ParameterInfo []{}; paramters = t.GetParameters(); object[] param1 = paramters; Object obj = Activator.CreateInstance(service); response = t.Invoke(obj, param1); break; } if (response != null) { return response; } else return null; }
It is very good blog and useful for students and developers.Thanks.
ReplyDeleteDot Net Training in OMR