0
votes

I'm very new to silverlight and WCF Ria services. I have background experience in WPF and WinForms.

Right now I'm developing a silverlight application that consists basically on a web page fetching data from a remote server.

I've read tons of forums and articles that explain how to use and consume web services and WCF. I've followed the msdn walkthrough on how to create a simple app that gets data from a DB and it worked great.

The problem is that I don't want any WCF related code or resource in my UI controls. Right now I'm using using the layered programming architecture:

UI --> BLL --> DAL

Each of these elements is a single project in the same solution. My DAL project is the web service (WCF Ria) that comunicates with the server.

I have a simple class (User Service) and method (GetUsers) in my DAL project with the following code:

        LoadOperation<u_WEBUSERS> loadOp = this.userContext.Load(this.userContext.GetU_WEBUSERSQuery());
        loadOp.Completed += (sender, args) =>
        {
            users = new List<UserObj>();
            foreach (var v in loadOp.Entities)
            {
                u_WEBUSERS uweb = v as u_WEBUSERS;
                UserObj u = new UserObj();

                u.Nome = uweb.nome;
                u.Morada = uweb.morada;
                users.Add(u);
            }
        };
        return users;

The thing is that my users object returns null. If I breakpoint I see that first is ending the method and only after is calling the completed event.

Is there any way make my GetUsers() to return the data base information? Maybe the layered achitecture that I'm using isn't the one suited for what I want... Thanks

2

2 Answers

1
votes

You can use simple Action or Action<T> delegate:

public void LoadUsers(Action<IEnumerable<UserObj>> callBack)
{
    LoadOperation<u_WEBUSERS> loadOp = this.userContext.Load(this.userContext.GetU_WEBUSERSQuery());
    loadOp.Completed += (sender, args) =>
    {
        users = new List<UserObj>();
        foreach (var v in loadOp.Entities)
        {
            u_WEBUSERS uweb = v as u_WEBUSERS;
            UserObj u = new UserObj();

            u.Nome = uweb.nome;
            u.Morada = uweb.morada;
            users.Add(u);
        }
        if(callBack != null)
            callBack(users);
    };
}
0
votes

You're mixing synchronous and asynchronous code together. You're setting up the completed event but the call doesn't return until it completes when you don't specify a handler. See the example here. So your code would be:

LoadOperation<u_WEBUSERS> loadOp =   this.userContext.Load(this.userContext.GetU_WEBUSERSQuery());
users = new List<UserObj>();
foreach (var v in loadOp.Entities)
{
     u_WEBUSERS uweb = v as u_WEBUSERS;
     UserObj u = new UserObj();

     u.Nome = uweb.nome;
     u.Morada = uweb.morada;
     users.Add(u);
 }
 return users;