【问题标题】:returning a JSON formatted file via WCF通过 WCF 返回 JSON 格式的文件
【发布时间】:2013-06-13 22:02:53
【问题描述】:

我们已经看到许多与通过 WCF 返回 JSON 数据相关的帖子,但它们都涵盖了将对象转换为 JSON,然后通过属性的魔力返回转换为 JSON 的对象。
我们有许多想要通过 WCF 服务返回的预格式化 JSON 文件。基本上我们需要做的就是读取文件(或文件的缓存副本),然后将数据作为字符串返回。我认为......读入 JSON 文件,将其序列化为对象然后反序列化回 JSON 似乎很浪费。对此有什么帮助吗?

【问题讨论】:

    标签: asp.net json wcf


    【解决方案1】:

    使用 WebHttpBinding 时,这就像创建一个带有 Stream 返回类型的 WebGet 注释方法一样简单:

    [WebGet]
    public Stream GetFile(Int32 someId)
    {
      //your logic to lookup or create the file here.
    
      //Open the file (a MemoryStream would be acceptible as well if you were creating the data on the fly
      Stream stream = File.OpenRead(yourFilePath);
    
      //register an event to clean up the temporary file (if necessary)
      OperationContext.Current.OperationCompleted += (s, e) =>
      {
        File.Delete(yourFilePath);
      };
    
      return stream;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多