Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

wcf

c#-4.0

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;
        }
    }
like image 620
Enigma34 Avatar asked Jun 01 '12 19:06

Enigma34


2 Answers

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.

like image 74
Chris Shain Avatar answered Nov 04 '22 16:11

Chris Shain


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();
    }
}
like image 43
Reed Copsey Avatar answered Nov 04 '22 18:11

Reed Copsey