WCF 数据服务显示未处理异常的错误

WCF Data Services showing error of unhandled exception

我是 运行 一个非常简单的程序,我有 ASP.NET 项目,我在其中添加了 WcfDataService1 文件,文件名为 WcfDataService1.svc,如下所示:

namespace WebApplication
{
    public class WcfDataService : EntityFrameworkDataService<SampleDatabaseEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
            // Examples:
            // config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
            // config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }
    }
}

我还检查了我的视图标记:

<%@ ServiceHost Language="C#" Debug="true" Factory="System.ServiceModel.Activation.WebServiceHostFactory, System.ServiceModel.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Service="WebApplication.WcfDataService" CodeBehind="WcfDataService.svc.cs" %>

但仍然显示以下错误:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: The type 'WebApplication.WcfDataService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

更新:

我重新启动电脑,然后它显示以下问题:

Could not load file or assembly 'Microsoft.Data.Services, Version=5.6.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

我尝试了很多方法,但仍然显示错误。

I checked in my C:\Program Files\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics there are 5.6.2 version dlls are there, I replaced them with 5.6.4 but still getting the same error.

也许您尝试卸载然后重新安装软件包? 你可以

  • 清理你的解决方案
  • 删除所有obj和bin目录
  • 从整个解决方案中卸载此包
  • 确保在 GAC 中删除此文件:&userprofile%.nuget\packages\microsoft.data.services - 确保删除整个目录
  • 现在您可以从 NuGet 安装所需的版本