C# 调用webservice 出现缺少命名空间为###的服务说明

xiaoben9 2016-04-08 06:02:37
今天在用C#调用webservice的时候出现如下异常:

找不到 JetSun.ServiceBus:EventBusSvc 的定义。缺少命名空间为 JetSun.ServiceBus 的服务说明。
参数名: name


这是我的代码
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Web;
using System.Net;
using System.Reflection;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Web.Services;
using System.Text;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace JetSun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//写日志函数!
public static void WriteLog(string s, string LogAddress)
{
//如果日志文件为空,则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
if (LogAddress == "")
{
LogAddress = Environment.CurrentDirectory + '\\' +
DateTime.Now.Year + '-' +
DateTime.Now.Month + '-' +
DateTime.Now.Day + "_Log.log";
}

//把异常信息输出到文件
StreamWriter sw = new StreamWriter(LogAddress, true);
sw.WriteLine(s);
sw.WriteLine();
sw.Close();
}


//动态调用 webservice 函数
public static object InvokeWebMethod(string _url, string _methodName,
params object[] _params)
{
WriteLog("开始执行 动态调用 webservice 函数...........", "");
WebClient client = new WebClient();
//String url = "http://localhost:3182/Service1.asmx?WSDL";//这个地址可以写在Config文件里面
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 = "yzb"; //这个命名空间可以自己取
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 = "MyTest.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)
{
// 显示编译错误信息
}
Assembly asm = Assembly.LoadFrom("MyTest.dll");//加载前面生成的程序集
//Type t = asm.GetType("yzb.WeatherWS"); //前面的命名空间.类名,类必须是webservice中定义的
Type t = asm.GetType("yzb.EventBusSvc");
//Type t = asm.GetType("JetSun.Iniuservice");
object o = Activator.CreateInstance(t);
MethodInfo method = t.GetMethod(_methodName);//GetPersons是服务端的方法名称,你想调用服务端的什么方法都可以在这里改,最好封装一下
object item = method.Invoke(o, _params); //注:method.Invoke(o, null)返回的是一个Object,如果你服务端返回的是DataSet,这里也是用(DataSet)method.Invoke(o, null)转一下就行了
WriteLog("动态调用 webservice 函数执行完.........", "");
return item;
}

static string showXml()
{
string str = "";
StreamReader objReader = new StreamReader("D:\\test.xml");
string sLine = "";

while (sLine != null)
{
sLine = objReader.ReadLine();
if (sLine != null)
str += sLine.Trim();
}
//Console.WriteLine(str);
return str;
}

private void Form1_Load(object sender, EventArgs e)
{
//Read();
WriteLog("开始执行 主函数 .........", "");
try
{
string _url = "http://svrwcf2.his-test.com/CoreService/EventBus.svc?wsdl";//内网的地址
string _get = Form1.InvokeWebMethod(_url, "XmlPublish",
new object[] {showXml()}).ToString();//我自己的函数调用!
//object o = Form1.InvokeWebMethod(_url, "getRegionProvince", new object[] { });
//string[] s = (string[])o;
//string str="";
WriteLog("函数的返回结果是:.........", "");
WriteLog(_get, "");
MessageBox.Show(_url);
//MessageBox.Show(o.ToString());
//for (int i = 0; i < s.Length; i++)
//{ str +=s[i]; }
//MessageBox.Show(str);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
WriteLog(ex.Message, "");
}
WriteLog("主函数执行完毕.........", "");
}
}
}


但是这个程序在调用外网的webservice和本地的webservice都是没有错的,就是现在用上面的扯关系调用内网的webservice会出错,请各位赐教!谢谢
...全文
3202 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
bole886 2018-03-27
  • 打赏
  • 举报
回复
http://heisetoufa.iteye.com/blog/1684456 DiscoveryClientProtocol dcp = new DiscoveryClientProtocol(); dcp.DiscoverAny(baseWsdlUrl); dcp.ResolveAll(); foreach (object osd in dcp.Documents.Values) { if (osd is ServiceDescription) importer.AddServiceDescription((ServiceDescription)osd, null, null); ; if (osd is XmlSchema) importer.Schemas.Add((XmlSchema)osd); }
mlovelcottage 2017-09-20
  • 打赏
  • 举报
回复
楼主问题解决了吗 同样遇到了这个问题
小K的大师兄 2017-05-26
  • 打赏
  • 举报
回复
我也遇到了这样的问题 请问楼主解决该问题了吗?

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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