Service Operation返回错误类型的IEnumerable [英] Service Operation return IEnumerable of wrong type
本文介绍了Service Operation返回错误类型的IEnumerable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的服务操作:
[WebGet]
public IEnumerable< string> GetDisksInfo(string machineName,string userName,string password)
{
List< string> result = new List< string>();
ConnectionOptions oConn = new ConnectionOptions();
oConn.Username = Uri.UnescapeDataString(userName);
oConn.Password = Uri.UnescapeDataString(password);
oConn.Impersonation = ImpersonationLevel.Impersonate;
string strNameSpace = @" \\" + machineName + @" \root \ cimv2" ;;
System.Management.ManagementScope oMs = new System.Management.ManagementScope(strNameSpace,oConn);
//获取固定磁盘统计信息
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery(" select WinSp_DocicalDisk中的FreeSpace,大小,名称)其中DriveType = 3");
//执行查询
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
//获取结果
ManagementObjectCollection oReturnCollection = oSearcher.Get();
//遍历找到的驱动器并写出信息
foreach(管理对象oReturn in oReturnCollection)
{
result.Add(oReturn ["命名"]的ToString()删除(1));
double gb = Math.Round((Convert.ToDouble(oReturn [" Size"]))/ 1073741824.00,2);
result.Add(gb.ToString());
gb = Math.Round((Convert.ToDouble(oReturn [" FreeSpace"]))/ 1073741824.00,2);
result.Add(gb.ToString());
}
返回结果;
}
当我运行此服务操作时,值将返回为类型'element'而不是字符串,如下所示:
<?xml version =" 1.0"编码= QUOT; UTF-8英寸;?>
- < GetDisksInfo xmlns =" http://schemas.microsoft.com/ado/2007/08/dataservices"的xmlns:M =" HTTP://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
< element> C< / element>
< element> 269.4< / element>
< element> 249.18< / element>
< element> D< / element>
< element> 10< / element>
< element> 9.92< / element>
< / GetDisksInfo>
任何想法为什么?在此先感谢!
解决方案
这是原始值集合的XML表示形式,如字符串。
http://www.odata.org/media/30002/OData% 20Atom%20Format.html#collectionofsimpletypedproperties
I have a service operation like this:
[WebGet] public IEnumerable<string> GetDisksInfo(string machineName, string userName, string password) { List<string> result = new List<string>(); ConnectionOptions oConn = new ConnectionOptions(); oConn.Username = Uri.UnescapeDataString(userName); oConn.Password = Uri.UnescapeDataString(password); oConn.Impersonation = ImpersonationLevel.Impersonate; string strNameSpace = @"\\" + machineName + @"\root\cimv2"; System.Management.ManagementScope oMs = new System.Management.ManagementScope(strNameSpace, oConn); //get Fixed disk stats System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType = 3"); //Execute the query ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery); //Get the results ManagementObjectCollection oReturnCollection = oSearcher.Get(); //loop through found drives and write out info foreach (ManagementObject oReturn in oReturnCollection) { result.Add(oReturn["Name"].ToString().Remove(1)); double gb = Math.Round((Convert.ToDouble(oReturn["Size"])) / 1073741824.00, 2); result.Add(gb.ToString()); gb = Math.Round((Convert.ToDouble(oReturn["FreeSpace"])) / 1073741824.00, 2); result.Add(gb.ToString()); } return result; }
when i run this service operation, the values are returned as type 'element' instead of strings, like this:
<?xml version="1.0" encoding="UTF-8"?> -<GetDisksInfo xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> <element>C</element> <element>269.4</element> <element>249.18</element> <element>D</element> <element>10</element> <element>9.92</element> </GetDisksInfo>
Any ideas why? Thanks in advance!
解决方案
This is the XML representation for a collection of primitive values, like strings.
http://www.odata.org/media/30002/OData%20Atom%20Format.html#collectionofsimpletypedproperties
这篇关于Service Operation返回错误类型的IEnumerable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文