您的位置:首页 > 其它

实现WebService 动态 绑定 的方法

2010-08-31 17:05 253 查看
在有调用WebService 的项目里,一般引用 WebService 之后,则其IP 和Port 等信息是一起引用 和绑定了。

等下次需要改变IP或者端口号时,则需要重新引用 比较麻烦。

在工作,总结如下方法:

第一,需要把WebService 类与调它的项目或类放在一个解决方案。(这个一般只适合调用的服务都为自己项目中的东西)

第二,建一个工厂类,用来对所要处理的WebService 的一个处理 项目。

如我在项目的如下 处理:

首先,将要处理的WebService 引用到 这个工厂类所在的项目中,工厂 类的代码一般如下:

public class WSRefFactory
{
private static WSRefFactory _instance = null;
/// <summary>
/// 单例
/// </summary>
/// <returns></returns>
public static WSRefFactory GetInstance()
{
if (_instance == null)
{

_instance = new WSRefFactory();
}
return _instance;
}
/// <summary>
/// 构造函数
/// </summary>
private WSRefFactory() { }
private static string _WSVirtualPath = "SGAIESIMPlatform/WSDL"; //web 服务虚拟目录
/// <summary>
/// web 服务虚拟目录,传入的格式:“http://192.168.1.1/wsdl”
/// </summary>
public string WSVirtualPath
{
get { return _WSVirtualPath; }
set
{
_WSVirtualPath = value;
SetWSVirtualPathAll();
}
}
private static WSWorkFlow.WSWorkFlow _WorkFlowWebSRef = new WebServiceRef.WSWorkFlow.WSWorkFlow();
/// <summary>
/// 数据通信WebService
/// </summary>
public WSWorkFlow.WSWorkFlow WorkFlowWebSRef
{
get { return _WorkFlowWebSRef; }
set { _WorkFlowWebSRef = value; }
}
#region 方法定义
private static void SetWSVirtualPathAll()
{
SetWSVirtualPath(_WorkFlowWebSRef, _WSVirtualPath);
}
private static void SetWebHost(System.Web.Services.Protocols.SoapHttpClientProtocol ws, string host)
{
ws.Url = ws.Url.ToUpper().Replace("LOCALHOST", host);
}
private static void SetWSVirtualPath(System.Web.Services.Protocols.SoapHttpClientProtocol ws,
string virtualPath)
{
string httpStr = "HTTP://";
if (virtualPath.ToUpper().IndexOf(httpStr) >= 0)
ws.Url = virtualPath + ws.Url.Substring(ws.Url.LastIndexOf("/"), ws.Url.Length - ws.Url.LastIndexOf("/"));
else
{
string temp = ws.Url.ToLower().Remove(0, httpStr.Length);
string temp1 = temp.Substring(0, temp.IndexOf("/") + 1);
ws.Url = httpStr + temp1 + virtualPath + temp.Substring(temp.LastIndexOf("/"), temp.Length - temp.LastIndexOf("/"));
}
}
/// <summary>
/// 动态将Web引用绑定到WebService上
/// </summary>
public static void DicoverAll()
{
_WorkFlowWebSRef.Discover();
}
#endregion
}


这样之后,只需要在需要调用WebService 的地方引用此项目 在使用WebService 中的东西之前 先调用 DicoverAll() 方法

就OK了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: