2010-12-13 39 views
1

调用Web服务时(更新)从的CommunicationException Silverlight 4的

我已经建立了使用VS2010模板简单的Silverlight 4 Bing地图应用程序。

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

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

我可以从ASP.NET页面中调用Web Service,而不会出现问题。

但是,尽我所能,我无法让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()); 
} 

它应该输出返回的列表对象的数量,而是它抛出以下异常:

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的不是本地主机,它的工作!

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

任何人都知道为什么?

回答

1

我猜你已经添加了另一个Web项目到你的解决方案来承载这个WCF服务?如果是这样,它很可能是一个跨域问题,其中Silverlight应用程序试图与另一个域上的服务进行通信,而不是它所源自的域(尽管它只是端口号不同,如果可能,请将WCF在您创建Silverlight应用程序时创建的项目中的服务,这可能会解决您的问题。如果您仍然遇到问题(或情况并非如此),请尝试使用Fiddler来查看幕后发生的情况。为了得到提琴手拿起交通(然而它忽略本地主机流量),更换引用您的URI为localhost与ipv4.fiddler。

希望这有助于...

克里斯·安德森

注意:在发布此消息之前,我没有看到您的编辑。这是一个奇怪的!