Reputation: 1081
Then when I tried to use the new services I had an Exception.
Error on Reference.cs:
public System.IAsyncResult BeginGetArticle(MyWP7App.MyService.GetArticleRequest request, System.AsyncCallback callback, object asyncState) { object[] _args = new object[1]; _args[0] = request; System.IAsyncResult _result = base.BeginInvoke("GetArticle", _args, callback, asyncState); return _result; }
CommunicationException: {"The remote server returned an error: NotFound."}
StatusDescription: Unauthorized
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at System.Delegate.DynamicInvokeOne(Object[] args)
at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
This how I'm trying to connect to my web service:
in ParsingHelper.cs:
using System;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.Collections.ObjectModel;
namespace MyWP7App
{
[XmlRoot("root")]
public class Categories
{
[XmlArray("Categories")]
[XmlArrayItem("Category")]
public ObservableCollection<Category> Collection { get; set; }
}
}
public class Category
{
[XmlAttribute("ID")]
public int ID { get; set; }
[XmlAttribute("SubCategories")]
public int SubCategoriesCount { get; set; }
[XmlElement("Name")]
public string Name { get; set; }
[XmlArray("SubCategories")]
[XmlArrayItem("SubCategory")]
public ObservableCollection<SubCategory> Collection { get; set; }
}
in MyPage.xaml.cs:
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Serialization;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
namespace MyWP7App
{
public partial class CategoriesPage : PhoneApplicationPage
{
private ObservableCollection<Category> itemsSource;
public ObservableCollection<Category> ItemsSource
{
get
{
return this.itemsSource;
}
set
{
this.itemsSource = value;
}
}
private static MyService.MobileServiceSoapClient Service = null;
public PanoramaMainPage()
{
InitializeComponent();
if (null == ItemsSource)
GetCategories();
else
imtListBox.ItemsSource = this.ItemsSource;
}
private void GetCategories()
{
Service = new MyService.MobileServiceSoapClient();
// I tried to do the following when the service is secure, but I had the same error:
// Service.ClientCredentials.UserName.UserName = "Username";
// Service.ClientCredentials.UserName.Password = "Password";
Service.GetCategoriesCompleted += new EventHandler<MyService.GetCategoriesCompletedEventArgs>(Service_GetCategoriesCompleted);
Service.GetCategoriesAsync();
}
void Service_GetCategoriesCompleted(object sender, MyService.GetCategoriesCompletedEventArgs e)
{
try
{
if (e.Result == null || e.Error != null)
{
MessageBox.Show("There was an error downloading the XML-file!");
}
if (!e.Cancelled)
{
XmlSerializer serializer = new XmlSerializer(typeof(Categories));
XDocument document = XDocument.Parse("<root>" + e.Result.ToString() + "</root>");
Categories arts = new Categories();
arts = (Categories)serializer.Deserialize(document.CreateReader());
this.ItemsSourceListBox = arts.Collection;
imtListBox.ItemsSource = this.Items1Source;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Service.Abort();
}
}
Upvotes: 0
Views: 1553
Reputation: 3046
have a look at this post http://msdn.microsoft.com/en-us/library/ff637320(VS.96).aspx
This post details how to pass username / password. Here's a snippet from the page
using (OperationContextScope contextScope = new OperationContextScope(svc.InnerChannel))
{
byte[] bc = System.Text.Encoding.UTF8.GetBytes(@"username" + ":" + "password"); // the string passed after basic:
HttpRequestMessageProperty httpProps = new HttpRequestMessageProperty();
httpProps.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(bc);
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpProps;
// call the service.
}
Upvotes: 1