Search code examples
silverlightwindows-phone-7servicereferencecommunicationexception

Windows Phone 7 Silverlight Service References CommunicationException: "Server returned an error: Not Found"


I am developing an application for Windows Phone 7. I am trying to use services which are provided by the web site I am trying to get information from. I am using an asynchronous request. So if I try to get information from a web site without any authentication I use this code:

EventSrv.EventSrvSoapClient client = new EventSrv.EventSrvSoapClient();
client.GetAppointmentsAsync();
client.GetAppointmentsCompleted += new EventHandler<EventSrv.GetAppointmentsCompletedEventArgs>(events_completed);

and it works fine. But as soon as I want to use a service from a web site which requires authentication I get a

CommunicationException: _innerException:"Server returned an error: Not Found"

at

public L2P.DocumentsService.GetDocumentsResponse EndGetDocuments(System.IAsyncResult result) 
{
object[] _args = new object[0];
//Between this line
L2P.DocumentsService.GetDocumentsResponse _result = ((L2P.DocumentsService.GetDocumentsResponse)(base.EndInvoke("GetDocuments", _args, result)));
//and this line
return _result;
}

I am passing the credentials the following way:

DocumentsService.BaseServiceSoapClient docClient = new DocumentsService.BaseServiceSoapClient();
docClient.ClientCredentials.UserName.UserName = Variables.username;
docClient.ClientCredentials.UserName.Password = Variables.password;
docClient.GetDocumentsCompleted += new EventHandler<DocumentsService.GetDocumentsCompletedEventArgs>(getDocumentsCompleted);
docClient.GetDocumentsAsync();

It actually doesn't matter if I pass the credentials or not, I get the same exception. I don't really know what the problem is, maybe it has nothing to do with the authentication. I've read all the articles here on CommunicationException but they couldn't solve my problem.

Any help will be appreciated!


Solution

  • I've finally figured it out! The server uses Basic Authentication and the header is set to "POST" by default. So I needed to modify the Header, set it to "Basic" and add the credentials as well. Furthermore the

    CommunicationException: "Server returned an error: Not Found"

    always appear if there is any unhandled exception. So you need to debug and check the _innerException for more information.