質問

enter code hereI would like to send dynamic type with .asvc web service. I have one class with some properties. I would like to send any kind of object even class in this list. And cast back on client side.

I mean I always want to use SysResult type. But sometimes with List<Apple>, sometimes List<Orrange>

Like bellow

    [Serializable]
    public class SysResult
    {
        public int CRCResult;
        public long CRCTransactionId;
        public bool CRCStatus;
        public string CRCMessage;
        public List<Anything> ListObject;
    }

I've tried to use object[], Array, List<object>, dynamic etc. But I am getting this error. "The underlying connection was closed: The connection was closed unexpectedly."

By the way for all non-specific object types service referance class represents object[] type.

  [System.Runtime.Serialization.DataMemberAttribute(IsRequired=true)]
        public object[] ListObject {
            get {
                return this.ListObjectField;
            }
            set {
                if ((object.ReferenceEquals(this.ListObjectField, value) != true)) {
                    this.ListObjectField = value;
                    this.RaisePropertyChanged("ListObject");
                }
            }
        }

Is there any way to send dynamic type? Or am I trying to do something totally wrong?

Thanks in advance.

役に立ちましたか?

解決

You have to return a known type . It can't return just "object" or even an interface. How would wcf know what to serialize the object into on the client if there is no datacontract?

Normally, when passing parameters and return values between a client and a service, both endpoints share all of the data contracts of the data to be transmitted. However, this is not the case in the following circumstances:

... The declared type for the information to be transmitted is Object. Because every type inherits from Object, and it cannot be known in advance which type is actually sent, the receiving endpoint cannot determine in advance the data contract for the transmitted data.

http://msdn.microsoft.com/en-us/library/ms730167(v=vs.110).aspx

Also

You could use KnownTypes to map your Interfaces and Objects. Look at this links: WCF - Object as known type -> interoperable? WCF, Interface return type and KnownTypes

他のヒント

This might give you some help in the matter at hand

WCF does not support the use of generic methods for service operation. In other words, only concrete types can be used for service operations. Open generic types cannot be used. Now, it is important to clarify this is not a limitation of WCF. Rather, it is a limitation of WSDL, which is used to expose service metadata to consumers. There is no construct within WSDL to define a generic type. Generally speaking, I agree with this behavior since it decreases the coupling between a client and service.

Although generic methods are not supported, it is possible to expose generic objects for the purpose of exchanging data. However, there are some limitations. Let's take a closer look:

http://jeffbarnes.net/blog/post/2007/05/10/WCF-Serialization-and-Generics.aspx

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top