我想从WCF服务返回JSONified匿名类型。从匿名类型返回Json
我已经这样做是成功的,但是我正在寻找一个更好的选择..
// In IService
[OperationContract]
[FaultContract(typeof(ProcessExecutionFault))]
[Description("Return All Room Types")]
[WebInvoke(UriTemplate = "/GetAllRoomTypes", Method = "GET", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Stream GetAllRoomTypes();
// In Service Implementation
[LogBeforeAfter]
Stream GetAllRoomTypes()
{
try
{
var allRoomTypes = Helper.GetAllRoomTypes();
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(allRoomTypes);
writer.Flush();
stream.Position = 0;
return stream;
}
catch (Exception ex)
{
TableLogger.InsertExceptionMessage(ex);
return null;
}
}
// In Business Logic:
public string GetAllRoomTypes(){
try
{
return CustomRetryPolicy.GetRetryPolicy().ExecuteAction(() =>
{
using (var context = new DatabaseEntity())
{
var retResult = from v in context.RoomMasters select new { Id = v.RoomTypeID, Type = v.RoomType };
var retResult1 = retResult.ToJson();
return retResult1;
}
}
);
}
catch (Exception ex)
{
Trace.Write(String.Format("Exception Occured, Message: {0}, Stack Trace :{1} ", ex.Message, ex.StackTrace));
return null;
}
}
我的问题是,是否有更好的方法来做到这一点?
约重新调谐[信息](http://msdn.microsoft.com/en-us/library/ms734675(V = vs.110)什么。 aspx),我在[Nelibur](https://github.com/Nelibur/Nibibur)中也是这样做的。 Here're [Service](https://github.com/Nelibur/Nelibur/wiki/How-to-create-REST-message-based-Servcie-on-pure-WCF)和[Client](https:// github.com/Nelibur/Nelibur/wiki/How-to-create-REST-message-based-Client-on-pure-WCF) – GSerjo