神奇的数字在gzip头信息是不正确的。请确保您在一个gzip流过 [英] The magic number in GZip header is not correct. Make sure you are passing in a GZip stream

查看:816
本文介绍了神奇的数字在gzip头信息是不正确的。请确保您在一个gzip流过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索产品名称和使用的WebAPI作为NameID的对象,它的IDS从的EntityFramework。 code,对于它是如下。

 公共类ProductController的:ApiController
{
    保护MainDataContext DB =新MainDataContext();
    // GET / API /值
    公众的IQueryable< NameID的>得到()
    {
        返回db.Products.Select(X =>新建NameID的{ID = x.ID,名称= x.Name})AsQueryable已()。
    }    // GET / API /价值/ 5
    公共NameID的获取(长ID)
    {
        VAR的结果= db.Products.Select(X =>新建NameID的{ID = x.ID,名称= x.Name})的SingleOrDefault(X => x.ID == ID);
        如果(ID == 0 ||结果== NULL)
            抛出新的Htt presponseException(的HTTPStatus code.NotFound);
        返回结果;
    }}
公共类NameID的{
    众长ID {获取;设置;}
    公共字符串名称{;设置;}
}

它会抛出错误如下

 幻数在gzip头信息是不正确的。
请确保您传递一个gzip流。在System.IO.Com pression.GZipDe coder.ReadHeader(INPUTBUFFER输入)
在System.IO.Com pression.Inflater.De code()
在System.IO.Com pression.Inflater.Inflate(字节[]字节,偏移的Int32,的Int32长度)
在System.IO.Com pression.DeflateStream.Read(byte []数组,偏移的Int32,的Int32计数)
在System.IO.Com pression.GZipStream.Read(byte []数组,偏移的Int32,的Int32计数)
在System.Xml.XmlTextReaderImpl.InitStreamInput(URI基本URI,字符串baseUriStr,涧流,字节[]字节的Int32 BYTECOUNT,编码编码)
在System.Xml.XmlTextReaderImpl..ctor(流流,字节[]字节的Int32 BYTECOUNT,XmlReaderSettings设置,开放的基本URI,字符串baseUriStr,XmlParserContext背景下,布尔closeInput)
在System.Xml.XmlReaderSettings.CreateReader(流输入,乌里基本URI,字符串baseUriString,XmlParserContext inputContext)
在System.Xml.XmlReader.Create(流输入,XmlReaderSettings设置字符串的基本URI)
在System.Xml.Linq.XDocument.Load(流流,LoadOptions选项)
在System.Data.Entity.Migrations.Edm.ModelCom pressor.Decom preSS(字节[]字节)
在System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel(字符串&安培; migrationId)
在System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel()
在System.Data.Entity.Internal.InternalContext.QueryForModel()
在System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext,ModelHashCalculator modelHashCalculator,布尔throwIfNoMetadata)
在System.Data.Entity.Internal.InternalContext.CompatibleWithModel(布尔throwIfNoMetadata)
在System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext上下文)
在System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6()
在System.Data.Entity.Internal.InternalContext.PerformInitializationAction(动作动作)
在System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
在System.Data.Entity.Internal.LazyInternalContext&LT; InitializeDatabase&GT; b__4(InternalContext C)
在System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput输入)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1动作)
在System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(类型的EntityType)
在System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
在System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
在System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
在System.Linq.Queryable.Select [TSource,TResult](IQueryable`1源,防爆pression`1选择器)
在ProductAPI.Controllers.ProductController.Get()在D:\\演示\\ ProductAPI \\ \\控制器ProductController.cs:24行
在lambda_method(封闭,对象,对象[])
在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object实例,对象[]参数)
在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.Execute(HttpControllerContext controllerContext,IDictionary`2参数)
在System.Web.Http.Controllers.ApiControllerActionInvoker.<>c__DisplayClass2.<InvokeActionAsync>b__0()
在System.Threading.Tasks.TaskHelpers.RunSynchronously [TResult](Func`1 FUNC键的CancellationToken的CancellationToken)


解决方案

堆栈跟踪似乎预示着有从数据库中读取实体框架模型元数据的问题。

HistoryRepository.GetLastModel 要求 ModelCom pressor.Decom preSS 使用 XDocument.Load 来读取从 GZipStream 阅读一些XML。这将失败,并在数据库中的模型元数据是最有可能损坏。

您可以尝试重新创建数据库来解决这个问题。

I am trying to retrieve product name and its ids as NameID object from EntityFramework using WebAPI. code for which is as following.

public class ProductController : ApiController
{
    protected MainDataContext db = new MainDataContext();
    // GET /api/values
    public IQueryable<NameID> Get()
    {
        return db.Products.Select(x=>new NameID{ ID=x.ID,Name=x.Name }).AsQueryable();
    }

    // GET /api/values/5
    public NameID Get(long id)
    {
        var result = db.Products.Select(x=>new NameID{ ID=x.ID,Name=x.Name }).SingleOrDefault(x => x.ID == id);
        if (id == 0 || result == null)
            throw new HttpResponseException(HttpStatusCode.NotFound); 
        return result;
    }

}
public class NameID {
    public long ID {get;set;}
    public string Name {get;set;}
}

It throws error as following

The magic number in GZip header is not correct.
Make sure you are passing in a GZip stream.

at System.IO.Compression.GZipDecoder.ReadHeader(InputBuffer input) 
at System.IO.Compression.Inflater.Decode() 
at System.IO.Compression.Inflater.Inflate(Byte[] bytes, Int32 offset, Int32 length) 
at System.IO.Compression.DeflateStream.Read(Byte[] array, Int32 offset, Int32 count) 
at System.IO.Compression.GZipStream.Read(Byte[] array, Int32 offset, Int32 count) 
at System.Xml.XmlTextReaderImpl.InitStreamInput(Uri baseUri, String baseUriStr, Stream stream, Byte[] bytes, Int32 byteCount, Encoding encoding) 
at System.Xml.XmlTextReaderImpl..ctor(Stream stream, Byte[] bytes, Int32 byteCount, XmlReaderSettings settings, Uri baseUri, String baseUriStr, XmlParserContext context, Boolean closeInput) 
at System.Xml.XmlReaderSettings.CreateReader(Stream input, Uri baseUri, String baseUriString, XmlParserContext inputContext) 
at System.Xml.XmlReader.Create(Stream input, XmlReaderSettings settings, String baseUri) 
at System.Xml.Linq.XDocument.Load(Stream stream, LoadOptions options) 
at System.Data.Entity.Migrations.Edm.ModelCompressor.Decompress(Byte[] bytes) 
at System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel(String& migrationId) 
at System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel() 
at System.Data.Entity.Internal.InternalContext.QueryForModel() 
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata) 
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata) 
at System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) 
at System.Data.Entity.Internal.InternalContext.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() 
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) 
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() 
at System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) 
at System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) 
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) 
at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() 
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() 
at System.Linq.Queryable.Select[TSource,TResult](IQueryable`1 source, Expression`1 selector) 
at ProductAPI.Controllers.ProductController.Get() in D:\Demo\ProductAPI\Controllers\ProductController.cs:line 24 
at lambda_method(Closure , Object , Object[] ) 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) 
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.Execute(HttpControllerContext controllerContext, IDictionary`2 arguments) 
at System.Web.Http.Controllers.ApiControllerActionInvoker.<>c__DisplayClass2.<InvokeActionAsync>b__0() 
at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)

解决方案

The stack trace seems to indicate the there is a problem reading the Entity Framework model metadata from the database.

HistoryRepository.GetLastModel calls ModelCompressor.Decompress that uses XDocument.Load to read read some XML from a GZipStream. This fails and the model metadata in the database is most likely corrupted.

You can try to recreate the database to get around this problem.

这篇关于神奇的数字在gzip头信息是不正确的。请确保您在一个gzip流过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆