I have a class called Test which has four public properties and one of them is static. the problem is after deserialization the static property contains null value. i have debugged the code and found that at server side it contains the value which is a collection , but at client side it becomes null after deserialization. i know static members doesn't serialize and deserialize so obviously it should contain the value.
Static variables are global and stateful - thus they exist solely in the context of the application, or in other words, memory.
You could pass the value of the static property in another non-static property, but you can't send your application's memory down to the client.
If the static value is initialised when the type is loaded (via a field initialiser or via the type initialiser/static constructor) then it should contain the value.
If however, the server side static value is initialised as a side effect of some method call, then you would have to reproduce this method call on the client as well.
I'm not sure I understand...as you say in your question:
i know static members doesn't serialize and deserialize...
Given that, why would you expect the value from the server to propagate to the client? You will need to find an alternative means of transferring this property (make an instance property, send your own message, etc.)
Remember that static member values exist within a particular .NET application domain, and application domains exist within a particular operating system process.
Given that the server and client are different operating system processes and possibly even different machines, as Adam mentioned, there is no way for the value you had on the server to automatically transfer to the client without you writing some code.
I think maybe there is a misconception - Serialization is not packaging up the instance and its static members on the server, and sending it down to the client. It is extracting the values of the members it regards as serializable (e.g. members annotated with [DataMember], or instance members, but not static members), and sending down only those values to the client.
Therefore the value on the client will be the same as the value on the server was before you set it to the value you're now expecting to see on the client.
However: I notice you also mention you see that a collection of yours has a null value.
If you are using DataContractSerializer in a PartialTrust environment, be aware that it may not call the constructor of your class.
Quote:
When instantiating the target object during deserialization, the DataContractSerializer does not call the constructor of the target object.
If that collection was created by your constructor, this may explain why you see null.
Related
I have Document entity having read-only (from client perspective) property modificationAuthor (I want it to be modified only on server side which is aware of currently logged user). Document entity has has no setters for modificationAuthor property neither in proxy nor in domain object.
The problem is that it's not a simple property but other entity (User) so I need to add modificationAuthor to the paths when I invoke requestFactory.find(id).with(paths).fire() to get Document instance. (so that modificationAuthor is not null when I want to read it from Document).
But after adding it to paths and trying to persist Document object I'm getting exception: Could not locate setter for property modificationAuthor.... It seems that RF requires setters even for properties that are designed to be read-only.
Is there currently any way to avoid adding setters to such properties and be able to read it and persist parent object?
This is a side-effect of issue 5952, which will (should) be fixed in GWT 2.5, to be released in a month or so: RF erroneously thinks the modificationAuthor property has been modified (because it fails at comparing the User entity with itself), so it sends an operation to the server for that property, and the server then tries to set the property value to the given entity (which happens to be the one that's already there).
In the mean time, I'm afraid you'll have to add a dummy setter (it does not need to actually assign the property value) to your Document domain object.
I have an abnormal serialization/de-serialization behavior in my ASP.NET MVC application.
Setup and architecture are as follows:
.Net 3.5, MVC 2, Unity 2.0 for dependency injection, IIS 7. State is stored in ASPNET state process. Application apart from models, views and controllers also contains a number of application specific services and model mappers (for interfacing with existing code). Two of the services have per Session based lifetime, for which I implemented custom PerSessionLifetimeManager (as Unity does not have one out of the box). This lifetime manager uses HttpContext.Current.Session to store objects. There are fair bit of dependencies between controllers, mappers, services and between services as well.
Problem:
One of session lifetime services contains a private boolean field which changes value under some internal logic conditions, however this value changes by outside of my code. After some investigation I found that problem relates to this object being serialized/de-serialized twice every time and values of the field are different during serializations.
Investigation done so far:
I have a break points/logging on object constructor and I wrapped field into a property and put breakpoint/logging on setter. Object is definitely constructed only once and there are no calls to change property value except those which should be called. I have made object to implement ISerializable and put logging of System.Threading.Thread.CurrentThread.ManagedThreadId and object hash (constructed from all fields of the object). From what I can see if value of the property/field changes object gets serialized twice once with new value and immediately after with original value. Next time object is used it de-serializes in LIFO order, so the original (unchanged) value object is pulled out first and then the changed one. I tried to log a full stack trace for these serialization calls, but it seem to be all .Net internal calls.
Questions:
Why is there multiple serializations with different values? How to avoid this?
Current work around:
I used Session instead of private field/property and it works fine, however use of session on heavily loaded websites is not the best thing and I am hoping to for some other solution.
Thank you.
Are you sure the value is becoming corrupted or are you simply loosing the value due to the session being purged. Do you have any other values in the session that disappear at the same time? I was a bit confused by your "State is stored in ASPNET state process. " statement. Is your session management set to InProc or are you using a StateServer? If it is InProc, you definitely will experience loss of session values (especially in shared hosting environments), and should switch to an ASP State Server Service or MSSQL State Server solution.
The JavaDoc for EntityProxy.stableId() says:
"An id returned by a proxy newly created by RequestContext.create
Object.equals(Object) those returned later by proxies to the persisted object."
Does that mean that the stable id will be valid for that object across different request contexts? across different request factory instances? I suppose I'm asking what is the scope of the stable id?
The EntityProxyId object has a global scope and is suitable for long-term use as a Map key or member of a Set. EntityProxyId objects can be used across different instances and types of a RequestFactory (assuming that the proxy type is reachable from the RequestFactory in question). The RequestFactory.getHistoryToken() and getProxyId methods can create a string representation of an EntityProxyId that is suitable for client-side persistence.
The one exception to the global scope of an EntityProxyId is the id of a newly-created EntityProxy that has not yet been persisted on the server. An "ephemeral" id is only usable with the RequestFactory from which the newly-created proxy object is derived. Once the proxy has been sent to the server and the server has provided a non-null id, the ephemeral id is upgraded to a persistent id and becomes indistinguishable from any other persistent id. The object identity of the upgraded EntityProxyId does not change, so ephemeral ids can be added to Map or Set and be retrieved later.
I have a Product object with a property that is a collection of type Workflows. In my "GetProducts" method on the domaincontext object I have set a breakpoint at the return statement to see if the workflows collection is filled.
It is.
On the client side I check Context.Products[0].Workflows in another breakpoint and I see 0 results. Is there a way to persist this nested data for consumption on the client side or is RIA Services inhibited from doing this?
If you have or can download the RiaServicesOverviewPreview.pdf document section 4.8 details how to do this. The basic summary it.
Make sure your L2S query specifies the .LoadWith<>() parameter. Lazy loading doesn't work with RIA services so you have to use implicit loading.
You need to apply the "IncludeAttribute" to the associated member. For example add the [Include] attribute on your Workflows field in the Product metadata class.
Ensure that your Workflow (child) type is exposed as a client type so it gets genned to the client side.
You can get the document here: http://www.microsoft.com/downloads/details.aspx?FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabce&displaylang=en
I should kick myself. I realized that I needed to add "[Include]" to the property in Product within the DataService.metadata.cs file and now it gets sent to the client.
What do you do with classes that have no member data, only methods?
Do you make them static?
In my case it is an repository class that executes queries against the database. Maybe I got the repository pattern wrong... (It does implement an interface)
Inherit from an Interface mean that you cannot use static. Simply create a class and instantiate it.
If it implements an interface, and gets passed around as that interface, then you can't make the members (or the class) static. The interface aspect means that although an instance won't have any actual fields, it still contains valuable information - its type.
You might want to make it a singleton, but there's no particular need to.
Why won't you make a database wrapper class, that maintains the connection to database opened/closed. Moreover, it can also open it automatically if new query is sent. You can include the functions your class has, running them right on the inner pointer to the database.
I guess this is the best database management pattern. If you use it, you should make a Factory method that returns the object of this class initialized on some specific database. Then you pass that object around.
Or if you are lazy and sure that you will need only 1 database, make it a singleton.
It depends. Most of the time it might be possible to make the class static, but sometimes you need to pass an instance of it around. Sounds like you might be having this situation. In this case perhaps you should consider a Singleton pattern, since more than 1 instance of the class is not likely to be needed?