Reputation: 384
I want to serialize an Entity Framework object (User) and send it to a php script using a typical webClient.OpenWrite method. I don't know if its a good approach or not, but i got dozens of problems with SOAP object serialization.
My initial "User" entity looks like this:
[Table("TableUsers")]
public class User
{
public User()
{
Products = new HashSet<Product>();
}
public int Id { get; set; }
[Required]
[StringLength(4000)]
[Index(IsUnique = true)]
public string UserName { get; set; }
[StringLength(4000)]
public string UserCookie { get; set; }
[StringLength(4000)]
public string CompanyName { get; set; }
...
public virtual ICollection<Product> Products { get; set; }
}
And when i tried to serialize all these things and send it to a server with the next piece of code:
using (Stream postStream = Client.OpenWrite("http://test.com/analytics.php"))
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(postStream, user);
}
Soap Serializer does not support serializing Generic Types ...
Okey, fine. I marked my Product collection with [SoapIgnore] attribute. And seems that this attribute is not working because i got the same error. Then i decided to delete User Constructor from User class and bingo! serialization is performed! (okey, i can live without interface inicialization)
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<i2:User_782DCAD431DFFDCAE6D6A91B7338AB23B0463133F84A5181874089B6BAEBDBC5 id="ref-1" xmlns:i2="http://test.com">
<User_x002B__x003C_Id_x003E_k__BackingField>6</User_x002B__x003C_Id_x003E_k__BackingField>
<User_x002B__x003C_UserName_x003E_k__BackingField id="ref-3">[email protected]</User_x002B__x003C_UserName_x003E_k__BackingField>...
Finally, i used attributes like [DataContract(Name = "User"] and [DataMember(Name = "Name")]. As the result, instead of full soap envelope Fiddler showed me only a part of it!
What is wrong with this soapFormatter? why there is no exeptions? how should i mark all this classes and properties to make a pretty printed soap query?
Upvotes: 0
Views: 278
Reputation: 183
Try put this at your context class:
public class MyContext : DbContext
{
public MyContext()
{
this.Configuration.ProxyCreationEnabled = false;
}
....
}
Here is the link with more details: https://msdn.microsoft.com/en-us/data/jj592886.aspx
Upvotes: 0