添加WEB引用,引入的WebService地址会直接写入app.config配置文件,这样一来,用户可以用记事本打开配置文件,轻松的查看或修改WebService地址.如何将引入的WEB引用地址,写入自定义.cs文件封装起来,达到隐藏WebService地址的效果,还请赐教.
具体app.config中的引用配置入下
<applicationSettings>
<RedSpider.Properties.Settings>
<setting name="Services_Service" serializeAs="String">
<value>http://user.xxx.com/Ser.asmx </value>
</setting>
</RedSpider.Properties.Settings>
</applicationSettings>
C# code
using System.Configuration; using System.Xml; // 读取:(如果没有在“解决方案资源管理器”的“引用”里添加"System.configuration"请右击"引用"然后选择“System.configuration”、“确定”) ImagePath.Text = ConfigurationManager.AppSettings[ " ImagePath " ].ToString(); // 写(调用的时候直接调用下面的函数就行了。) public static void SetValue( string AppKey, string AppValue) { // System.Configuration.ConfigurationSettings.AppSettings.Set(AppKey,AppValue); XmlDocument xDoc = new XmlDocument(); xDoc.Load(System.Windows.Forms.Application.ExecutablePath + " .config " ); XmlNode xNode; XmlElement xElem1; XmlElement xElem2; xNode = xDoc.SelectSingleNode( " //appSettings " ); xElem1 = (XmlElement)xNode.SelectSingleNode( " //add[@key=' " + AppKey + " '] " ); if ( xElem1 != null ) xElem1.SetAttribute( " value " ,AppValue); else { xElem2 = xDoc.CreateElement( " add " ); xElem2.SetAttribute( " key " ,AppKey); xElem2.SetAttribute( " value " ,AppValue); xNode.AppendChild(xElem2); } xDoc.Save(System.Windows.Forms.Application.ExecutablePath + " .config " ); }
或者你也可以将它加密后写入注册表。直接把地址固定死了写在代理类里就可以了,
你打开的项目目录,应该有一个Web referer之类的目录,你点击进去之后再点开你添加的WEB服务引用目录,里面就有一个referer.cs 是不是这么拼写我记不得太清楚了,反正就一个CS
文件,你把他打开,你可以直接修改他的构造函数,里面有一个关系URL的操作,你直接把写死了URL赋值给this.URL就可以不用App.COnfig的文件里的配置了,其实,你可以把这个CS文件直接复制到项目根目录,改个名称,添加到项目工程里,关于Web服务引用的其它东西都是可以不要的,关键就是在那个cs就够了
我都是直接用这个方法,直接只要这个cs文件里的类,然后访问很多个和这个代理相同WDSL的WebServices,地址是根据参数去数据库取,以达到根据不同的参数,可以向不同的WebService发送请求,