學習C#心得體會,c#調用webservices大概思路以下: web
比較凌亂 c#
1 實例化ws 服務器
2 密碼 網絡
3 引用ws並調用相關方法 學習
4回調方法中取值 ui
例子以下: spa
-------------------------------------------------------- orm
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;
} server
//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();