简体   繁体   中英

this operation is not supported in the wcf test client because it uses type system.object[]

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[]"

在此处输入图片说明

im 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;
        }
    }

The problem is that ArrayList can be a list of anything (thus object[] in the error), and the test client can't handle that. While it is perfectly legal in WCF to return an array of arbitrary objects, you should consider returning the actual type that the client is interested in- in this case an array of String should do.

Also, for what it is worth, on modern (>1.1) versions of .NET, ArrayList is usually not used. The generic List<T> is usually more appropriate.

Since you're adding strings ( ProcessName.ToString() - though ToString() is not required as ProcessName is already a string ) to your service, you should define your method to return a List<string> instead of ArrayList .

This can be simplified to:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
class Windows_processes_Service:IWindows_processes_Service
{
    public List<string> GetRunningProcesses()
    {
        return Process.GetProcesses().Select(p => p.ProcessName).ToList();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM