如何在Rad Center Windows Phone应用程序中调用WCF服务

问题描述:

大家好,
我通过使用Windows Azure云服务开发了wcf服务.
现在,我需要将该wcf服务调用到我的应用程序中.我正在使用Rad Center Windows Phone应用程序.我在服务参考中添加了wcf服务,但无法创建
page1.xaml.cs 中的参考.任何建议表示赞赏.

注意:我已经浏览了下面的链接,但对我的概念没有帮助.


在此先感谢.
nareshraju

Hi all,
I developed a wcf service by using windows Azure Cloud service.
Now i need to call that wcf service into my application. I am using Rad center windows phone application. I added wcf service in service reference but not possible to create
a reference in page1.xaml.cs . any suggestions appreciate.

Note: I already goggled i got below link but not useful to my concept.


thanks in advance.
nareshraju

如果您已经在与该代码相同的项目中成功创建了服务引用,那么编写它就不难了.使您的配置文件正确无误,但代码可能如下所示:


假设我的服务参考称为MyServiceReference,而Web服务本身称为MyWebService.我们将说您有一个名为Get42的方法,该方法以整数形式返回数字42.


If you''ve successfully created a service reference in the same project as that code, then it shouldn''t be too hard to code it. Getting your config file correct is fiddly, but the code probably goes something like this:


Say my Service Reference is called MyServiceReference and the web service itself is called MyWebService. We''ll say that you have a method called Get42 which returns the number 42 as an int.


public Page1()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(Page1_Loaded);
}

void Page1_Loaded(object sender, RoutedEventArgs e)
{
    // Instantiate a web service client
    // Note that the name MyWebService gets the word Client appended
    MyServiceReference.MyWebServiceClient proxy = new MyServiceReference.MyWebServiceClient();
    // Create a handler for the webservice - note it has the word Completed on the end of the web service operation included 
    proxy.Get42Completed += new
        EventHandler<MyServiceReference.Get42CompletedEventArgs>
        (GetDataCompleted);
    // Make the call to the webservice, note it has the word Async appended
    proxy.Get42Async();
}

void Get42Completed(object sender,
    MyServiceReference.Get42CompletedEventArgs e)
{
    // Always check for errors before examining the result
    if(e.Error != null)
    {
        MessageBox.Show(e.Error.ToString());
        return;
    }
    // Read the result
    MessageBox.Show(e.Result.ToString());
}



以下是需要注意的几件事...

您的服务参考应设置为允许异步调用,因此请右键单击它,然后选择配置服务参考"并进行检查.还要检查您的Web服务没有将其操作标记为静态,因为这会使它们对客户端不可见.

希望有帮助.

如果这能回答您的问题,请标记为答案.



Here are a few things to watch out for...

Your service reference should be set to allow Async calls, so right click it, and choose "Configure service reference" and check that. Also check that your web service hasn''t got its operations marked as static as that makes them invisible to the client.

Hope that helps.

Please mark as the answer if this answers your question.