WCF WEB API Client Pure Json:如果在大数据量的API调用中使用MemoryStream会导致性能问题吗
我编写了以下代码来根据用户传递的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么使用 MemoryStream?为什么不直接将 JSON 作为字符串返回......
这将允许您将任何“DataContract”与 JSON 相互转换。
Why are you using a MemoryStream? Why not just return the JSON as a string....
This will let you convert any "DataContract" to or from JSON.