WCF WEB API Client Pure Json:如果在大数据量的API调用中使用MemoryStream会导致性能问题吗

发布于 2025-01-07 17:41:17 字数 1350 浏览 0 评论 0原文

我编写了以下代码来根据用户传递的 JobId 返回不同的 Json 对象。 我想知道的是,在 Function ReturnPureJson 中使用 MemoryStream 是否会导致 WCF API 调用中出现任何与性能相关的问题?

    [WebInvoke(UriTemplate = "{jobId}", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
    public Stream GetJobData(Guid jobId)
    {

        object responseData = null;

        //Code to fetch the proper Job Based on the JobId Passed
        Job job = GetJobData(jobId);

        if (job != null)
        {
            switch (job.JobType)
            {
                case JobType.UserData:
                    responseData = GetUserData(); //Returns a List<UserData> where Userdata is a class
                    break;
                case Job.EJobType.ApplicationData:
                     responseData = GetApplicationData();//Returns a List<ApplicationData> where ApplicationData is a class
                    break;
                  //some more case statements to fetch appropriate response
            }
        }

        return ReturnPureJson(responseData);
    }
    private Stream ReturnPureJson(dynamic responseModel)
    {
        string jsonClient = Json.Encode(responseModel);
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
    }

I have written the following code to return a different Json Object based on the JobId user is passing.
what i wanted to know was whether using MemoryStream in the Function ReturnPureJson cause any performance related issuse in the WCF API calls?

    [WebInvoke(UriTemplate = "{jobId}", Method = "GET", ResponseFormat = WebMessageFormat.Json)]
    public Stream GetJobData(Guid jobId)
    {

        object responseData = null;

        //Code to fetch the proper Job Based on the JobId Passed
        Job job = GetJobData(jobId);

        if (job != null)
        {
            switch (job.JobType)
            {
                case JobType.UserData:
                    responseData = GetUserData(); //Returns a List<UserData> where Userdata is a class
                    break;
                case Job.EJobType.ApplicationData:
                     responseData = GetApplicationData();//Returns a List<ApplicationData> where ApplicationData is a class
                    break;
                  //some more case statements to fetch appropriate response
            }
        }

        return ReturnPureJson(responseData);
    }
    private Stream ReturnPureJson(dynamic responseModel)
    {
        string jsonClient = Json.Encode(responseModel);
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        return new MemoryStream(Encoding.UTF8.GetBytes(jsonClient));
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故人如初 2025-01-14 17:41:17

为什么使用 MemoryStream?为什么不直接将 JSON 作为字符串返回......

Imports System.IO
Imports System.Runtime.Serialization.Json

Public Class JsonConvertor(Of t)
    Public Function FromJSON(ByRef sreader As StreamReader) As t
       If GetType(t).Equals(GetType(String)) Then
           Dim result As Object = sreader.ReadToEnd.Replace("""", "")
           Return CType(result, t)
       Else
           Dim ds As New DataContractJsonSerializer(GetType(t))
           Dim result As t = DirectCast(ds.ReadObject(sreader.BaseStream), t)
           ds = Nothing
           Return result
       End If
   End Function

   Public Function ToJSON(ByVal ObjectToBeConverted As t) As String
       Dim s As Stream = New MemoryStream()
       Dim ds As New DataContractJsonSerializer(GetType(t))
       ds.WriteObject(s, ObjectToBeConverted)

       Dim output As New StreamReader(s)
       Dim ostr As String = output.ReadToEnd
       s.Dispose()
       Return ostr
   End Function
End Class

这将允许您将任何“DataContract”与 JSON 相互转换。

Why are you using a MemoryStream? Why not just return the JSON as a string....

Imports System.IO
Imports System.Runtime.Serialization.Json

Public Class JsonConvertor(Of t)
    Public Function FromJSON(ByRef sreader As StreamReader) As t
       If GetType(t).Equals(GetType(String)) Then
           Dim result As Object = sreader.ReadToEnd.Replace("""", "")
           Return CType(result, t)
       Else
           Dim ds As New DataContractJsonSerializer(GetType(t))
           Dim result As t = DirectCast(ds.ReadObject(sreader.BaseStream), t)
           ds = Nothing
           Return result
       End If
   End Function

   Public Function ToJSON(ByVal ObjectToBeConverted As t) As String
       Dim s As Stream = New MemoryStream()
       Dim ds As New DataContractJsonSerializer(GetType(t))
       ds.WriteObject(s, ObjectToBeConverted)

       Dim output As New StreamReader(s)
       Dim ostr As String = output.ReadToEnd
       s.Dispose()
       Return ostr
   End Function
End Class

This will let you convert any "DataContract" to or from JSON.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文