从Silverlight 4调用Web Service时的CommunicationException

时间:2010-12-13 14:49:20

标签: silverlight wcf silverlight-4.0 bing-maps

(更新)

我使用VS2010模板构建了一个简单的Silverlight 4 Bing Maps应用程序。

在同一个解决方案中,我有一个带有简单Web服务的ASP.NET项目:ContentService.asmx。

我现在正在本地机器上运行。

我可以在ASP.NET页面中调用Web服务而没有任何问题。

然而,尽我所能,我无法让Silverlight与之交谈。

我尝试从Silverlight调用Web服务,如下所示:

public BingMapAppPanel()
{
    InitializeComponent();

    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);

    EndpointAddress endPoint = new EndpointAddress("http://localhost:49501/ContentService.asmx");

    ContentServiceSoapClient contentService = new ContentServiceSoapClient(binding, endPoint);

    contentService.GetAllCategoriesCompleted += new EventHandler<GetAllCategoriesCompletedEventArgs>(contentService_GetAllCategoriesCompleted);
    contentService.GetAllCategoriesAsync();
}

void contentService_GetAllCategoriesCompleted(object sender, GetAllCategoriesCompletedEventArgs e)
{    
    MessageBox.Show(e.Result.Count.ToString());
}

它应输出返回的List对象的计数,但它会抛出以下异常:

Bing Maps has encountered an exception. Please press CTRL+C to copy the error message text.

ErrorSource: Unhandled Exception.

ErrorType:  System.Reflection.TargetInvocationException

ErrorMessage:  An exception occurred during the operation, making the result invalid.  Check InnerException for exception details.

ErrorCallStack:
   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()

   at BingMapApp.Content.GetAllCategoriesCompletedEventArgs.get_Result()

   at BingMapApp.BingMapAppPanel.contentService_GetAllCategoriesCompleted(Object sender, GetAllCategoriesCompletedEventArgs e)

   at BingMapApp.Content.ContentServiceSoapClient.OnGetAllCategoriesCompleted(Object state)

InnerType:  System.ServiceModel.CommunicationException

InnerMessage:  

InnerCallStack:
   at System.ServiceModel.AsyncResult.End[TAsyncResult](IAsyncResult result)

   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)

   at System.ServiceModel.ClientBase`1.ChannelBase`1.EndInvoke(String methodName, Object[] args, IAsyncResult result)

   at BingMapApp.Content.ContentServiceSoapClient.ContentServiceSoapClientChannel.EndGetAllCategories(IAsyncResult result)

   at BingMapApp.Content.ContentServiceSoapClient.BingMapApp.Content.ContentServiceSoap.EndGetAllCategories(IAsyncResult result)

   at BingMapApp.Content.ContentServiceSoapClient.EndGetAllCategories(IAsyncResult result)

   at BingMapApp.Content.ContentServiceSoapClient.OnEndGetAllCategories(IAsyncResult result)

   at System.ServiceModel.ClientBase`1.OnAsyncCallCompleted(IAsyncResult result)

我尝试了各种crossdomain.xml和clientaccesspolicy.xml文件,没有任何效果。

任何建议都非常感谢 - 谢谢。

更新

我将端点地址更改为127.0.0.1而不是localhost,并且它有效!

EndpointAddress endPoint = new EndpointAddress(“http://127.0.0.1:49501/ContentService.asmx”);

任何人都知道为什么?

1 个答案:

答案 0 :(得分:1)

我猜你已经在你的解决方案中添加了另一个Web项目来托管这个WCF服务?如果是这样,它很可能是一个跨域问题,Silverlight应用程序试图与另一个域上的服务进行通信而不是它所源自的服务(即使它只是端口号不同。如果可能,托管WCF在您创建Silverlight应用程序时创建的项目中的服务,这可能会解决您的问题。如果您仍然遇到问题(或者情况并非如此),请尝试使用Fiddler查看幕后发生的情况。但是要让Fiddler获取流量(它会忽略localhost流量),请用ipv4.fiddler替换URI中对localhost的引用。

希望这会有所帮助......

Chris Anderson

注意:在发布此内容之前,我没有看到您的编辑内容。那太奇怪了!

相关问题