Quantcast
Channel: VBForums - WPF, WCF, WF
Viewing all articles
Browse latest Browse all 256

WCF serialize deserialize WCF service

$
0
0
It cannot return list of T , it cannot return memorystream, cannot return anything.

So how would i build that?

If i do:

Code:

// Iservice
 [OperationContract]

        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
        string JsonSerialize(RootObject root);

//Service
  public string JsonSerialize(RootObject root) {
            string sserialized = new JavaScriptSerializer().Serialize(root);
            return sserialized;
        }

When i try to use the service from an app like this:

Code:

  var objectToSerialize = new RootObject();
            objectToSerialize.maindataclass = new List<MainDataClass>
                          {
                            new MainDataClass {
                            AllowDBNull = true,
                            Caption = "p",
                            ColumnName = "myc",
                            MaxLength = 40,
                            Readonly = false
                            },
                            new MainDataClass {
                            AllowDBNull = true,                           
                            Caption = "p2",
                            ColumnName = "myc2",
                            MaxLength = 40,
                            Readonly = false
                            }
                          };
            string jsonString = new JavaScriptSerializer().Serialize(objectToSerialize);

            IwcfdataClient srv = new ServiceReferenceData.IwcfdataClient();
// the error is below!
            string jsonstring = srv.JsonSerialize((ServiceReferenceData.RootObject)objectToSerialize);

Will error: Cannot convert type 'DXWebApplication2.RootObject' to 'DXWebApplication2.ServiceReferenceData.RootObject'


Also something like this on the WCF:

Code:

public T DeserializeJSon<T>(string jsonString)
        {
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
            T obj = (T)ser.ReadObject(stream);
            return obj;
        }

will not accept the memorystream,.

So how can i do this to work?

Thanks.

Viewing all articles
Browse latest Browse all 256

Trending Articles