WinRT应用程序使用NAV Web服务并获得此消息

时间:2013-09-23 09:56:52

标签: microsoft-dynamics dynamics-nav

我做了以下操作并得到了以下错误消息:

错误消息:

mscorlib.dll中出现'System.AggregateException'类型的异常,但未在用户代码中处理 附加信息:发生一个或多个错误。 如果存在此异常的处理程序,则可以安全地继续该程序。

问题:

a)上面的代码中出现的问题似乎只是我想要检索一条记录。

b)必须在WinRT或Windows应用商店中使用异步方法吗?

c)以下代码是否能够从Navision中检索记录?


-----1-------  Windows store App to access Nav Web Services

1.1 Added the service reference  in WinRT App 
1.2 Added a class1.cs in WinRT App


private async void btnImportCustomer_Click(object sender, RoutedEventArgs e)
{
    Task _asyncCustomer = Class1.Customer.Listing.GetAsyncRecords("Y007");



    ###  encounterd error here: ####

   string  g_strmsg = _asyncCustomer.Result.No + " “ +_asyncCustomer.Result.Name;

}


-----2----------     Class1.cs use inside WinRT App Project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileNAVSalesSystem
{
    class Class1
    {
        public static string _webserviceurlpage = "http ://{0}:{1}/{2}/WS/{3}/Page/{4}";
        public static string _webserviceurlcodeunit = "http://{0}:{1}/{2}/WS/{3}/Codeunit/{4}";
        public static Uri _webserviceuripage = null;
        public static Uri _webserviceuricodeunit = null;


  #region Customer

        public class Customer
        {
            public class Card
            {
                //Do something for Card Type
            }

            public class Listing
            {
                public static wsCustomerList.Customer_List_PortClient GetService()
                {
                    _webserviceuripage = new Uri(string.Format(_webserviceurlpage, "msxxx", "7047", "DynamicsNAV_xxx", Uri.EscapeDataString("Global xxx  Pte. Ltd."), "Customer List"));

                    System.ServiceModel.BasicHttpBinding _wSBinding = new System.ServiceModel.BasicHttpBinding();
                    _wSBinding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.TransportCredentialOnly;
                    _wSBinding.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
                    _wSBinding.MaxBufferSize = Int32.MaxValue;
                    _wSBinding.MaxReceivedMessageSize = Int32.MaxValue;

                    //_wSBinding.UseDefaultWebProxy = false;

                    wsCustomerList.Customer_List_PortClient _ws = new wsCustomerList.Customer_List_PortClient(_wSBinding, new System.ServiceModel.EndpointAddress(_webserviceuripage));
                    _ws.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
                    _ws.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential("xxx","xxxx", "companyName");
                    return _ws;
                }



   //-------------------------- Using Async Methods 

     public static async Task GetAsyncRecords(string _No)
     {
             wsCustomerList.Customer_List_PortClient _ws = GetService();
             wsCustomerList.Customer_List _List = (await _ws.ReadAsync(_No)).Customer_List;
             if (_ws.State == System.ServiceModel.CommunicationState.Opened)
                        await _ws.CloseAsync();
                    return _List;
      }

  public static async Task GetAsyncRecords(wsCustomerList.Customer_List_Filter[] _filters)
     {
            wsCustomerList.Customer_List_PortClient _ws = GetService();
             wsCustomerList.Customer_List[] _List;
             List _filterArray = new List();
                    _filterArray.AddRange(_filters);
                    _List = (await _ws.ReadMultipleAsync(_filterArray.ToArray(), null, 0)).ReadMultiple_Result1;
             if (_ws.State == System.ServiceModel.CommunicationState.Opened)
                        await _ws.CloseAsync();
                       return _List;
                }

  public static async Task GetAsyncRecords(wsCustomerList.Customer_List_Filter[] _filters, string _bookmarkkey)
     {
        wsCustomerList.Customer_List_PortClient _ws = GetService();
        wsCustomerList.Customer_List[] _List;
                    List _filterArray = new List();
        _filterArray.AddRange(_filters);
       _List = (await _ws.ReadMultipleAsync(_filterArray.ToArray(), _bookmarkkey, 0)).ReadMultiple_Result1;
        if (_ws.State == System.ServiceModel.CommunicationState.Opened)
                  await _ws.CloseAsync();
                return _List;
        }

  public static async Task GetAsyncRecords(wsCustomerList.Customer_List_Filter[] _filters, string _bookmarkkey, int _setsize)
    {
           wsCustomerList.Customer_List_PortClient _ws = GetService();
           wsCustomerList.Customer_List[] _List;
            List _filterArray = new List();
                    _filterArray.AddRange(_filters);
           _List = (await _ws.ReadMultipleAsync(_filterArray.ToArray(), _bookmarkkey, _setsize)).ReadMultiple_Result1;
            if (_ws.State == System.ServiceModel.CommunicationState.Opened)
                        await _ws.CloseAsync();
                    return _List;
                }

            }
        }

        #endregion
    }
//--- end namespace
}

1 个答案:

答案 0 :(得分:0)

我知道这是一段时间以前发布的这个问题,但其他人可能会偶然发现它,所以这里有:

a)上述代码中出现的问题似乎只是为了检索记录。

您的返回类型似乎不正确。

b)必须在WinRT或Windows商店应用中使用异步方法吗?

是的,当使用Windows移动平台(Windows应用商店和Windows手机应用)时,您必须使用异步调用。

c)以下代码能否从Navision中检索记录?

很难说,但对我而言,您尝试检索的数据似乎格式不正确。我会从我当前检索登录的一个项目中给出一个简单的例子:

    private async void Button_Click(object sender, RoutedEventArgs e)
    {
        await call();
    }

    private async Task call()
    {
        BasicHttpBinding binding = new BasicHttpBinding();
        NetworkCredential cred = new NetworkCredential("username", "password", "domain");
        WS_PortClient ws = new WS_PortClient(binding, new EndpointAddress("Webservice-URL"));

        binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
        ws.ClientCredentials.Windows.ClientCredential = cred;

        CheckLogin_Result s = await ws.CheckLoginAsync("parameter");
        string k = s.return_value.ToString();
        MessageDialog d = new MessageDialog(k, "message");
        await d.ShowAsync();
    }

希望它有所帮助!