0
  1. Webサービス「http://service.mydomain.com/MobileService.asmx」を作成しました
  2. Windowsモバイルアプリケーションを作成しました
  3. このWebサービスへの参照を追加しました
  4. コーディングを開始してWP7アプリケーションを終了し、サービスを展開しました
  5. 次に、IIS 7.5を使用して匿名認証を無効にし、基本認証を使用してWebサービスを保護しました。
  6. 基本認証を使用した後、サービスに新しい参照を追加し、VS 2010が認証について質問した後、ユーザー名とパスワードを使用しました
  7. その後、新しいサービスを使おうとすると、例外が発生しました。

    Reference.csのエラー:

    public System.IAsyncResult BeginGetArticle(MyWP7App.MyService.GetArticleRequest request、System.AsyncCallback callback、object asyncState){object [] _args = new object [1]; _args[0]=リクエスト; System.IAsyncResult _result = base.BeginInvoke( "GetArticle"、_args、callback、asyncState); return _result; }


CommunicationException:{"リモートサーバーがエラーを返しました: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)



これが私のWebサービスに接続しようとしている方法です:
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 に答える