8

hi while running my wcf service it gives me error "this operation is not supported in the wcf test client because it uses type system.object[]"

enter image description here

i m trying to retrieve the running process list.

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
    class Windows_processes_Service:IWindows_processes_Service
    {
        ArrayList RunningProcesses_Name = new ArrayList();
        public ArrayList GetRunningProcesses()
        {
            Process[] processlist = Process.GetProcesses();
            foreach (Process nme_processes in processlist)
            {
                RunningProcesses_Name.Add(nme_processes.ProcessName.ToString());
            }
            return RunningProcesses_Name;
        }
    }
4

2 回答 2

3

问题是它ArrayList可以是任何东西的列表(因此object[]在错误中),而测试客户端无法处理它。虽然在 WCF 中返回任意对象数组是完全合法的,但您应该考虑返回客户端感兴趣的实际类型——在这种情况下,String应该使用数组。

此外,对于它的价值,在.NET 的现代(>1.1)版本上,ArrayList通常不使用。泛型List<T>通常更合适。

于 2012-06-01T19:19:53.333 回答
1

由于您正在向您的服务添加字符串(ProcessName.ToString()- 虽然ToString()不是必需的,因为ProcessName它已经是 a string),所以您应该定义您的方法以返回 aList<string>而不是ArrayList.

这可以简化为:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
class Windows_processes_Service:IWindows_processes_Service
{
    public List<string> GetRunningProcesses()
    {
        return Process.GetProcesses().Select(p => p.ProcessName).ToList();
    }
}
于 2012-06-01T19:25:40.087 回答