c#中手动及半自动编译wsdl并调用

首先,手动编译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来调用。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是C#调用wsdl接口的三种方法示例: 1. 使用Visual Studio自动生成代理类: ```csharp // 引用WebService接口 using WebServiceNamespace; // 创建代理类实例 WebServiceClient client = new WebServiceClient(); // 调用接口方法 string result = client.MethodName(parameter); ``` 2. 使用命令行工具wsdl.exe生成代理类: ```shell // 打开命令提示符 // 进入wsdl.exe所在目录 // 执行以下命令生成代理类 wsdl /language:c# /out:生成类的物理路径 /url:WebService接口URL或wsdl文件路径 // 在代码引用生成的代理类 using WebServiceNamespace; // 创建代理类实例 WebServiceClient client = new WebServiceClient(); // 调用接口方法 string result = client.MethodName(parameter); ``` 3. 手动解析wsdl文件: ```csharp // 引用System.Web.Services和System.Web.Services.Description命名空间 using System.Web.Services; using System.Web.Services.Description; using System.Xml; // 创建WebService描述文件的URL string wsdlUrl = "WebService接口URL或wsdl文件路径"; // 创建ServiceDescription对象 ServiceDescription serviceDescription = ServiceDescription.Read(wsdlUrl); // 创建ServiceDescriptionImporter对象 ServiceDescriptionImporter importer = new ServiceDescriptionImporter(); importer.ProtocolName = "Soap"; // 指定协议为Soap // 添加ServiceDescription对象 importer.AddServiceDescription(serviceDescription, null, null); // 创建CodeNamespace对象 CodeNamespace codeNamespace = new CodeNamespace("WebServiceNamespace"); // 创建CodeCompileUnit对象 CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); codeCompileUnit.Namespaces.Add(codeNamespace); // 生成代理类代码 ServiceDescriptionImportWarnings warnings = importer.Import(codeNamespace, codeCompileUnit); // 使用CodeDomProvider编译代理类 CodeDomProvider codeDomProvider = CodeDomProvider.CreateProvider("CSharp"); CompilerParameters compilerParameters = new CompilerParameters(); compilerParameters.GenerateExecutable = false; compilerParameters.GenerateInMemory = true; CompilerResults compilerResults = codeDomProvider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit); // 获取生成的代理类类型 Type proxyType = compilerResults.CompiledAssembly.GetTypes().FirstOrDefault(t => t.Name == "WebServiceClient"); // 创建代理类实例 object proxyInstance = Activator.CreateInstance(proxyType); // 调用接口方法 MethodInfo method = proxyType.GetMethod("MethodName"); string result = (string)method.Invoke(proxyInstance, new object[] { parameter }); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值