views:

278

answers:

4

Hi,

I've made a little game in silverlight that records users scores whilst they play.

I decided it would be a lot better if I could implement a leaderboard, so I created a database in mySQL to store all the high scores along with names and dates. I have created some communications to the database in ASP.net. This works and I can simply insert and get data within the code.

It's now time to link the silverlight project with the ASP.net database communications, so I can send the users name and score as variables to my ASP.net code and then it will upload it to the database. That's all I need. Surely there must be an easy way of doing this, I just can't seem to find any ways when researching.

Thanks in advance, Lloyd

A: 

By ASP.NET, do you mean an ASP.NET Webforms app?

If so, an ASP.NET Webforms app is a method of building a UI. What you need is an API, for your Silverlight app to use programatically. For this purpose you may want to consider building an ASP.NET Webservice instead, which provides an API over HTTP.

AdamRalph
A: 

What do you need its to send data to web server from a Silverlight application, right?

You can:

Rubens Farias
A: 

An easy way to do this is to have your Silverlight code create a REST URL by encoding the information into the query string, and invoking an .aspx page on the server. The page wouldn't need to return any markup; it would just handle the back-end stuff and return.

Alternatively, you could make a web service call from Silverlight to your back end.

I prefer the latter approach. It's a little more work the first time through, but it's also more general purpose and makes for generally better code in the long run.

Although technically you could use JavaScript, I wouldn't suggest it; why go backwards in tech if you don't have to?

RickNZ
+2  A: 

Hi Lloyd,

At first you need add Generic Handler to your ASP.Net project.

  public class Handler1 : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        string userName = context.Request["user"];
        int score = int.Parse(context.Request["score"]);
        //And store it in DB
    }
 }

After you need call this handler from SilverLight app:

         string uri = HtmlPage.Document.DocumentUri.ToString();

        // Remove the web page from the current URI to get the root URI. 
         string   rootUri = uri.Remove(uri.LastIndexOf('/'),
         uri.Length - uri.LastIndexOf('/')); 

         string diggUrl = String.Format(rootUri + "/" + "test.ashx?user={0}&score={1}", "testuser", "234");

        // Initiate Async Network call to Digg
        WebClient diggService = new WebClient();
        diggService.DownloadStringAsync(new Uri(diggUrl));
cromacl