Silverlight Retrieving on-demand reference data from a silent server

I have a text file with a list of 300,000 words and the frequency with which they occur. Each line is in Word: FequencyOfOccurence format. I want this information to be available from C # code. I cannot hard copy the list, because it is too long, and I’m not sure how to access it from a file on the server. Ideally, I would like information to be downloaded only if it was used (to save bandwidth), but this is not a high priority, since the file is not too large and the Internet speed is constantly increasing. It should not be used for binding. This information does not need to be edited after the creation of the project.

+4
source share
3 answers

Here is another alternative. Close the file and paste it into the clientBin folder next to the application XAP. Then at the application point where the content is required, do something like this: -

public void GetWordFrequencyResource(Action<string> callback) { WebClient client = new WebClient(); client.OpenReadAsync += (s, args) => { try { var zipRes = new StreamResourceInfo(args.Result, null) var txtRes = Application.GetResourceStream(zipRes, new Uri("WordFrequency.txt", UriKind.Relative)); string result = new StreamReader(txtRes.Stream).ReadToEnd(); callback(result); } catch { callback(null); //Fetch failed. } } client.OpenReadAsync(new Uri("WordFrequency.zip", UriKind.Relative")); } 

Usage: -

  var wordFrequency = new Dictionary<string, int>(); GetWordFrequencyResource(s => { // Code here to burst string into dictionary. }); // Note code here is asynchronous with the building of the dictionary don't attempt to // use the dictionary here. 

The above code allows you to store the file in an efficient zip format, but not in XAP itself. Therefore, you can download it on demand. It uses the fact that XAP is a zip file, so Application.GetResourceStream , which is designed to extract resources from XAP files, can be used in a zip file.

By the way, I really do not suggest you use a dictionary, I just use the dictionary as a simple example. In fact, I would suggest that the file is in an ordered order. If so, you can use KeyValuePair<string, int> for each record, but create your own collection type that stores them in an array or List , and then use some binary search methods to index it.

+2
source

You can create a server-side WCF service that will send data to a Silverlight application. After receiving the information, you can cache it in memory inside the client. Here's an example of calling a WCF service method from Silverlight.

Another possibility is to embed the text file in the Silverlight assembly, which is deployed for the client:

 using (var stream = Assembly.GetExecutingAssembly() .GetManifestResourceStream("namespace.data.txt")) using (var reader = new StreamReader(stream)) { string data = reader.ReadToEnd(); // Do something with the data } 
0
source

Based on your comments, you can download a word list file if you require a very thin server level. The XAP file that contains your Silverlight application is nothing more than a ZIP file with all the link files for your Silverlight client tier. Try adding the word list as content that compiles in XAP and see how big the file is. Text is usually compressed very well. In general, however, you will want to be friendly to your users, how much memory your application consumes. Loading a huge text file into memory, in addition to everything you need in your application, can cause your application to become a hog resource.

The best practice, in general, would be to call a web service. The service could execute any search logic that you need. Here's a quick search blog post where you should start: (It was written for SL2, but it should apply for SL3.)

Call Web Services with Silverlight 2

It would be even better to save your list on SQL Server. It will be much easier and faster to request.

0
source

Source: https://habr.com/ru/post/1301261/


All Articles