問題一:webservice服務中若是保持Sessionhtml
調用Sessionweb
對於Web Service,每一個方法的調用都會啓動一個Session,能夠用下面的方法來使多個調用在同一個Session裏cookie
CWSSyscfg cwsCfg = new CWSSyscfg();session
cwsCfg.CookieContainer = new System.Net.CookieContainer();ide
CWSSyscfg是一個Web Service類,Web Service的給代理類設置CookieContainer屬性,只要多個代理的CookieContainer屬性是相同的值,ui
則對這些Web Service的調用在同一個Session。能夠用單例模式來實現。url
通常的webservice session 是由cookie 保持的
在兩次訪問之間保持一個cookie 上下文 就能夠保持一個session
因此你須要把前一次返回的結果的http頭中 cookie 的全部內容 做爲下一次發送你request http頭的cookie內容就能夠保持session上下文了 spa
參考文章:http://www.cnblogs.com/RascallySnake/archive/2010/09/10/1823036.html代理
http://www.cnblogs.com/Jan_Dai/archive/2011/04/01/2001742.htmlcode
本人的Demo:http://files.cnblogs.com/zfanlong1314/WebService%E4%B8%ADSession%E7%9A%84%E4%BD%BF%E7%94%A8.zip
問題二:動態調用WebService時如何保持Session
核心代碼:
//設置CookieContainer 1987raymond添加 PropertyInfo property = t.GetProperty("CookieContainer"); property.SetValue(obj, container, null);
支持webservice服務保持session狀態的動態代理服務類。
public object InvokeWebservice(string url, string @namespace, string classname, string methodname, object[] args,CookieContainer container) { try { System.Net.WebClient wc = new System.Net.WebClient(); System.IO.Stream stream = wc.OpenRead(url + "?WSDL"); System.Web.Services.Description.ServiceDescription sd = System.Web.Services.Description.ServiceDescription.Read(stream); System.Web.Services.Description.ServiceDescriptionImporter sdi = new System.Web.Services.Description.ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, "", ""); System.CodeDom.CodeNamespace cn = new System.CodeDom.CodeNamespace(@namespace); System.CodeDom.CodeCompileUnit ccu = new System.CodeDom.CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); Microsoft.CSharp.CSharpCodeProvider csc = new Microsoft.CSharp.CSharpCodeProvider(); System.CodeDom.Compiler.ICodeCompiler icc = csc.CreateCompiler(); System.CodeDom.Compiler.CompilerParameters cplist = new System.CodeDom.Compiler.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"); System.CodeDom.Compiler.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); //設置CookieContainer 1987raymond添加 PropertyInfo property = t.GetProperty("CookieContainer"); property.SetValue(obj, container, null); System.Reflection.MethodInfo mi = t.GetMethod(methodname); return mi.Invoke(obj, args); } catch (Exception ex) { throw ex; } } }