how to pass List<Object> to WCF

2020-03-29 00:03发布

I have already configured my Service Reference and set my collection to Generic.List .

When i fill the List<Objects> with a string or int, it works fine. But when I fill it with a List<String>, it gives a runtime-error while trying to serialize parameter

So is there a way to pass a List<Objects> that contains several List<String> to WCF?

标签: c#
6条回答
Melony?
2楼-- · 2020-03-29 00:32

Use the KnownType attribute on your data contract. See Data Contract Known Types.

[KnownType(typeof(List<string>))]
[DataContract]
public class YourClass
{
    [DataMember]
    public List<object> YourList { get; set; }
}
查看更多
够拽才男人
3楼-- · 2020-03-29 00:38

Did you try passing the list as a "List of List of String" instead of "List of Objects"? Maybe SOAP lacks the serialization INFO when you pass the list as object....

List<List<String>> allYourStringLists = new List<List<String>>();
List<String> firstStringList = new List<String> { "a", "b" };

allYourStringLists.add(firstStringList);

then pass the "allYourStringLists" object

查看更多
可以哭但决不认输i
4楼-- · 2020-03-29 00:41

I think you'll need to pass an array of objects, rather than trying to use a generic. Web services are platform agnostic so need to be consumed by potentially any client - what if a .NET 1.x applicaton tried to access the service?

查看更多
Animai°情兽
5楼-- · 2020-03-29 00:42

Hmm... so you want to pass a list of list of strings. correct?

why not doing this then?

List<List<string>>
查看更多
Juvenile、少年°
6楼-- · 2020-03-29 00:43

Using the XmlSerializer Class:

Windows Communication Foundation (WCF) can use two different serialization technologies to turn the data in your application into XML that is transmitted between clients and services, a process called serialization.

DataContractSerializer as the Default:

By default WCF uses the DataContractSerializer class to serialize data types. This serializer supports the following types: Primitive types (for example, integers, strings, and byte arrays), as well as some special types, such as XmlElement and DateTime, which are treated as primitives.

Data contract types (types marked with the DataContractAttribute attribute).

Types marked with the SerializableAttribute attribute, which include types that implement the ISerializable interface.

Types that implement the IXmlSerializable interface.

Many common collection types, which include many generic collection types.

Many .NET Framework types fall into the latter two categories and are thus serializable. Arrays of serializable types are also serializable. For a complete list, see Specifying Data Transfer in Service Contracts. The DataContractSerializer, used together with data contract types, is the recommended way to write new WCF services. For more information, see Using Data Contracts.

When to Use the XmlSerializer Class:

WCF also supports the XmlSerializer class. The XmlSerializer class is not unique to WCF. It is the same serialization engine that ASP.NET Web services use. The XmlSerializer class supports a much narrower set of types than the DataContractSerializer class, but allows much more control over the resulting XML and supports much more of the XML Schema definition language (XSD) standard. It also does not require any declarative attributes on the serializable types. For more information, see the XML Serialization topic in the .NET Framework documentation. The XmlSerializer class does not support data contract types. When using Svcutil.exe or the Add Service Reference feature in Visual Studio to generate client code for a third-party service, or to access a third-party schema, an appropriate serializer is automatically selected for you. If the schema is not compatible with the DataContractSerializer, the XmlSerializer is selected.

Manually Switching to the XmlSerializer:

At times, you may have to manually switch to the XmlSerializer. This happens, for example, in the following cases: When migrating an application from ASP.NET Web services to WCF, you may want to reuse existing, XmlSerializer-compatible types instead of creating new data contract types.

When precise control over the XML that appears in messages is important, but a Web Services Description Language (WSDL) document is not available, for example, when creating a service with types that have to comply to a certain standardized, published schema that is not compatible with the DataContractSerializer.

When creating services that follow the legacy SOAP Encoding standard.

In these and other cases, you can manually switch to the XmlSerializer class by applying the XmlSerializerFormatAttribute attribute to your service, as shown in the following code.

[ServiceContract]
[XmlSerializerFormat]
public class BankingService
{
[OperationContract]
    public void ProcessTransaction(BankingTransaction bt)
    {
        // Code not shown.
    }

}

//BankingTransaction is not a data contract class,
//but is an XmlSerializer-compatible class instead.
public class BankingTransaction
{
    [XmlAttribute]
    public string Operation;
    [XmlElement]
    public Account fromAccount;
    [XmlElement]
    public Account toAccount;
    [XmlElement]
    public int amount;
}
//Notice that the Account class must also be XmlSerializer-compatible.
查看更多
乱世女痞
7楼-- · 2020-03-29 00:47

I've had the same problem passing generic lists in a WCF Service. The reason for this is that by default DataContractSerializer is used to serialize the data. Using the XmlSerializer solved the problem for me.

more info: http://msdn.microsoft.com/en-us/library/ms733901.aspx

查看更多
登录 后发表回答