0
  1. 我创建了一个网络服务“http://service.mydomain.com/MobileService.asmx”
  2. 我创建了一个 Windows 移动应用程序
  3. 我添加了对此 Web 服务的引用
  4. 我开始编码并完成了 WP7 应用程序并部署了 we 服务
  5. 然后我使用 IIS 7.5 禁用匿名身份验证并使用基本身份验证来保护我的 Web 服务。
  6. 使用基本身份验证后,我添加了对我的服务的新引用,VS 2010 询问身份验证,我使用了我的用户名和密码
  7. 然后,当我尝试使用新服务时,我遇到了异常。

    Reference.cs 上的错误:

    公共 System.IAsyncResult BeginGetArticle(MyWP7App.MyService.GetArticleRequest 请求,System.AsyncCallback 回调,对象 asyncState) { object[] _args = new object[1]; _args[0] = 请求;System.IAsyncResult _result = base.BeginInvoke("GetArticle", _args, callback, asyncState); 返回_结果;}


CommunicationException:{“远程服务器返回错误:NotFound。”}
状态描述:未经授权

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)



这就是我尝试连接到我的网络服务的方式:
在 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; }
    }


在 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();
        }
    }


4

1 回答 1

1

看看这篇文章 http://msdn.microsoft.com/en-us/library/ff637320(VS.96).aspx

这篇文章详细介绍了如何传递用户名/密码。这是页面中的一个片段

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.
}
于 2012-05-23T10:19:42.537 回答