C# 动态调用WebService WebClient.OpenRead(url) 问题

编程菜鸟0604号 2019-04-12 11:58:28
在动态调用某平台提供的服务时, 这两句
WebClient wc = new WebClient();
Stream stream = wc.OpenRead(url + "?WSDL");

第一次调用的时候没有任何问题,第二次调用就会卡在第二句,然后报错:操作超时

两次的url是一样的

请教各位大神!!!
...全文
1015 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
stherix 2019-04-12
  • 打赏
  • 举报
回复
引用 2 楼 编程菜鸟0604号 的回复:
[quote=引用 1 楼 stherix 的回复:] 只要调用一次就可以了 动态生成程序集后 你可以缓存起来,没必要每次调用接口都要去重新请求一次wsdl
那如果对方提供的服务接口发生了变化呢?[/quote] 一般在一定时期内不会发生变化 一般的app都不会运行太久,重新运行就可以重新获取接口了 另外 stream读取完后,记得Close
  • 打赏
  • 举报
回复
引用 1 楼 stherix 的回复:
只要调用一次就可以了 动态生成程序集后 你可以缓存起来,没必要每次调用接口都要去重新请求一次wsdl
那如果对方提供的服务接口发生了变化呢?
stherix 2019-04-12
  • 打赏
  • 举报
回复
只要调用一次就可以了 动态生成程序集后 你可以缓存起来,没必要每次调用接口都要去重新请求一次wsdl
stherix 2019-04-12
  • 打赏
  • 举报
回复
代码反正都差不多,应该不会有问题 超时可能是对方网站的问题吧 你可以尝试下其他的提供webservice的地址 比如有很多提供天气预报的,看连续访问他们会不会也有这个问题
  • 打赏
  • 举报
回复
引用 3 楼 stherix 的回复:
[quote=引用 2 楼 编程菜鸟0604号 的回复:] [quote=引用 1 楼 stherix 的回复:] 只要调用一次就可以了 动态生成程序集后 你可以缓存起来,没必要每次调用接口都要去重新请求一次wsdl
那如果对方提供的服务接口发生了变化呢?[/quote] 一般在一定时期内不会发生变化 一般的app都不会运行太久,重新运行就可以重新获取接口了 另外 stream读取完后,记得Close[/quote] 大神 帮忙看下 代码有什么问题吗?
  public static object InvokeWebService(string url, string classname, string methodname, object[] strSend)
        {
            object[] args = strSend;
            string @namespace = "EnterpriseServerBase.WebService.DynamicWebCalling";
            if ((classname == null) || (classname == ""))
            {
                classname = GetWsClassName(url);
            }
            try
            {
                //获取WSDL   
                WebClient wc = new WebClient();
                //MessageBox.Show(url);
                Stream stream = wc.OpenRead(url + "?WSDL");
                ServiceDescription sd = ServiceDescription.Read(stream);
                stream.Close();
                wc.Dispose();
                ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
                sdi.AddServiceDescription(sd, "", "");
                CodeNamespace cn = new CodeNamespace(@namespace);

                //注意classname一定要赋值获取 
                classname = sd.Services[0].Name;
                
                //生成客户端代理类代码          
                CodeCompileUnit ccu = new CodeCompileUnit();
                ccu.Namespaces.Add(cn);
                sdi.Import(cn, ccu);
                CSharpCodeProvider icc = 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)
            {
                MessageBox.Show("调用WebService发生错误,错误原因:" + ex.Message);
                throw new Exception(ex.InnerException.Message, new Exception(ex.InnerException.StackTrace));
                // return "Error:WebService调用错误!" + ex.Message;
            }
        }

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧