WebAPI:无法加载一个或多个请求的类型

时间:2014-12-25 08:00:06

标签: c# entity-framework silverlight asp.net-web-api

我正在使用Silverlight开发应用程序。我在我的解决方案中创建了WebAPI项目:

BusinessApplication8
BusinessApplication8.Web
BusinessApplication8.WebAPI

第一个是Silverlight项目,第二个包含我的EDMX文件。在我的WebAPI项目中,我使用我的DbContext:

public IEnumerable <object> Get() 
{
    var database = new DABIREntities();
    var query = database.noe_nameh.Take(1).Select(item = > new {
        Id = item.uid,
        Description = item.sharh
    }).ToList();
    return query;
}

在本地主机中一切正常,我在浏览器中得到结果。当我发布我的WebAPI项目并通过URL调用该GET方法时,我收到此错误:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
    Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
    </ExceptionMessage>
    <ExceptionType>System.Reflection.ReflectionTypeLoadException</ExceptionType>
    <StackTrace>
    at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeModule.GetTypes() at System.Reflection.Assembly.GetTypes() at
 System.Data.Metadata.Edm.ObjectItemAttributeAssemblyLoader.LoadTypesFromAssembly() at System.Data.Metadata.Edm.ObjectItemAssemblyLoader.Load() at 
System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, ObjectItemLoadingSessionData loadingData) at 
System.Data.Metadata.Edm.AssemblyCache.LoadAssembly(Assembly assembly, Boolean loadReferencedAssemblies, KnownAssembliesSet knownAssemblies, EdmItemCollection 
edmItemCollection, Action`1 logLoadMessage, Object& loaderCookie, Dictionary`2& typesInLoading, List`1& errors) at 
System.Data.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(ObjectItemCollection objectItemCollection, Assembly assembly, Boolean loadReferencedAssemblies, 
EdmItemCollection edmItemCollection, Action`1 logLoadMessage) at System.Data.Metadata.Edm.ObjectItemCollection.ImplicitLoadAssemblyForType(Type type, EdmItemCollection 
edmItemCollection) at System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadAssemblyForType(Type type, Assembly callingAssembly) at 
System.Data.Objects.ObjectContext.GetTypeUsage(Type entityCLRType) at System.Data.Objects.ObjectContext.CreateObjectSet[TEntity](String entitySetName) at 
BusinessApplication8.Web.DABIREntities.get_noe_nameh() at BusinessApplication8.WebAPI.Controllers.SearchController.Get() at lambda_method(Closure , Object , Object[] ) 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) at 
System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken 
cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task 
task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at
 System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at 
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at 
System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at 
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()

    </StackTrace>
</Error>

任何想法?

1 个答案:

答案 0 :(得分:0)

最后我解决了我的问题。首先,我将API方法更改为:

public IEnumerable<object> Get()
        {
            try
            {
                var database = new DABIREntities();
                var query = database.noe_nameh.Take(1).Select(item => new
                {
                    Id = item.uid,
                    Description = item.sharh
                }).ToList();
                return query;
            }
            catch (ReflectionTypeLoadException exception)
            {
                return exception.LoaderExceptions.Select(item => item.Message).ToList();
            }
        }

这帮助我弄清了我的确切例外情况。我们应该将Copy to Local设置为以下dll:

System.ServiceModel
System.ServiceModel.DomainServices.EntityFramework
System.ServiceModel.DomainServices.Hosting
System.ServiceModel.DomainServices.Server