我正在使用Silverlight的MVVM模式。
我有这些项目: -
MVVMDemo.Data(Ria服务链接设置为MVVMDemo.Data.Web) MVVMDemo.Data.Web(包含所有RIA服务) MVVMDemo.Data.ViewModels MVVMDemo.Data.Models MVVMDemo(包含所有视图) MVVMDemo.Data.Common(包含所有接口)
这是我的RIA服务类: -
namespace MVVMDemo.Data.Web
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.ServiceModel.DomainServices.EntityFramework;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
[EnableClientAccess()]
public class UserServices : LinqToEntitiesDomainService<IssueVisionEntities>
{
public IQueryable<User> GetUsersByLimit(int skip , int take)
{
return this.ObjectContext.Users.Skip(skip).Take(take);
}
public IQueryable<User> GetUsers()
{
return this.ObjectContext.Users;
}
public void InsertUser(User user)
{
if ((user.EntityState != EntityState.Detached))
{
this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
}
else
{
this.ObjectContext.Users.AddObject(user);
}
}
public void UpdateUser(User currentUser)
{
this.ObjectContext.Users.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
}
public void DeleteUser(User user)
{
if ((user.EntityState == EntityState.Detached))
{
this.ObjectContext.Users.Attach(user);
}
this.ObjectContext.Users.DeleteObject(user);
}
}
}
这是我称之为RIA服务的模型: -
namespace MVVMDemo.Models
{
[Export(typeof(IUserModel))]
public class UserModel : IUserModel
{
private const int PAGESIZE = 10;
private int skip = 0;
private UserServices _context;
private UserServices Context
{
get
{
if (_context == null)
{
_context = new UserServices();
}
return _context;
}
}
public void GetAllUsersAysnc()
{
PerformQuery<User>(Context.GetUsersQuery(), GetAllUsersAsyncComplete);
}
private void PerformQuery<T>(EntityQuery<T> query , EventHandler<EntityResultArgs<T>> eventHandler) where T:Entity
{
this.Context.Load<T>(query, LoadBehavior.RefreshCurrent, r =>
{
if (eventHandler != null)
{
try
{
if (r.HasError)
{
eventHandler(this, new EntityResultArgs<T>(r.Error));
r.MarkErrorAsHandled();
}
else
eventHandler(this, new EntityResultArgs<T>(r.Entities));
}
catch (Exception ex)
{
eventHandler(this, new EntityResultArgs<T>(ex));
}
}
},null);
}
public event EventHandler<EntityResultArgs<Data.Web.User>> GetAllUsersAsyncComplete;
public void GetAllUsersByLimit()
{
PerformQuery<User>(Context.GetUsersByLimitQuery(skip,PAGESIZE), GetAllUsersByLimitAsyncComplete);
}
public event EventHandler<EntityResultArgs<Data.Web.User>> GetAllUsersByLimitAsyncComplete;
}
每当我运行方法GetAllUsersAysnc
时,我总会收到此错误: -
http://img408.imageshack.us/img408/6150/62968542.jpg
这是我在MVVMDemo.Data.Web中的app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add name="DomainServiceModule" preCondition="managedHandler"
type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
<connectionStrings>
<add name="IssueVisionEntities" connectionString="metadata=res://*/IssueVisionModel.csdl|res://*/IssueVisionModel.ssdl|res://*/IssueVisionModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=TAPESH\MSSQLSERVER2008;Initial Catalog=IssueVision;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.web>
<httpModules>
<add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</httpModules>
</system.web>
</configuration>
这是我托管Silverlight应用程序的Asp.Net网站的web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
我不知道发生了什么,我是RIA服务的初学者。请帮助,我在我的智慧结束。
提前致谢:)