Windows phone 7 wp7中发生KeyNotFoundException
我创建了用于检查用户名和密码的WCF Web服务,并使用MYSQL数据库检查用户名和密码是否存在。我在WP7中创建了一个应用程序,其中有两个文本框,分别是用户名、密码和登录按钮。请告诉我原因Windows phone 7 wp7中发生KeyNotFoundException,windows-phone-7,Windows Phone 7,我创建了用于检查用户名和密码的WCF Web服务,并使用MYSQL数据库检查用户名和密码是否存在。我在WP7中创建了一个应用程序,其中有两个文本框,分别是用户名、密码和登录按钮。请告诉我原因 我的代码在这里 using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using Sy
我的代码在这里
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using consumeWCFwp7DB.ServiceReference1;
namespace consumeWCFwp7DB
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
private void btnadd(object sender, RoutedEventArgs e)
{
ServiceReference1.ServiceClient obj = new ServiceReference1.ServiceClient();
string username = txtusername.Text.ToString();
string password = txtpassword.Text.ToString();
obj.loginAsync(username, password);
obj.loginCompleted+=new EventHandler<loginCompletedEventArgs> (obj_loginCompleted);
}
void obj_loginCompleted(object sender, loginCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show(e.ToString());
}
else
{
MessageBox.Show("Error");
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Shapes;
使用Microsoft.Phone.Controls;
使用ConsumerWCFWP7DB.ServiceReference1;
命名空间ConsumerWCFWP7DB
{
公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
}
私有void btnadd(对象发送方,路由目标)
{
ServiceReference1.ServiceClient obj=新的ServiceReference1.ServiceClient();
字符串username=txtusername.Text.ToString();
字符串密码=txtpassword.Text.ToString();
对象loginAsync(用户名、密码);
obj.loginCompleted+=新事件处理程序(obj_loginCompleted);
}
void obj_loginCompleted(对象发送方,loginCompletedEventArgs e)
{
如果(e.Error==null)
{
Show(例如ToString());
}
其他的
{
MessageBox.Show(“错误”);
}
}
}
}
异常在Reference.cs中,这意味着抛出错误的是WCF web服务,而不是电话应用程序。
我建议您在运行服务时调试服务,找出抛出错误的位置,并相应地修复/捕获错误
编辑:
刚刚注意到你在ASP.NET应用程序中对它的评论。这可能意味着没有正确接收密钥。这也意味着您没有处理错误。您应该仔细检查服务的绑定设置。我相信您只为它配置了
WSHttpBinding
。但是silverlight和windows phone 7不支持WSHttpBinding
。Windows Phone 7支持的唯一绑定是BasicHttpBinding
,因此您应该使用BasicHttpBinding
配置您的服务,然后客户端调用就可以工作了
KeyNotFoundException
是由于默认绑定(BasicHttpBinding
)不存在于服务的绑定列表中(仅在服务中找到WSHttpBinding
)。解决方案
您可以在服务中修改文件Web.config:
<endpoint address=""
binding="basicHttpBinding"
contract="WcfServiceForWinMobile.IWin7MobileService"/>
在哪一行引发异常?此代码没有任何内容是使用密钥访问集合的,我们缺少一个重要部分Hello KooKiz我添加了出现异常的图像如果我在asp.net中使用相同的web服务,此异常将发生在Reference.cs文件中,工作正常。wp7中会出现异常。有人能帮我吗。。。