1.動態調用WebService?
解決方案:添加動態調用WebService類WebServiceHelper
;代碼如下:
WebServiceHelper
using System;

 

using System.Collections.Generic;

 

using System.Web;

 

using System.Net;

 

using System.Web.Services.Description;

 

using System.Text;

 

using System.IO;

 

using System.CodeDom;

 

using Microsoft.CSharp;

 

using System.CodeDom.Compiler;

 

/// <summary>

 

///WebServiceHelper 的摘要說明

 

/// </summary>

 

public class WebServiceHelper

 

{

 

public WebServiceHelper()

 

{

 

//

 

//TODO: 在此處添加建構函式邏輯

 

//

 

}

 

#region InvokeWebService

 

//動態調用web服務

 

public static object InvokeWebService(string url, string methodname, object[] args)

 

{

 

return WebServiceHelper.InvokeWebService(url, null, methodname, args);

 

}




public static object InvokeWebService(string url, string classname, string methodname, object[] args)

 

{

 

string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";

 

if ((classname == null) || (classname == ""))

 

{

 

classname = WebServiceHelper.GetWsClassName(url);

 

}




try

 

{

 

//獲取WSDL

 

WebClient wc = new WebClient();

 

Stream stream = wc.OpenRead(url + "?WSDL");

 

ServiceDescription sd = ServiceDescription.Read(stream);

 

ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();

 

sdi.AddServiceDescription(sd, "", "");

 

CodeNamespace cn = new CodeNamespace(@namespace);




//生成用戶端代理類代碼

 

CodeCompileUnit ccu = new CodeCompileUnit();

 

ccu.Namespaces.Add(cn);

 

sdi.Import(cn, ccu);

 

CSharpCodeProvider csc = new CSharpCodeProvider();

 

ICodeCompiler icc = csc.CreateCompiler();




//設定編譯參數

 

CompilerParameters cplist = new CompilerParameters();

 

cplist.GenerateExecutable = false;

 

cplist.GenerateInMemory = true;

 

cplist.ReferencedAssemblies.Add("System.dll");

 

cplist.ReferencedAssemblies.Add("System.XML.dll");

 

cplist.ReferencedAssemblies.Add("System.Web.Services.dll");

 

cplist.ReferencedAssemblies.Add("System.Data.dll");




//編譯代理類

 

CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);

 

if (true == cr.Errors.HasErrors)

 

{

 

System.Text.StringBuilder sb = new System.Text.StringBuilder();

 

foreach (System.CodeDom.Compiler.CompilerError ce in cr.Errors)

 

{

 

sb.Append(ce.ToString());

 

sb.Append(System.Environment.NewLine);

 

}

 

throw new Exception(sb.ToString());

 

}




//生成代理實例,並調用方法

 

System.Reflection.Assembly assembly = cr.CompiledAssembly;

 

Type t = assembly.GetType(@namespace + "." + classname, true, true);

 

object obj = Activator.CreateInstance(t);

 

System.Reflection.MethodInfo mi = t.GetMethod(methodname);




return mi.Invoke(obj, args);

 

}

 

catch (Exception ex)

 

{

 

throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));

 

}

 

}




private static string GetWsClassName(string wsUrl)

 

{

 

string[] parts = wsUrl.Split('/');

 

string[] pps = parts[parts.Length - 1].Split('.');




return pps[0];

 

}

 

#endregion

 

}




2.調用WebService時,傳入參數為Entity[](即實體集合等複雜物件),如何傳入以及接收參數?
解決方案:第一步序列化物件
第二步:動態調用WebService
序列化物件
#region 序列化在記憶體中

 

XmlSerializer xs = new XmlSerializer(typeof(Entity[]));

 

MemoryStream mstream = new MemoryStream();

 

XmlTextWriter writer = new XmlTextWriter(mstream, Encoding.Default);

 

xs.Serialize(mstream, en);

 

writer.Close();

 

string str = Encoding.UTF8.GetString(mstream.ToArray());

 

#endregion

 

#region 寫入檔再讀取出字串

 

//FileStream fs = new FileStream("c:\\aaa.txt", FileMode.OpenOrCreate);

 

//xs = new XmlSerializer(typeof(Entity[]));

 

//xs.Serialize(fs, en);

 

//fs.Close();




//fs = new FileStream("c:\\aaa.txt", FileMode.Open, FileAccess.Read);

 

//StreamReader reader = new StreamReader(fs, Encoding.Default);

 

//str = reader.ReadToEnd();

 

//reader.Close();

 

//reader.Dispose();

 

//fs.Close();

 

//fs.Dispose();

 

#endregion




代碼
string url = "HTTP://localhost:4494/SMWebService/WebService_S.asmx";

 

object[] args = new object[2];

 

args[0] = mapname;

 

args[1] = str;

 

object result = WebServiceHelper.InvokeWebService(url, "AddEntity", args);



第三步:WebService中的處理過程(反序列化)

 

代碼
[WebMethod]

 

public bool AddEntity(string mapname,string strentites)

 

{

 

Stream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(strentites));

 

XmlSerializer xml = new XmlSerializer(typeof(Entity[]));

Entity[] entities = (Entity[])xml.Deserialize(stream);

}
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()