简体   繁体   English

通过WCF RIA服务将JSON从服务器传输到客户端

[英]Transfer JSON from Server to Client via WCF RIA Service

I am complete newbie to Silverlight and the WCF platform. 我是Silverlight和WCF平台的新手。 I want to get some data from a server using a Silverlight client. 我想使用Silverlight客户端从服务器获取一些数据。 The solution has a WCF RIA service class library for reading the data and serializing it into a JSON string, but I can't figure out how to create the request for the data, run the server method and return the JSON string for Deserialization at the client side. 该解决方案具有WCF RIA服务类库,用于读取数据并将其序列化为JSON字符串,但是我无法弄清楚如何创建数据请求,运行服务器方法并在JSON返回用于反序列化的JSON字符串。客户端。

I have spent hours searching and no reasonable solution. 我花了数小时进行搜索,没有找到合理的解决方案。 Till now I have done this: 到目前为止,我已经做到了:

[WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "data/{id}")]
public string getLogs(string server)
{
    EventLog[] remoteEventLogs = EventLog.GetEventLogs(System.Environment.MachineName);

    ObservableCollection<string> logs = new ObservableCollection<string>();

    for (int i = 0; i < remoteEventLogs.Length; i++)
    {
        logs.Add(remoteEventLogs[i].Log);
    }

     return serializer(logs); //helper function using DataContractJsonSerializer

}

But I can't wrap my head around what is going on. 但是我无法解决发生的一切。 Any help please! 请帮忙!

If you are using RIA Services, you just need to do this: 如果您使用的是RIA服务,则只需执行以下操作:

[Invoke]
public string getLogs(string server)
{
    ...
    return serializer(logs);
}

And now you can call the getLogs from the Silverlight. 现在,您可以从Silverlight调用getLogs。 But you can too use: 但是您也可以使用:

[Invoke]
public IEnumerable<string> getLogs(string server)
{
    ...
    return logs;
}

this way, you return the list without having to serialize it to json. 这样,您无需将列表序列化为json就可以返回列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM