在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
c# 动态运用webservice并设置超时时间: 默认超时时间为100000 ,也就是100s,如图:
可以通过上图的语句对其进行修改 using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using System.Web.Services.Description; using System.Xml; namespace Sea.Hus.Service.Agent { /// <summary> /// 动态调用WebService代理类 /// </summary> public class WebServiceAgent { private object agent; private Type agentType; private const string CODE_NAMESPACE = "See.WebServiceAgent.Dynamic"; /// <summary> /// 构造 /// </summary> /// <param name="url"></param> public WebServiceAgent(string url) { try { XmlTextReader reader = new XmlTextReader(url + "?wsdl"); //创建和格式化 WSDL 文档 ServiceDescription sd = ServiceDescription.Read(reader); //创建客户端代理代理类 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter(); sdi.AddServiceDescription(sd, null, null); //使用 CodeDom 编译客户端代理类 CodeNamespace cn = new CodeNamespace(); CodeCompileUnit ccu = new CodeCompileUnit(); ccu.Namespaces.Add(cn); sdi.Import(cn, ccu); Microsoft.CSharp.CSharpCodeProvider icc = new Microsoft.CSharp.CSharpCodeProvider(); CompilerParameters cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.XML.dll"); cp.ReferencedAssemblies.Add("System.Web.Services.dll"); cp.ReferencedAssemblies.Add("System.Data.dll"); CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu); agentType = cr.CompiledAssembly.GetTypes()[0]; agent = Activator.CreateInstance(agentType); //设置超时时间 ((System.Web.Services.Protocols.WebClientProtocol)(agent)).Timeout = 200000;//毫秒 } catch (System.Exception ex) {throw; } } ///<summary> ///调用指定的方法 ///</summary> ///<param name="methodName">方法名,大小写敏感</param> ///<param name="args">参数,按照参数顺序赋值</param> ///<returns>Web服务的返回值</returns> public object Invoke(string methodName, params object[] args) { MethodInfo mi = agentType.GetMethod(methodName); return this.Invoke(mi, args); } ///<summary> ///调用指定方法 ///</summary> ///<param name="method">方法信息</param> ///<param name="args">参数,按照参数顺序赋值</param> ///<returns>Web服务的返回值</returns> public object Invoke(MethodInfo method, params object[] args) { return method.Invoke(agent, args); } } }
|
请发表评论