首先,手动编译wsdl并调用:
1)使用浏览器打开webService页面。如:http://192.168.1.5:8080/Service.axms?wsdl
2)使用页面另存功能,将当前页面保存为Service.wsdl文件。
3)使用管理员权限打开 VS开发人员命令提示,
输入命令 wsdl Service.wsdl文件全路径 /out: 将要生成的cs文件的全路径
如:wsdl c:\\...\\Service.wsdl /out: c:\\...\\Service.cs
注意:这里的命令中如果路径内容里存在空格,请使用英文双引号将路径包括起来,如:"c:\\xxxx\\Service.wsdl"
该命令将在out所指定的路径下生成.cs文件,.cs文件中的内容就为webService页面源文件的代理类。
4)在c#项目中--添加现有项--该.cs文件--引入System.Web.Serivce,使用该.cs文件中的方法,即可调用webService页面中同名接口,并向WebService传递数据。
优点:编译时不需要设置细节参数,省心啊
缺点:有环境要求
上述步骤3中,如果没有安装VS,可以使用 Microsoft SDKs 下的wsdl.exe工具。Microsoft SDKs 需要安装。
其次,半自动方法编译wsdl并调用:
上代码吧,主要是使用了c#的类调用应用程序wsdl.exe完成。
这种方式下,请使用管理员权限打开VS,否则运行时可能会实现不了功能。
//先验证url
private void CheckUrl(String sUrl)
{
if (sUrl.Equals(""))
{
MessageBox.Show("请输入url!");
return;
}
if (!sUrl.Contains("http://"))
{
MessageBox.Show("请输入正确的url!");
return;
}
if (sUrl.Contains("localhost"))
{
MessageBox.Show("请用明确的IP地址替换localhost。");
return;
}
sUrl = sUrl.Trim();
if (!sUrl.EndsWith("?wsdl") && !sUrl.EndsWith("?WSDL"))
{
sUrl += "?wsdl";
}
UrlText.Text = sUrl;
//UrlText.Refresh();
//MessageBox.Show(sUrl);
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(sUrl);
HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
//成功
//sOutputInfo = "Url连接成功!\r\n";
OutputText.Text += "Url连接成功!\r\n";
}
else
{
//失败
//sOutputInfo = "Url连接成功!\r\n";
OutputText.Text += "Url连接失败!\r\n";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
OutputText.Text += "发生了异常!程序执行失败!\r\n";
return;
}
}
//调用wsdl.exe编译wsdl文件
String sFileName = sUrl.Substring(sUrl.LastIndexOf("/")+1, sUrl.LastIndexOf(".") - sUrl.LastIndexOf("/")-1) ;
try
{
// 1. 使用 WebClient 下载 WSDL 信息。
WebClient web = new WebClient();
Stream stream = web.OpenRead(sUrl);
// 2. 创建和格式化 WSDL 文档。
ServiceDescription description = ServiceDescription.Read(stream);
description.Write(sWorkPath + "\\" + sFileName + ".wsdl");
//sOutputInfo = "wsdl文件写入成功!\r\n";
OutputText.Text += sFileName + ".wsdl" + "文件正在写入...\r\n";
//确定.wsdl文件的存在
if (File.Exists(sWorkPath + "\\" + sFileName + ".wsdl"))
{
OutputText.Text += sFileName + ".wsdl" + "文件写入成功!\r\n";
}
else
{
OutputText.Text += sFileName + ".wsdl" + "文件写入失败!\r\n";
}
//开始编译cs文件
OutputText.Text += "开始编译.cs文件...\r\n";
Process proc = new Process();
proc.StartInfo.FileName = sWorkPath + "\\wsdl.exe"; //"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools\\wsdl.exe";//
proc.StartInfo.Arguments = sWorkPath + "\\" + sFileName + ".wsdl" + " /out:" + sWorkPath + "\\" + sFileName + ".cs";
proc.StartInfo.UseShellExecute = false;
if (proc.Start())
{
//OutputText.Text += proc.ToString() + "\r\n";
OutputText.Text += "wsdl.exe命令调用成功!\r\n";
}
else
{
OutputText.Text += "wsdl.exe命令调用失败!\r\n";
}
//等待命令执行完成
proc.WaitForExit();
//确定.cs文件的存在
if (File.Exists(sWorkPath + "\\" + sFileName + ".cs"))
{
OutputText.Text += sFileName + ".cs" + "文件写入成功!\r\n";
}
else
{
OutputText.Text += sFileName + ".cs" + "文件写入失败!\r\n";
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
OutputText.Text += "发生了异常!程序执行失败!\r\n";
return;
}
}
调用csc.exe将.cs文件编译成.dll,这里使用了与上述同样的方法。
String sFileName = sUrl.Substring(sUrl.LastIndexOf("/") + 1, sUrl.LastIndexOf(".") - sUrl.LastIndexOf("/") - 1);
try
{
//开始编译dll文件
OutputText.Text += "开始编译.dll文件...\r\n";
Process proc = new Process();
proc.StartInfo.FileName = sWorkPath + "\\csc.exe"; //"C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v8.1A\\bin\\NETFX 4.5.1 Tools\\wsdl.exe";//cmd.exe //
//proc.StartInfo.Arguments = sWorkPath + "\\" + "*.cs" + " /out:" + sWorkPath + "\\" + sFileName + ".dll";
proc.StartInfo.Arguments = " /t:library /out:\"" + sWorkPath + "\\" + sFileName + ".dll\" \"" + sWorkPath + "\\*.cs\"";
// /t:library
proc.StartInfo.UseShellExecute = false;
//proc.Start();
//proc.WaitForInputIdle();
if (proc.Start())
{
//OutputText.Text += proc.ToString() + "\r\n";
OutputText.Text += "csc.exe命令调用成功!\r\n";
}
else
{
OutputText.Text += "csc.exe命令调用失败!\r\n";
}
//等待命令执行完成
proc.WaitForExit();
//确定.dll文件的存在
if (File.Exists(sWorkPath + "\\" + sFileName + ".dll"))
{
OutputText.Text += sFileName + ".dll" + "文件写入成功!\r\n";
}
else
{
OutputText.Text += sFileName + ".dll" + "文件写入失败!\r\n";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
OutputText.Text += "发生了异常!程序执行失败!\r\n";
return;
}
}
验证时,可将使用手动方法的第4个步骤,或者加载.dll来调用。