I just ran into a weird situation and I'm documenting it so I think I'm crazy later on. I have the following method in a WCF service which basically returns a List<> of data contracts.
public GetRuleSetsByCategoryIdResponse GetRuleSetsByCategoryId(GetRuleSetsByCategoryIdRequest request)
{
RuleSetLogic logic = new RuleSetLogic();
List<RuleSet> ruleSets = logic.GetRulesByCategoryId(request.CategoryId);
GetRuleSetsByCategoryIdResponse response = new GetRuleSetsByCategoryIdResponse();
response.RuleSets = ContractTranslator.TranslateBetweenRuleSetDataContractAndRuleSet.TranslateRuleSetListToRuleSetDataContractList(ruleSets);
return response;
}
There is nothing weird or fancy about the service. It just returns a collection of data contracts. When I first generated the proxy for this service I added a service reference to the Winform project (right clicked project, add service reference). The above collection of RuleSets got returned as a BindingList<RuleSetDataContract>. To me that was weird, I had expected it to be RuleSetDataContract[]. Basically an array of objects. It was weird but I lived with it whilst I was prototyping.
Later as I fleshed my app out more I moved my proxy class into a C# class library all on its on. When I generated the proxy class from a plain old C# class library class, the BindingList<RuleSetDataContract> collection was in fact replaced with an array of RuleSetDataContracts! If someone cares to go deep on this and explain why you would get two totally different types of proxy classes generated for me I'm all ears. Right now I'm in the coding zone and have to get done so let me leave you with my fix so I can get back to coding.
In the end what I really wanted to deal with were generic lists (List<Object>). Instead of using VS2005 to generate the file I shelled out to the Visual Studio command prompt and ran this command:
svcutil /d:. /noconfig /o:OrbbService.cs /r:DataContracts.dll /ct:System.Collections.Generic.List`1 http://localhost:57199/MyService.svc
Here is the break down of what these switches mean:
- /d - working directory
- /noconfig - don't generate the config file (duh)
- /o - filename for generated code
- /r - Used to specify assemblies that might contain types representing the metadata being imported
- /ct - fully qualified name of the type to use as a collection data type when code is generated from schemas
By running the above command I now have everything the way I really want it with List<Object> for all of the return types. Life is good. I can now continue my regularly scheduled programming.