积极答复者
Silverlight+WCF+Linq碰到一个问题,画面启动时报错:“用户代码未处理 KeyNotFoundException”

问题
答案
全部回复
-
1.你的服务是ria serivces还是原生的wcf?
2.查看代理类和config文件是否都正常生成报相应配置。
3.重新引用服务。
版主你好,我正好是在看您写的Silverlight3.0中的例子,13.8:实例:以LINQ、WCF、与DataGrid控件制作数据管理系统。是原生的WCF服务。服务器启动没有问题,在WCF 测试客户端中能看到GetUsers方法,点击调用能看到抽出数据。
以下是App。config代码
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> </configSections> <connectionStrings> <add name="WCFService.Properties.Settings.SampleDBConnectionString" connectionString="Data Source=LYH;Initial Catalog=SampleDB;Persist Security Info=True;User ID=sa;Password=manager" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation debug="true" /> </system.web> <!-- 部署服务库项目时,必须将配置文件的内容添加到 主机的 app.config 文件中。System.Configuration 不支持库的配置文件。--> <system.serviceModel> <services> <service name="WCFService.Service1"> <host> <baseAddresses> <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCFService/Service1/" /> </baseAddresses> </host> <!-- Service Endpoints --> <!-- 除非完全限定,否则地址将与上面提供的基址相关 --> <endpoint address="" binding="wsHttpBinding" contract="WCFService.IService1"> <!-- 部署时,应删除或替换下列标识元素,以反映 用来运行所部署服务的标识。删除之后,WCF 将 自动推断相应标识。 --> <identity> <dns value="localhost"/> </identity> </endpoint> <!-- Metadata Endpoints --> <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。 --> <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除--> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> <!-- 为避免泄漏元数据信息, 请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> <serviceMetadata httpGetEnabled="True"/> <!-- 要接收故障异常详细信息以进行调试, 请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息--> <serviceDebug includeExceptionDetailInFaults="False" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration>
以下是SampleDB.dbml代码
#pragma warning disable 1591 //------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:4.0.30319.1 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ namespace WCFService { using System.Data.Linq; using System.Data.Linq.Mapping; using System.Data; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; using System.ComponentModel; using System; using System.Runtime.Serialization; [DataContract] [global::System.Data.Linq.Mapping.DatabaseAttribute(Name="SampleDB")] public partial class SampleDBDataContext : System.Data.Linq.DataContext { private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource(); #region 可扩展性方法定义 partial void OnCreated(); partial void Insertuser(user instance); partial void Updateuser(user instance); partial void Deleteuser(user instance); #endregion public SampleDBDataContext() : base(global::WCFService.Properties.Settings.Default.SampleDBConnectionString, mappingSource) { OnCreated(); } public SampleDBDataContext(string connection) : base(connection, mappingSource) { OnCreated(); } public SampleDBDataContext(System.Data.IDbConnection connection) : base(connection, mappingSource) { OnCreated(); } public SampleDBDataContext(string connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } public SampleDBDataContext(System.Data.IDbConnection connection, System.Data.Linq.Mapping.MappingSource mappingSource) : base(connection, mappingSource) { OnCreated(); } [DataMember] public System.Data.Linq.Table<user> users { get { return this.GetTable<user>(); } } } [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.users")] public partial class user : INotifyPropertyChanging, INotifyPropertyChanged { private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty); private int _ID; private string _UserName; private System.Nullable<int> _Age; private string _Logo; private string _Company; private System.Nullable<System.DateTime> _CreateTime; private System.Nullable<bool> _IsActive; private string _Special; private string _Url; #region 可扩展性方法定义 partial void OnLoaded(); partial void OnValidate(System.Data.Linq.ChangeAction action); partial void OnCreated(); partial void OnIDChanging(int value); partial void OnIDChanged(); partial void OnUserNameChanging(string value); partial void OnUserNameChanged(); partial void OnAgeChanging(System.Nullable<int> value); partial void OnAgeChanged(); partial void OnLogoChanging(string value); partial void OnLogoChanged(); partial void OnCompanyChanging(string value); partial void OnCompanyChanged(); partial void OnCreateTimeChanging(System.Nullable<System.DateTime> value); partial void OnCreateTimeChanged(); partial void OnIsActiveChanging(System.Nullable<bool> value); partial void OnIsActiveChanged(); partial void OnSpecialChanging(string value); partial void OnSpecialChanged(); partial void OnUrlChanging(string value); partial void OnUrlChanged(); #endregion public user() { OnCreated(); } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_ID", DbType="Int NOT NULL", IsPrimaryKey=true)] public int ID { get { return this._ID; } set { if ((this._ID != value)) { this.OnIDChanging(value); this.SendPropertyChanging(); this._ID = value; this.SendPropertyChanged("ID"); this.OnIDChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_UserName", DbType="VarChar(150)")] public string UserName { get { return this._UserName; } set { if ((this._UserName != value)) { this.OnUserNameChanging(value); this.SendPropertyChanging(); this._UserName = value; this.SendPropertyChanged("UserName"); this.OnUserNameChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Age", DbType="Int")] public System.Nullable<int> Age { get { return this._Age; } set { if ((this._Age != value)) { this.OnAgeChanging(value); this.SendPropertyChanging(); this._Age = value; this.SendPropertyChanged("Age"); this.OnAgeChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Logo", DbType="VarChar(200)")] public string Logo { get { return this._Logo; } set { if ((this._Logo != value)) { this.OnLogoChanging(value); this.SendPropertyChanging(); this._Logo = value; this.SendPropertyChanged("Logo"); this.OnLogoChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Company", DbType="VarChar(250)")] public string Company { get { return this._Company; } set { if ((this._Company != value)) { this.OnCompanyChanging(value); this.SendPropertyChanging(); this._Company = value; this.SendPropertyChanged("Company"); this.OnCompanyChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CreateTime", DbType="DateTime")] public System.Nullable<System.DateTime> CreateTime { get { return this._CreateTime; } set { if ((this._CreateTime != value)) { this.OnCreateTimeChanging(value); this.SendPropertyChanging(); this._CreateTime = value; this.SendPropertyChanged("CreateTime"); this.OnCreateTimeChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_IsActive", DbType="Bit")] public System.Nullable<bool> IsActive { get { return this._IsActive; } set { if ((this._IsActive != value)) { this.OnIsActiveChanging(value); this.SendPropertyChanging(); this._IsActive = value; this.SendPropertyChanged("IsActive"); this.OnIsActiveChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Special", DbType="VarChar(250)")] public string Special { get { return this._Special; } set { if ((this._Special != value)) { this.OnSpecialChanging(value); this.SendPropertyChanging(); this._Special = value; this.SendPropertyChanged("Special"); this.OnSpecialChanged(); } } } [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_Url", DbType="VarChar(250)")] public string Url { get { return this._Url; } set { if ((this._Url != value)) { this.OnUrlChanging(value); this.SendPropertyChanging(); this._Url = value; this.SendPropertyChanged("Url"); this.OnUrlChanged(); } } } public event PropertyChangingEventHandler PropertyChanging; public event PropertyChangedEventHandler PropertyChanged; protected virtual void SendPropertyChanging() { if ((this.PropertyChanging != null)) { this.PropertyChanging(this, emptyChangingEventArgs); } } protected virtual void SendPropertyChanged(String propertyName) { if ((this.PropertyChanged != null)) { this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } } #pragma warning restore 1591
我网上查到,需要启用Silverlight的WCF服务程序。但是不明白怎么弄
我现在使用的VS2010,在解决方案管理器Silverlight项目名上右键新建项出来的菜单中,在已安装的Web模板中,没有service模块。在联机模块中也没有看到类似的Service模块。
-
你那个错还在吗?方便把项目发来看看么?