using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using System.Net;
using System.IO;
using System.CodeDom;
using Microsoft.CSharp;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Web.Services.Description;
using System.Xml.Serialization;
public class WebServiceHelper
{
private string ClassName = "TIPMService";
private string NameSapceName = "TIPMService";
private string URL = "http://192.168.1.175:8080/TIPM/services/TIPMService?WSDL";
public Boolean InitWebServiceConn(string url)
{
if (url == "")
return false;
ClassName = this.GetClassName(url);
if (ClassName == null)
return false;
URL = url;//这个地址可以写在Config文件里面,这里取出来就行了.在原地址后面加上: ?WSDL
try
{
WebClient client = new WebClient();
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); //添加WSDL文档。
CodeNamespace nmspace = new CodeNamespace(); //命名空间
nmspace.Name = NameSapceName;
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 = "..\\..\\TIPMService.dll";//输出程序集的名称
parameter.OutputAssembly = "TIPMWebServiceRef.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;
}
//根据WebService地址,模似生成一个代理类,,默认保存在bin目录下面
//TextWriter writer = File.CreateText("..\\..\\TIPMWebServiceRef.cs");
TextWriter writer = File.CreateText("TIPMWebServiceRef.cs");
provider.GenerateCodeFromCompileUnit(unit, writer, null);
writer.Flush();
writer.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
public void Test()
{
//TIPMService.TIPMService tipm = new TIPMService.TIPMService();
//string url = tipm.Url;
//TIPMService.routeUser user = tipm.GetUserRegInfo("0011", "");
//Assembly asm = Assembly.LoadFrom("TIPMService.dll");//加载前面生成的程序集
//Type t = asm.GetType("TIPMService.TIPMService");
//object o = Activator.CreateInstance(t);
//MethodInfo method = t.GetMethod("TIPMService");//GetPersons是服务端的方法名称,你想调用服务端的什么方法都可以在这里改,最好封装一下
//string str = (string)method.Invoke(o,null);
//String[] item = (String[])method.Invoke(o, null);
}
public void InvokeWebService(string url, string classname, string methodname, object[] args)
{
Assembly asm = Assembly.LoadFrom("TIPMService.dll");//加载前面生成的程序集
Type t = asm.GetType("TIPMService.TIPMService");
object o = Activator.CreateInstance(t);
MethodInfo method = t.GetMethod("GetPersons");//GetPersons是服务端的方法名称,你想调用服务端的什么方法都可以在这里改,最好封装一下
String[] item = (String[])method.Invoke(o, null);
//注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了,method.Invoke(0,null)这里的null可以传调用方法需要的参数,string[]形式的
foreach (string str in item)
Console.WriteLine(str);
//上面是根据WebService地址,模似生成一个代理类,如果你想看看生成的代码文件是什么样子,可以用以下代码保存下来,默认是保存在bin目录下面
}
//假如URL为"http://192.168.1.175:8080/TIPM/services/TIPMService?WSDL"
//最终的返回值为 TIPMService
private string GetClassName(string url)
{
string[] parts = url.Split('/');
string[] pps = parts[parts.Length - 1].Split('?');
return pps[0];
}
}