2012-04-10 9 views
4

Ich versuche, Produktname und seine IDs als NameID-Objekt von EntityFramework mit WebAPI abrufen. Code für den wie folgt ist.Die magische Zahl im GZip-Header ist nicht korrekt. Stellen Sie sicher, dass Sie einen GZip-Stream übergeben

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;} 
} 

Es wirft Fehler wie folgt

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) 
+2

versuchen Sie mit ZIP-Datei zu entpacken Gzip? – Zaki

+0

Ich versuche nichts. außer Rückgabe des NameID-Objekts. und das ist reines Vannila Project mit EntityFramework poco. Keine HTTP-Komprimierung eingestellt. –

+0

Falls es hilft, gibt es keinen Fehler beim Wiederherstellen von IQueryable. Ich ändere den Code, um weiter zu erklären. –

Antwort

5

Der Stack-Trace scheint es ein Problem gibt das Entity Framework-Modell Metadaten aus der Datenbank zu lesen, um anzuzeigen.

HistoryRepository.GetLastModel Anrufe ModelCompressor.Decompress, die XDocument.Load verwendet, um etwas XML von einem GZipStream lesen zu lesen. Dies schlägt fehl, und die Metadaten des Modells in der Datenbank sind höchstwahrscheinlich beschädigt.

Sie können versuchen, die Datenbank neu zu erstellen, um dieses Problem zu umgehen.

3

Es ist ein bisschen spät und es gibt bereits eine akzeptierte Antwort, die funktionieren wird. Wenn Sie jedoch bereits über eine funktionierende Datenbank verfügen und die Datenbank nicht aktualisieren möchten, können Sie Database.SetInitializer mit Null in der Funktion application_start von Global.asax aufrufen. Dies wird nicht nach der __migrationhistory Tabelle suchen, die die beschädigten Daten enthält.

3

Keine Möglichkeit, die Datenbank für mich neu zu erstellen, und ich verwende bereits Database.SetInitializer mit null.

Zum Glück habe ich eine up-to-date __MigrationHistory Tabelle auf eine andere Datenbank und ich verwendet, um dieses SQL-Abfrage den korrekten Wert in der Zieldatenbank zu setzen:

INSERT INTO TargetDbName.dbo.__MigrationHistory (MigrationId, Model, ProductVersion) 
SELECT MigrationId, Model, ProductVersion 
FROM SourceDbName.dbo.__MigrationHistory 
WHERE MigrationId = 'YYYYMMDDHHMMSSFFF_LastMigration' 
Verwandte Themen