- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
- using System.CodeDom.Compiler;
- using System.Reflection;
- using System.Web.Services.Description;
- using System.Xml.Serialization;
- using System.CodeDom;
-
- namespace CN100.Member.Utility
- {
- public class WebServiceHelper
- {
- private static WebServiceHelper webService = null;
-
- public static WebServiceHelper Instance(string webServiceUrl, string NamSpace)
- {
- if (webService == null)
- {
- webService = new WebServiceHelper(webServiceUrl, NamSpace);
- }
- return webService;
- }
-
- private WebServiceHelper()
- {
-
- }
-
-
-
-
- public string ServerUrl
- {
- get;
- set;
- }
-
-
-
-
- public string NameSpace
- {
- get;
- set;
- }
-
- private WebServiceHelper(string webServiceUrl, string namSpace)
- {
- ServerUrl = webServiceUrl;
- NameSpace = namSpace;
- }
-
-
-
-
-
- public bool GenerateWebService()
- {
- WebClient client = new WebClient();
- String url = ServerUrl + "?WSDL";
- Stream stream = client.OpenRead(url);
- ServiceDescription description = ServiceDescription.Read(stream);
- ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
- importer.ProtocolName = "Soap";
- importer.Style = ServiceDescriptionImportStyle.Client;
- importer.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties | CodeGenerationOptions.GenerateNewAsync;
- importer.AddServiceDescription(description, null, null);
- CodeNamespace nmspace = new CodeNamespace();
- nmspace.Name = NameSpace;
- CodeCompileUnit unit = new CodeCompileUnit();
- unit.Namespaces.Add(nmspace);
-
- ServiceDescriptionImportWarnings warning = importer.Import(nmspace, unit);
- CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
-
- CompilerParameters parameter = new CompilerParameters();
- parameter.GenerateExecutable = false;
- parameter.OutputAssembly = NameSpace + ".dll";
- parameter.ReferencedAssemblies.Add("System.dll");
- parameter.ReferencedAssemblies.Add("System.XML.dll");
- parameter.ReferencedAssemblies.Add("System.Web.Services.dll");
- parameter.ReferencedAssemblies.Add("System.Data.dll");
-
- CompilerResults result = provider.CompileAssemblyFromDom(parameter, unit);
- if (result.Errors.HasErrors)
- {
-
- return false;
- }
- return true;
- }
-
- private Assembly LoadAssembly(string nameSpace)
- {
- Assembly asm = null;
- try
- {
- asm=Assembly.LoadFrom(nameSpace + ".dll");
- }
- catch (FileNotFoundException ex)
- {
- if (GenerateWebService())
- {
- asm=Assembly.LoadFrom(nameSpace + ".dll");
- }
- }
- catch (Exception e)
- {
- throw e;
- }
- return asm;
- }
-
-
-
-
-
-
-
- public void ExcuteMethod(string methodName,string nameSpace,object[] args)
- {
- Assembly asm = LoadAssembly(nameSpace);
- Type t = asm.GetType(nameSpace);
- object o = Activator.CreateInstance(t);
- MethodInfo method = t.GetMethod(methodName);
- method.Invoke(o, args);
- }
-
- public void ExcuteMethod(string methodName, object[] args)
- {
- string nameSpace = NameSpace;
- ExcuteMethod(methodName, nameSpace, args);
- }
-
-
-
-
-
-
-
-
-
- public T ExcuteMethod<T>(string methodName, string nameSpace, object[] args)
- {
- Assembly asm = LoadAssembly(nameSpace);
- Type t = asm.GetType(nameSpace);
- object o = Activator.CreateInstance(t);
- MethodInfo method = t.GetMethod(methodName);
- T result = (T)method.Invoke(o, args);
- return result;
- }
-
- public T ExcuteMethod<T>(string methodName, object[] args)
- {
- string nameSpace=NameSpace;
- return ExcuteMethod<T>(methodName, nameSpace, args);
- }
-
- }
- }