FAQ:
1、最近做的項目需要和Java做的系統的webservice交互數據.一上來直接添加web引用,一路OK。但是地址在發布時要變的,以為也簡單,其實不然,改了app.config里的配置,調用代理類的方法,結果一直不變。郁悶了。google了一把解決了。原來不能直接實例化代理類。需要如下處理:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GB_PRINT.com.goodbabygroup.factorybarcode_sayHi;
using GB_PRINT.com.goodbabygroup.factorybarcode;
using GB_PRINT.com.goodbabygroup.factorybarcode_Login;
namespace GB_PRINT
{
/// <summary>
/// HelloWorld動態代理類
/// </summary>
public class wsHelloWorldProxy : HelloWorld
{
public wsHelloWorldProxy(string url)
{
this.Url = "http://" + url + "/ws/webservice/helloWorld";
}
}
/// <summary>
/// StoreProcedure動態代理類
/// </summary>
public class wsStoreProcProxy : StoreProcedure
{
public wsStoreProcProxy(string url)
{
this.Url = "http://" + url + "/ws/storeProcedure";
}
}
/// <summary>
/// StoreProcedure動態代理類
/// </summary>
public class wsLoginProxy : CsLoginService
{
public wsLoginProxy(string url)
{
this.Url = "http://" + url + "/ws/csLogin";
}
}
}
2、Json數據的解析。
string result =@"{"Own_Gp_Num_Id":"13633","Empe_Name":"推車一廠","Message":"登錄成功","Empe_Id":"500001","ORG_NO_Value":"兒童用品有限公司推車一廠-打印中心","SubEntity_Name":"兒童用品有限公司推車一廠","ENTITY_NAME":"兒童用品有限公司推車一廠","CLIENT_MAC":"cb159d4b7eadf88166bc0e6ff23f5314","popedomInfo":[ ],"Cort_Num_Id":"9586","Code":"0","ORG_NO_ID":"6"}";
popedomInfo":[ ],"在Json里面代碼個數組。我剛開始使用第一個版本的類去接,老是報什么"System.String"什么轉換錯誤,后來發現js里面[]代表一個數組。修改后成功。
第一版的:
/// <summary>
/// 用戶實體DTO
/// </summary>
public class LocalUserEntity
{
public string Own_Gp_Num_Id
{
get;
set;
}
public string Empe_Name
{
get;
set;
}
public string Empe_Id
{
get;
set;
}
public string ORG_NO_Value
{
get;
set;
}
public string SubEntity_Name
{
get;
set;
}
public string ENTITY_NAME
{
get;
set;
}
public string CLIENT_MAC
{
get;
set;
}
public string popedomInfo
{
get;
set;
}
public long Cort_Num_Id
{
get;
set;
}
public long ORG_NO_ID
{
get;
set;
}
public string Code
{
get;
set;
}
public string Message
{
get;
set;
}
}
第二版:
/// <summary>
/// 用戶實體DTO
/// </summary>
public class LocalUserEntity
{
public string Own_Gp_Num_Id
{
get;
set;
}
public string Empe_Name
{
get;
set;
}
public string Empe_Id
{
get;
set;
}
public string ORG_NO_Value
{
get;
set;
}
public string SubEntity_Name
{
get;
set;
}
public string ENTITY_NAME
{
get;
set;
}
public string CLIENT_MAC
{
get;
set;
}
public int [] popedomInfo
{
get;
set;
}
public long Cort_Num_Id
{
get;
set;
}
public long ORG_NO_ID
{
get;
set;
}
public string Code
{
get;
set;
}
public string Message
{
get;
set;
}
}
調用:
LocalUserEntity result = null;
try
{
jsonData_str = Program.csLogin.csLogin(vParam);
result = JsonConvert.DeserializeObject<LocalUserEntity>(jsonData_str);
}
catch (Exception ex)
{
throw ex;
}
//假裝授權
result.popedomInfo = new int[] { 1,2,3 };
if (result.Message.Equals("登錄成功"))
{
if (result.popedomInfo.Length > 0)
{
foreach(int popedomValue in result.popedomInfo)
{
ClientPopedom value = (ClientPopedom)Enum.Parse(typeof(ClientPopedom), popedomValue.ToString());
LocalPopedom.m_LocalPopedom.clientPopedom.Add(value);
}
}
}
記錄下來備忘,也給遇到同樣問題的朋友一個很遜的方案。