知识大全 动态IP的Web service调用
Posted 地址
篇首语:一寸光阴一寸金,寸金难买寸光阴。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 动态IP的Web service调用相关的知识,希望对你有一定的参考价值。
动态IP的Web service调用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
系统架构需要使用Web service来降低耦合性 但是 现场布置的时候 WebService的地址是不固定的
可以使用修改exe文件的对应的nfig中的设置来达到目的
nfig文件是一个XML配置文件 其中描述了Web Service的地址 节点@ configuration/system serviceModel/client/endpoint 中的 address 属性的值就是Web Service的地址 例如
创建一个类DynamicURL来进行Web Service地址修改操作
class DynamicURL using System Diagnostics; using System Xml; using System IO; using System Windows Forms; namespace TestWSDL public class DynamicURL static public string LoadURL() string exeConfigFile = Process GetCurrentProcess() MainModule FileName + nfig ; if (File Exists(exeConfigFile)) XmlDocument xmlDoc = new XmlDocument(); xmlDoc Load(exeConfigFile); XmlNode xn = xmlDoc SelectSingleNode(@ configuration/system serviceModel/client/endpoint ); if (xn != null) XmlElement xe = (XmlElement)xn; if (xe != null) return xe GetAttribute( address ); else MessageBox Show(string Format( 找不到文件 exeConfigFile)); return string Empty;
static public long SaveURL(string URL)
string exeConfigFile = Process GetCurrentProcess() MainModule FileName + nfig ; if (File Exists(exeConfigFile)) XmlDocument xmlDoc = new XmlDocument(); xmlDoc Load(exeConfigFile); XmlNode xn = xmlDoc SelectSingleNode(@ configuration/system serviceModel/client/endpoint ); if (xn != null) XmlElement xe = (XmlElement)xn; if (xe != null) xe SetAttribute( address URL); xmlDoc Save(exeConfigFile); return ; else MessageBox Show(string Format( 找不到文件 exeConfigFile)); return ;
为了测试 建议Web Service HelloWorld 里面有方法HelloWorld 返回字符串 Hello World 拷贝HelloWorld成HelloWorld 然后将HelloWorld的返回值设置成 This is the second Hello world Web Service 以便调用的时候 可以区别两个Web Service
在新建的窗体上添加 个按钮和 个TextBox控件
往工程中添加服务引用 ServiceReference VS 会自动添加 上服务描述
Form 为测试窗体
测试窗体 using System; using System Windows Forms;
namespace TestWSDL public partial class Form : Form public Form () InitializeComponent(); this textBoxURL Text = DynamicURL LoadURL();
private void buttonModifyURL_Click(object sender EventArgs e) if ( == DynamicURL SaveURL(this textBoxURL Text)) MessageBox Show( 修改Web Service的URL成功 );
private void buttonCallWebService_Click(object sender EventArgs e) this textBoxResult Text = ; ServiceReference ServiceSoapClient client = new TestWSDL ServiceReference ServiceSoapClient(); this textBoxResult Text = client HelloWorld();
测试步骤
在IIS中为两个Web Services分别设置虚拟目录HelloWorld和HelloWorld
运行程序 点击调用按钮查看调用结果 再将URL地址从 修改成 点击 修改Web Service地址 再调用Web Service查看结果
两次Web Service调用结果分别为 Hello World 和 This is the second Hello world Web Service
cha138/Article/program/net/201311/12414相关参考