WCF Proxy Returning Array instead of List EVEN THO

2019-02-08 13:22发布

问题:

I have a VS 2010 solution containing a WCF Library project and another project consuming that web service. After opening in VS 2012, it was upgraded.

The proxy now returns List<T> types as arrays, even though CollectionMappings is clearly set to Generic.List.

What could be happening?

Someone else has a similar problem here but he was downgrading from VS 2012 to VS 2010 instead.

Edit: I double checked, and Reference.svcmap contains:

<CollectionMappings>
  <CollectionMapping TypeName="System.Collections.Generic.List`1" Category="List" />
</CollectionMappings>

but Reference.cs contains stuff such as:

 public xxx.ServiceReference1.ADUser[] get_ADUsers;

when in the web service it is:

 public List<ADUser> get_ADUsers(string wildcard, string sortexp, string sortorder)

More info (added Dec 12, 2012):

The solution, created in VS2010, was working fine on another PC. It was checked into TFS from that PC. On this problematic PC, we did a mapping and GET. When we try to build, we got that error where all the List<T> types used in the service reference were all somehow treated as arrays. We installed VS 2010 on the problematic PC and GET that solution too. And the same error is also there. So, it appears it is not related to VS 2012.

All PCs are Windows 7 Professional.

More info (added Dec 19, 2012):

When the project was opened, ServiceReferences/ServiceReference1/Reference.cs on the local PC was modified automatically. The changes were huge. Below is a small part of it:

Two methods are shown. List<string> get_Hotlines() become string[] get_Hotlines() and List<string> get_HotlinesBySite() becomes string[] get_HotlinesBySite().

Why is the file changed even without my asking? The VS 2012 upgrade log said two files were changed but Reference.cs wasn't one of them.

回答1:

When you add reference to wcf service you need to change Collection Type to Generic List:

You can also update this settings just select service reference inside your solution, make right mouse click and select "Configure Service Reference..."



回答2:

What did it for me was:

  1. install all updates to Visual Studio 2013
  2. uncheck "reuse types in referenced assemblies"


回答3:

I had the same problem with VS2010. After hours of trying and testing I think, this is a bug of code generation. I was able to reproduce it. Here is code on server side, which has impact on behavior of Visual Studio code generation. The commented line generated this problem in my case. When this member was not nullable, then Visual studio generated Array collections instead List<>.

[DataContract]
public enum DocumentAttachmentSourceType
{
    [EnumMember]
    ServiceMission                              
}

[MessageContract]
public class DocumentUploadRequest : IDisposable
{
    [MessageHeader]
    public long NodeId { get; set; }

    [MessageHeader]
    public DocumentAttachmentSourceType? AttachmentSource { get; set; } //This works
    //public DocumentAttachmentSourceType  AttachmentSource { get; set; }   //This not works !!!!!!!

    [MessageBodyMember]
    public System.IO.Stream Stream { get; set; }


    public void Dispose()
    {
        if (Stream != null)
        {
            Stream.Close();
            Stream = null;
        }
    } 

}