Why Does Wcf Return Myobject[] Instead of List<T> Like I Was Expecting

Why does WCF return myObject[] instead of ListT like I was expecting?

You can specify that you want to use a generic list instead of an array by clicking the advanced button when you add a reference, or you can right click on the service reference and choose configure to change it in place.

The reason is that WCF serializes Generic lists as arrays to send across the wire. The configuration is just telling svcutil to create a proxy that converts them back to a generic list for your convenience.

WCF is Returing Array of Objects of MyClass instead of Generic List

I tried mostly all the method which i saw on the Stackoverflow.com and Internet. But i couldn't find one that could have done the job for me.

SO, finally, I found this article WCF the Right Wat by Miguel Castro and I implemented by services with the same architecture as the Miguel Sir is doing. The architecture resolved the problem.

But some programmers might not agree with the Architecture which is being used in that article because Model Classes project is being distributed to the clients as a DLL. Whereas in WCF usually, the Classes are exposed through the Service.

WCF returns ArrayOfKeyValueOfintstringKeyValueOfintstring[] instead of Dictionaryint, string and Array instread of List

Well I found out what was causing the serialization to be so crude.

In my ServiceContract I had the following:

[OperationContract]
List<DataTable> ShowTables();
[OperationContract]
DataTable FetchContacts(string filter = "All");
[OperationContract]
DataTable FetchUsers();
[OperationContract]
DataTable FetchDrops();

After commenting this out and recompiling my WCF Service Library, I found that everything serialized/ deserialized appropriately when generating the proxy.

It seems that when svcutil.exe encounters something that it does not know how to serialize then all bets are off. Quite literally it will ignore your commands/settings and serialize everything as gibberish like ArrayOfKeyValueOfinttringKeyValueOfintstring. So if you receive this error, you should ask yourself if svcutil.exe is able to properly serialize all of what you are returning.

I hope the identification of the source of my issue will help others in the future.

Return ListT as XML Response from WCF Service?

If you want the XML representation of the list return to the client my advice would be to serialize the list and return it as a string to the client.

Here is some code that can get you started. Haven't tested it but I think it might be easy for you to change.

public string GetColorsXmlRepresentation()
{
var colors = new List<Color>();

colors.Add(new Color {Name = "Red", Code = 1});
colors.Add(new Color {Name = "Blue", Code = 2});

return Serialize<List<Color>>(colors);
}

public string Serialize<T>(T instance)
{
var data = new StringBuilder();
var serializer = new DataContractSerializer(instance.GetType());

using (var writer = XmlWriter.Create(data))
{
serializer.WriteObject(writer, instance);
writer.Flush();

return data.ToString();
}
}

Hope it helps

WCF type conversion

By default, WCF proxy generation creates arrays for all collection types. So, you can fix this problem by changing your variable assignment to this

BusinessResponse<Product[]> reponse = client.GetProductList(2);

You can also change the default collection type generated by the proxy by choosing a collection type in the Advanced Settings (Array, List, ArrayList, etc.)

WCF Function Continue after return

What I would do is place the extra work in another function. Then, just before you return from the WCF function, create a new thread for the extra work function and run it.

Liststring and string[] WCF?

It is relevant to how you added the service reference. You can choose the collection type while add the service reference.

Add Service Reference -> Input your Url and with Advanced button you can select System.Collection.Generic.List for collection option.



Related Topics



Leave a reply



Submit