问题
I'm working on a silverlight reporting tool which draws up all sorts of fancy graphs based on client data. The issue I'm having right now is a good way to get all the data I need from the database to my silverlight app.
Sofar I have a web service which chunks up my data into groups of 1000 and ships them back to me. I need a bit over 3000 records, which calls for about 4 calls to the web service at 2 seconds a piece. Needless to say, it's slower than I'd want it to be.
I currently have these set:
binding.MaxBufferSize = 2147483647;
binding.MaxReceivedMessageSize = 2147483647;
I'm sure someone has a better way to grab db data that's faster. At the very least, a way that would let me get all my data in one try.
回答1:
It (based in part on the earlier question) sounds like bandwidth is the issue; I would give serious thought to trying protobuf-net; the "protocol buffers" format designed by Google is very efficient (much more so that the default DataContractSerializer
), and it can be used very conveniently from .NET. Ideal for bandwidth-related scenarios. The only glitch is that currently the WCF hooks don't work with Silverlight (so you can't just add an attribute / config entry), but you can pass the data as a byte[]
easily enough (just return byte[]
from a method).
For example; if you have a record like:
[ProtoContract]
public class MyRecord {
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public string Description {get;set;}
// etc
}
and a List<MyRecord>
, then you should be able to use:
byte[] result;
using(MemoryStream ms = new MemoryStream()) {
Serializer.Serialize(ms, list); // or maybe (list, ms) ;-p
result = ms.ToArray();
}
I've also seen somebody return a Stream
on the operation-contract (rather than a byte[]
) - which seemed to work better with MTOM (hint: you definitely want to enable MTOM if possible when passing raw binary).
来源:https://stackoverflow.com/questions/1462452/silverlight-data-access