简体   繁体   中英

Date Serialization in WCF Rest Service

Im using C# and I have the following LINQ statement

var WeeklySalesQuery = from d in db.DashboardFigures
                                 join s in outlets.Split(',').Select(x => int.Parse(x)) on d.OutletNo equals s
                                 where (d.TypeOfinformation == "SALES-TW")
                                 group d by new
                                 {
                                     d.TypeOfinformation,
                                     d.Date
                                 } into newGroupedresult
                                 select new Weeklysales()
                                 {
                                     dt = ((DateTime)newGroupedresult.Key.Date),
                                     Sv = (double)newGroupedresult.Sum(d => d.Value_1) + (double)newGroupedresult.Sum(d => d.Value_2),
                                     KPI = 1
                                 };

However, the format of dt that is coming out is

"\/Date(1421884800000+0000)\/

I have tried Changing the code to

dt = ((DateTime)newGroupedresult.Key.Date).ToString("dd.mm.yyyy")

bit I get build errors telling me I cannot convert a string into DateTime format.

As this is a new REST service, the best option is to abandon WCF and create use ASP.NET Web API to create REST services. WCF Rest services were a stop-gap measure that carry a lot of legacy baggage. One of them is the largely abandoned and slow serializer they use.

While it is possible to change the serializer it isn't trivial. The process is described in Supporting different data and serialization formats in WCF by Carlos Figueira and it's definitely not for beginners.

The code you posted suggests you are trying to create a REST service to support a BI dashboard. In this case you should also consider OData on top of Web API. OData exposes a REST service over data entities and allows clients to specify queries using URL query parameters like filter, select, group etc.

This will allow your clients to specify the criteria and columns they want in the URL without requiring modifications to your service code. OData works on top of IQueryable so the query that is finally executed only pulls the data the client wants from the database.

Visual Studio has wizards that allow you to easily expose OData services from any Entity Framework model, although OData isn't limited to only EF.

Clients can create query URLs directly or they can use the proxies generated by Visual Studio to work with LINQ syntax, or the javascript client libraries.

Finally, you don't need to worry about clients making weird requests that will kill the database. You can expose specific queries only as methods, or apply your own restrictions on the IQueryable that will be exposed.

It's also very easy to add caching support, making operations like revisiting the same page while paging or viewing different dashboard pages, very cheap. Doing this with WCF is non-trivial.

PS: Excel can also read data from OData services. Should stop analysts and financial managers from demanding access to the database.

A good blow-by-blow tutorial is Create an OData v4 Endpoint Using ASP.NET Web API 2.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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