学习C#心得体会,c#调用webservices大概思路如下:
比较凌乱
1 实例化ws
2 密码
3 引用ws并调用相关方法
4回调方法中取值
例子如下:
--------------------------------------------------------
namespace bms.common
{
/// <summary>
/// Web服务(单例类)
/// </summary>
public class WebServiceSingleton
{
/// <summary>
/// Url服务器地址
/// </summary>
private static string Url_Http = "";
/// <summary>
/// Url服务器文件路径 (另外ServiceName表示服务名)
/// </summary>
private static string Url_Path = "/baseproject/ws/";
/// <summary>
/// 获取Web服务器地址
/// </summary>
/// <returns></returns>
public static string GetUrl()
{
string URL = "";
if (string.IsNullOrWhiteSpace(Url_Http))
{
Url_Http = Constant.readValue("SyncSeting", "Http");
if(string.IsNullOrWhiteSpace(Url_Http))
{
throw new Exception("服务器地址不可为空,请先设置服务器地址!");
}
//URL 格式:IP+端口+文件夹地址+服务名 http://192.168.1.110:8080/baseproject/ws/proDpInfo.ws?wsdl
}
URL = Url_Http + Url_Path;
return URL;
}
//public static void RefreshWebSetting()
//{
// Url_Http = "";
// Url_Path = "";
//}
private static NetworkCredential myCred = null;
/// <summary>
/// 服务器网络凭证
/// </summary>
/// <returns></returns>
private static NetworkCredential GetCredential()
{
if (myCred == null)
{
string loginUser = Constant.readValue("SyncSeting", "User"); ;//用户名
string loginPSW = Constant.readValue("SyncSeting", "Passwrod");//密码
//string loginHost = Properties.Settings.Default.HostName;//主机名,可以是IP地址,也可以服务器名称
myCred = new NetworkCredential(loginUser, loginPSW);
//NetworkCredential myCred = new NetworkCredential("username", "123456", "yourip");//"username", "123456", "yourservername"
}
return myCred;
}
private static string wsProjectInfo_ServiceName = "projectInfo.ws?wsdl";
private static IProjectInfoService wsProjectInfo = null;
/// <summary>
/// 获取 项目信息Web服务
/// </summary>
/// <returns></returns>
public static IProjectInfoService GetProjectInfoService()
{
if (wsProjectInfo == null)
{
wsProjectInfo = new IProjectInfoService();
wsProjectInfo.Credentials = GetCredential();
wsProjectInfo.Url = GetUrl() + wsProjectInfo_ServiceName;
}
return wsProjectInfo;
}
private static string wsProDpInfo_ServiceName = "proDpInfo.ws?wsdl";
private static IProDpInfoService wsProDpInfo = null;
/// <summary>
/// 获取 项目部Web服务
/// </summary>
/// <returns></returns>
public static IProDpInfoService GetProDpInfoService()
{
if (wsProDpInfo == null)
{
wsProDpInfo = new IProDpInfoService();
wsProDpInfo.Credentials = GetCredential();
wsProDpInfo.Url = GetUrl() + wsProDpInfo_ServiceName;
}
return wsProDpInfo;
}
//加凭证
private static string wsBaseMessage_ServiceName = "basemessage.ws?wsd";
private static IBaseMessageService wsBaseMessage = null;
public static IBaseMessageService GetBaseMessage()
{
if (wsBaseMessage == null)
{
wsBaseMessage = new IBaseMessageService();
wsBaseMessage.Credentials = GetCredential();
wsBaseMessage.Url = GetUrl() + wsBaseMessage_ServiceName;
}
return wsBaseMessage;
}
}
}
------------------------------------------------------------
namespace bms
{
class BaseMessage
{
bmsDataEntities data = null;
IBaseMessageService wsBaseMessage = null;
/// <summary>
/// 私有初始化数据
/// </summary>
private BaseMessage()
{
data = Constant.DataEntity;
wsBaseMessage = WebServiceSingleton.GetBaseMessage();
}
private static BaseMessage m_BaseMessage = null;
/// <summary>
/// 单例类
/// </summary>
/// <returns></returns>
public static BaseMessage GetInstance()
{
if (m_BaseMessage == null)
{
m_BaseMessage = new BaseMessage();
}
return m_BaseMessage;
}
/// <summary>
/// 下载数据回调事件
/// </summary>
public void BaseMessageDown()
{
wsBaseMessage.updateBaseMessageCompleted += new updateBaseMessageCompletedEventHandler(wsWorkType_updateBaseMessageCompleted);
wsBaseMessage.updateBaseMessageAsync();
}
void wsWorkType_updateBaseMessageCompleted(object sender, updateBaseMessageCompletedEventArgs e)
{
wsBaseMessage.updateBaseMessageCompleted += new updateBaseMessageCompletedEventHandler(wsWorkType_updateBaseMessageCompleted);
if (e.Error != null)
{
RadMessageBox.Show(e.Error.ToString(), "错误", System.Windows.Forms.MessageBoxButtons.OK, RadMessageIcon.Error);
return;
}
//工程施工类型
List<TWorkType> worktypelist = e.Result.listTWorkTypes.ToList();
for (int i = 0; i < worktypelist.Count; i++)
{
WorkType wp = new WorkType();
wp.Build_ID = worktypelist[i].wTID;
wp.Remarks = worktypelist[i].remarks;
wp.TypeName = worktypelist[i].typeName;
data.AddToWorkType(wp);
}
data.SaveChanges();
//工程工种
List<TWorkSort> worksortlist = e.Result.listTWorkSorts.ToList();
for (int j = 0; j < worksortlist.Count; j++)
{
WorkSort ws = new WorkSort();
ws.WorkSort_ID = worksortlist[j].workSortID;
ws.TypeName = worksortlist[j].typeName;
ws.TypeUnit = worksortlist[j].typeUnit;
ws.Remarks = worksortlist[j].remarks;
data.AddToWorkSort(ws);
}
data.SaveChanges();
//工程等级下载
List<THonorLevel> honorlevellist = e.Result.listTHonorLevels.ToList();
for (int k = 0; k < honorlevellist.Count; k++)
{
HonorLevel hl = new HonorLevel();
hl.Honor_ID = honorlevellist[k].honorID;
hl.LevelName = honorlevellist[k].levelName;
hl.LevelKey = honorlevellist[k].levelKey;
hl.Remarks = honorlevellist[k].remarks;
data.AddToHonorLevel(hl);
}
data.SaveChanges();
MessageBox.Show("更新成功!!");
}
/// <summary>
/// 下载worktype 数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
//void wsWorkType_updateWorkTypeCompleted(object sender, updateWorkTypeCompletedEventArgs e)
//{
// wsWorkType.updateWorkTypeCompleted -= new updateWorkTypeCompletedEventHandler(wsWorkType_updateWorkTypeCompleted);
// if (e.Error != null)
// {
// RadMessageBox.Show(e.Error.ToString(), "错误", System.Windows.Forms.MessageBoxButtons.OK, RadMessageIcon.Error);
// return;
// }
// List<TWorkType> worktypelist = e.Result.ToList();
// //工程类型
// worktypelist.FindAll(o=>{
// WorkType worktype = new WorkType();
// worktype.Build_ID = o.wTID;
// worktype.Remarks = o.remarks;
// worktype.TypeName = o.typeName;
// return true;
// });
//THonorLevel thonorlevel = e.Result;
//TWorkSort tworksort = e.Result;
//}
}
}
---------------------------------------
m_WorkType = BaseMessage.GetInstance();
m_WorkType.BaseMessageDown();