开发者

wcf ria services method return

I have a S开发者_如何学Pythonilverlight site with wcf ria services, this is just a test. The service is as follow

[EnableClientAccess()]
    public class PersonService : DomainService
    {
        [Invoke]
        public string[] GetPersonNames()
        {
            return new string[] { "abc", "cba", "ddd", "ttt" };
        }

        [Invoke]
        public string GetName()
        {
            return "teste";
        }

        [Invoke]
        public string Test(string str)
        {
            return str;
        }
    }

I have a xaml page where I call the service

SLRiaTest.Web.PersonContext person = new SLRiaTest.Web.PersonContext();

        public MainPage()
        {
            InitializeComponent();

            person.GetPersonNames(OnFinished,null);
        }

        private void OnFinished(InvokeOperation<string[]> obj)
        {
            var list = obj.Value;

        }

but the Value is always null and the break point that I have in the serice never gets hit. I search the internet high and low and can't find anything that might tell me what I'm doing wrong, any help would be great, I'm desperate.... I suppose that I can use RIA service without entity framework right?


The details on what does and does not work for calls across the wire in RIA Services are complex. If you want a detailed answer on what is going on here, you'll have better luck asking on the RIA Services forum

However, you're kind of going against the design of RIA Services. If you want to return a collection of objects, you should use Query instead of Invoke.

[EnableClientAccess()]
public class PersonService : DomainService
{
    [Query]
    public IEnumerable<string> GetPersonNames()
    {
        return new string[] { "abc", "cba", "ddd", "ttt" };
    }
}

...

    public MainPage()
    {
        InitializeComponent();

        person.Load(person.GetPersonNamesQuery(), OnFinished, null);
    }

    private void OnFinished(LoadOperation<IEnumerable<string>> obj)
    {
        var list = obj.Value;
    }

I just did that off the top of my head so might have some minor errors. But that's the general idea.


I have a working example of what you are trying. You don't need to change your domain service. Ensure that you have installed Visual Studio 2001 SP1 which also updates WCF RIA to SP1.

In your Silverlight page you can call your domain service as follows:

public partial class MainPage : UserControl
    {
        private PersonContext _ctx = new PersonContext();

        public MainPage ()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler( MainPage_Loaded );
        }

        private void MainPage_Loaded ( object sender, RoutedEventArgs e )
        {
            _ctx.GetPersonNames( ( op ) =>
                {
                    if ( !op.HasError )
                    {
                        // The data here is your String[]
                        var data = op.Value;
                    }
                }, null );
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜