Xamarin.Forms调用AX2012 R3 WebService抛出NotImplementedException求助
Xamarin.Forms调用AX2012 R3 WebService抛出System.NotImplementedException的解决建议
1. 改用异步方法调用(最常见原因)
Xamarin平台下,添加ASMX服务引用时默认优先生成基于任务的异步方法,同步方法(如GetItemName)可能仅生成空实现并抛出NotImplementedException。而桌面C#项目允许同步调用,因此不会触发该问题。
修改你的Xamarin代码为异步调用:
// 方法标记为async private async void Button_Clicked(object sender, EventArgs e) { string itemName = ""; Debug.WriteLine("Start"); ASMXService.TestWebserviceClient client = new ASMXService.TestWebserviceClient(); ASMXService.CallContext cpny = new ASMXService.CallContext() { Company = "USMF" }; client.ClientCredentials.Windows.ClientCredential.UserName = @"sagcn\wangy"; client.ClientCredentials.Windows.ClientCredential.Password = "aoc-111"; // 使用异步方法并await itemName = await client.GetItemNameAsync(cpny, "C0004"); Debug.WriteLine($"Item Name: {itemName}"); Debug.WriteLine("End"); }
若需保留同步调用,可在服务引用配置中重新生成同步方法:
- 右键项目中的服务引用 → 选择「配置服务引用」
- 勾选「生成同步操作」选项,点击确定后重新生成代理类
2. 检查并修正绑定配置
Xamarin项目默认的服务绑定配置可能与桌面C#项目不一致,导致身份验证或通信失败,异常被包装为NotImplementedException。手动配置绑定确保与桌面端匹配:
private async void Button_Clicked(object sender, EventArgs e) { string itemName = ""; Debug.WriteLine("Start"); // 手动创建BasicHttpBinding,匹配Windows身份验证配置 var binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly; binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows; // 指定AX WebService的Endpoint地址 var endpoint = new EndpointAddress("http://你的AX服务器地址/TestWebservice.asmx"); ASMXService.TestWebserviceClient client = new ASMXService.TestWebserviceClient(binding, endpoint); ASMXService.CallContext cpny = new ASMXService.CallContext() { Company = "USMF" }; client.ClientCredentials.Windows.ClientCredential.UserName = @"sagcn\wangy"; client.ClientCredentials.Windows.ClientCredential.Password = "aoc-111"; itemName = await client.GetItemNameAsync(cpny, "C0004"); Debug.WriteLine($"Item Name: {itemName}"); Debug.WriteLine("End"); }
3. 处理安卓平台的特殊限制
- 明文HTTP访问:如果AX WebService使用HTTP协议(非HTTPS),安卓9及以上版本默认禁止明文流量,需在
AndroidManifest.xml中添加配置:<application android:usesCleartextTraffic="true"> </application> - NTLM身份验证支持:安卓平台对NTLM的支持需要确保HttpClientHandler启用相关设置,可在创建客户端时指定超时并验证凭据配置:
client.Endpoint.Binding.SendTimeout = TimeSpan.FromSeconds(30);
4. 重新生成服务引用
如果上述方法无效,删除现有服务引用,重新添加:
- 右键项目 → 「添加」→ 「服务引用」
- 输入AX WebService的WSDL地址(如
http://你的AX服务器地址/TestWebservice.asmx?wsdl) - 确保勾选「生成基于任务的异步操作」,完成添加
内容的提问来源于stack exchange,提问作者CFSR
相关产品推荐
相关产品推荐

