I am looking for an example of how to use the new DataServiceState Save and Restore methods in a WP7 application in order to tombstone a datacontext - I cannot find any examples and the approach I used resulted in an exception
this saves the data context correctly
PhoneApplicationService.Current.State["DataContext"] = DataServiceState.Save(this.Model.Entities);
this attempts to restore it after the app is re-activated
var dc = (PhoneApplicationService.Current.State["DataContext"] as DataServiceState).Restore();
but throws an exception
An item could not be added to the collection. When items in a DataServiceCollection are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection.
This is the same exception I get if I try to reload a datacontext that I stored "directly" (without using the DataServiceState.Save method) in the PhoneApplicationService.Current.State. I cannot find any offical documentation on the new ODATA v2 DataServiceState class or examples.
thanks
Michael
I am looking for an example of how to use the new DataServiceState Save and Restore methods in a WP7 application in order to tombstone a datacontext
Are you sure? First link - bottom of the page.
Have you tried the walkthrough here?
How to: Preserve and Restore Application State for Windows Phone
Storing and retrieving datacontext is tricky due to serialization and object reference issues. The MSFT team is working on improving DataServiceState. I've succeeded in saving and restoring the context in a WP7 app, by storing the DataServiceState returned by Save() in the app state (just like you've done). Then on activated, I first instantiate my DataServiceClient (which contains the context and the DataServiceCollection), and then I call a RestoreData method in the client and pass the retrieved DataServiceState to it. The method restores the context and DSC within the DataServiceClient.
Related
In my ASP.NET Core MVC site, I have a SignalR hub that has my DbContext injected into the constructor. The hub pulls data from the database and send it to a kendo ui grid for the user to view. This data is filtered in that hub on which group is selected (stored in the database).
The group selection is done outside the context of the hub. When I change the users selected group the page reloads to update various UI elements. When the signalr hub is then called, the selected group is still set as what it was prior to the change. After digging for a bit I came across this issue on the signalr github. What I understand is because the hub is transient, the DbContext is as well and since the hub is long running the DbContext is never updated.
Is there a simple way around this while still dependency injecting the DbContext or do I need to create and dispose a new context for every call? If so what is the best way to go about doing that and still pass the connection string from the appsettings.json?
EDIT
I am currently using Microsoft.AspNetCore.SignalR.Server and not the new Microsoft.AspNetCore.SignalR library.
The only way I could get around this issue with Microsoft.AspNetCore.SignalR.Server was to add a DbContextOptionsBuilder<T> singleton to the the ConfigureServices method in Startup.cs and then call that in a using(...) in the hub. While I feel this is a dirty way around the issue, I also believe it is the only way around the issue. Microsoft recently deprecated SignalR-Server and are moving to a new code base at SignalR. Hopefully this issue will be addressed in their new version.
Startup.cs
DbContextOptionsBuilder<PortalDbContext> builder = new DbContextOptionsBuilder<PortalDbContext>();
builder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
services.AddSingleton(builder.Options);
Hub Classes
using (PortalDbContext dbContext = new PortalDbContext(_dbContextOptions))
{
...
}
I already have an object called Permission in my Parse application. It has four properties, namely: targetObject, roles, operations and action which appear fine in the Parse application core console.
Now, I added one more property called type to the Permission object.
I am able to save the object and read back the type property in the retrieved objects but I am not able to see the new column type for this new property in the Parse application core console.
What do I need to do in order to see this new column in the console?
Try reloading the app from the drop down list of apps or signing back in.
As soon as you create a new column, simply refresh the webpage showing the Parse Data Explorer.. they are aware of this issue, but it is set as a low priority to correct.
I use an RKObjectManager to load objects from a remote resource, and, use a tableView to display the fetched objects. When my tableView model is deallocated I cancel all current requests with
[self.objectManager cancelAllObjectRequestOperationsWithMethod:RKRequestMethodGET matchingPathPattern:self.resourcePath];
When the user reloads the view, a new model is created - Instead of creating the objectManager from scratch, I fetch the same one (I store the objectManager instance elsewhere). I'm trying to use a single object manager across the app for the same service/site - not sure if we can use multiple object managers against the same persistent object store? However, now all requests to the resource path fails with the following error.
restkit.network:RKObjectRequestOperation.m:569 Object request failed: Underlying HTTP request operation failed with error: Error Domain=NSURLErrorDomain Code=-999 "The operation couldn’t be completed. (NSURLErrorDomain error -999.)"
It looks like once I cancel against a resource path on an objectManager, I can't reload that resource via a fresh request at a later point in time. What would the best practice be to cancel current requests and reload later? In the earlier delegates version of restkit, my app would crash if I didn't remove my model/view as delegate from the object manager. I'm thinking I should still cancel my requests to avoid such issues even with the new blocks way of operation? Pointers/advice much appreciated. Thanks
Regards
George M.P.
not sure if we can use multiple object managers against the same persistent object store?
No problem with that.
When the user reloads the view, a new model is created - Instead of creating the objectManager from scratch, I fetch the same one...
If you create a new model (I guess you mean managed object store?) you should probably create a new object manager to go with it. In theory you can give the old object manager the new store but there is (or could be) a lot of internal caching that could be invalidated.
Cancelling and then reloading later should be fine, the question is what you're doing with the object store in between...
NOTE: There are 3 questions in here and I did not make separate questions since they are all somewhat related to the same code.
I have the following code that registers the connection to my RavenDB in the Application_Start once per the application's life cycle:
var store = new DocumentStore { Url = "http://localhost:8080" };
store.Initialize();
builder.RegisterInstance(store).SingleInstance();
Now this works fine and this is something that should be created only once per the application's life cycle. Now I wanted to add in the DocumentSession to Autofac so I tried to add in this in the Application_Start:
var session = store.OpenSession();
builder.RegisterInstance(session).SingleInstance();
In my UserRepository I have the following constructor:
public UserRepository(DocumentStore store, DocumentSession session)
When I try to run this, I get the follow runtime error:
Cannot resolve parameter 'Raven.Client.Document.DocumentSession Session' of constructor 'Void .ctor(Raven.Client.Document.DocumentStore, Raven.Client.Document.DocumentSession)'
That error to me sounds like Autofac does not think it has a DocumentSession however that is what store.OpenSession() returns so it should. Anyone know what would be causing this error? Am I not setting the session variable correctly (it is the same as the store variable which works fine)?
Another thing which may or may not be related to the above issue is how do I add an instance of an object to Autofac per request instead of per the applications life cycle? While the RavenDB DocumentStore object should only be created once be the life application cycle, the DocumentSession should be created once per the request (maybe creating it per application level is causing the error above).
One last question I will throw there about Autofac (mildly related to the code above) is about releasing the objects. If you take a look at this tutorial:
http://codeofrob.com/archive/2010/09/29/ravendb-image-gallery-project-iii-the-application-lifecycle.aspx
The last piece of code:
ObjectFactory.ReleaseAndDisposeAllHttpScopedObjects();
and the point of this code is to prevent leaking the sessions. Now is this something I also need to worry about for Autofac and if so, how would I do this in Autofac?
I'm guessing you want something like:
builder.Register(c => c.Resolve<DocumentStore>().OpenSession()).InstancePerLifetimeScope();
"The default ASP.NET and WCF integrations are set up so that InstancePerLifetimeScope() will attach a component to the current web request or service method call." - Autofac: InstanceScope
Basically, in a web app, InstancePerLifetimeScope handles the one per HTTP context aspect, and also disposes any types that implement IDisposable.
There was also the issue that OpenSession returns a IDocumentSession instead of a DocumentSession. Changing my class to look for a IDocumentSession along with doing what Jim suggested worked, thanks.
I have a WP7 app using the ODATA v2 library with DataServiceState class and I am able to call the DataServiceState.Save method to store a DataServiceCollection during tombstoning. I can also call the DataServiceState.Restore method and successfully restore a DataServiceCollection that was saved.
The problem arises when the DataServiceCollection Type that has been saved contains one or more Collection properties that have been loaded/expanded.
If I don't expand those properties (using the Entities.BeginLoadProperty method) it works fine. But if I expand one or more of the properties, when I try to restore the collection I get the following exception
An item could not be added to the collection. When items in a DataServiceCollection
are tracked by the DataServiceContext, new items cannot be added before items have been loaded into the collection.
Not sure what I am missing - the DataServiceState class is supposed to address the issues with trying to deserialize ODATA DataServiceCollections - which seems to work one level deep but as soon as it goes to 2 levels - the exception is thrown
thanks
Michael
According to this post: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataservices/thread/0806c41a-2699-4390-8aaf-14b9c75a9dca the ODATA library for WP7 doesn't work with the $expand option in the current release and they're planning on better support for tombstoning in the next release.