automaticly serialize object via web service - object

I have my webservice method which sends an object as a result, for example:
public Dog GetDog();
where dog looks like this:
public class Dog{
public string Name { get; set;}
public int Age{ get; set;}
}
Is there any way how to set the default serialization method without having to change the method to:
public string GetDog(){
return dog.toString();
}
thanks

Assuming you are talking about .net web services. Here is the trick.
[WebMethod]
public Dog GetDog()
{
//method code
}
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public class Dog{
public string Name { get; set;}
public int Age{ get; set;}
}
You can read this article for further understanding.
http://ryanfarley.com/blog/archive/2004/05/26/737.aspx

Related

Automapper - flattening of object property

let's say I have
public class EFObject
{
public int Id { get; set; }
public int NavId { get; set; }
public NavObject Nav { get; set; }
}
public class DTOObject
{
public int Id { get; set; }
public int NavId { get; set; }
public string NavName { get; set; }
}
My expectation was high, and I thought to my self the built-in flattening should handle this, so my mapping is very simple
CreateMap<DTOObject, EFObject>().ReverseMap();
Unfortunately, converting DTOObject to EFObject does not work as expected because EFObject.Nav is null. Since I used the name NavId and NavName I would expect it to create a new NavObject and set the Nav.Id and Nav.Name accordingly.
My Problem : Is there a feature in Automapper that will allow me to achieve the intended result without having to manually write a rule to create an NavObject when mapping the Nav property?.
Unflattening is only configured for ReverseMap. If you want unflattening, you must configure Entity -> Dto then call ReverseMap to create an unflattening type map configuration from the Dto -> Entity.
as noted by Automapper documentation here

how to desactivate lazy loading for entity framework 6

public class Country : Item
{
public string Code
{get; set;}
public string CodePhone
{ get; set; }
public string Name
{get; set;}
public string Flag
{ get; set; }
public decimal? Latitude
{get; set;}
public decimal? Longitude
{get; set;}
public int RegionsCount
{get; set;}
[ForeignKey("DefaultCurrency")]
public int? DefaultCurrencyID
{get; set;}
public virtual Currency DefaultCurrency
{ get; set; }
public ContinentType ContinentType
{ get; set; }
public virtual ICollection<Property>
Properties
{get; set;}
public ICollection<CountryLocale>
CountryLocales
{ get; set; }
}
public class CountryLocale : ItemLocale
{
[ForeignKey("Country")]
public int CountryID
{get; set;}
public Country
Country
{get; set;}
public string FullName
{get;set;}
}
public TEntity Get(Expression<Func<TEntity, bool>> where, params string[] includes)
{
var model = this.DbSet;
foreach (var property in includes)
{
model.AsExpandable().Include(property);
}
return model.Where(where).FirstOrDefault();
}
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
Country country = this._CountryRepository.Get(p=>p.ID == this.CountryID, new string[] { "CountryLocales" });
Value cannot be null.
Parameter name: source
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: source
line : CountryLocale countrylo = country.CountryLocales.First();
i got this error when trying to desactivate lazyloading for performance reasons. how can i resolve the problem? thanks in advance.
As you disabled LazyLoading you need to manually load the child properties of your main object with Include
var country = db.Country.Include("CountryLocales");
The other way around would be:
Dont disable lazy load and use include in your queries.
Try including your child objects one by one until you feel the
performance is better.
Usually if you know you will iterate over a
child object you should use include in your query for that child
object.
remeber to include second level child if you need to access a property of that level... Include("Parent.Child")

What's the best way to convey required/optional DTO properties in ServiceStack?

I'm having an issue with my ServiceStack w/ Swagger implementation regarding documenting required/optional properties. Developers implementing clients that consume my services love the Swagger documentation, however they don't know which properties are required vs. optional--aside from getting a 400 response on each attempt to get a valid request through.
Take the following example:
public class UserProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public UserAddress Address { get; set; }
}
public class UserAddress
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Country { get; set; }
public string PhoneNumber { get; set; }
}
Swagger will cleanly show both of these types if they are part of my DTO, however I can't convey that FirstName, LastName, or any of the Address properties are required or not. Is there a way to accomplish this without having to roll a separate spec document?
You can use an [ApiMember(IsRequired = false)] attribute on the properties in the DTO to add extra information for swagger ui.
There is list of the attributes that swagger ui will recognise on the servicestack wiki

Parsing YouTube Json for Windows Store apps

I generate C# Class from http://json2csharp.com/ for any YouTube URL, in which some names are invalid like as follows:
public class Feed
{
public string __invalid_name__xmlns$media { get; set; }
public string __invalid_name__gd$etag { get; set; }
}
In the above code actual Youtube name is xmlns$media, gd$etag like that...
when I change those to:
public class Feed
{
public string xmlns$media { get; set; }
public string gd$etag { get; set; }
}
in C# it shows error because of special character $, If I don't use $ parsing doesn't happens and returns Null.
Help me fixing this!
Does this work for you?
[DataContract]
public class Feed
{
[DataMember(Name="xmlns$media")]
public string xmlns_media { get; set; }
[DataMember(Name="gd$etag")]
public string gd_etag { get; set; }
}

Serialize Object Azure Mobile Services

I am trying to serialize an object to Azure Mobile Services.
The object contains an array of a second object which should also be serialized.
[DataContract()]
class ObjectA
{
[DataMember(Name= "id")]
public int Id { get; set; }
[DataMember(Name = "info")]
public string info{ get; set; }
[DataMember(Name = "collectionOfB")]
public ObjectB[] myArrayOfB{ get; set; }
}
[DataContract()]
class ObjectB
{
[DataMember(Name= "id")]
public int Id { get; set; }
[DataMember(Name = "info")]
public string info{ get; set; }
}
I have loaded both table's properly and can insert an individual item into each of the tables.
However when I call the InsertAsync method on the table for objectA I receive an error
Cannot serialize member 'myArrayOfB' of type 'namespace.ObjectB[]' declared on type 'ObjectA'
Any idea's on what I need to do to fix this?
Mobile Services doesn't support serialization of arrays. There are two good posts here that show how you might support this:
http://blogs.msdn.com/b/carlosfigueira/archive/2012/08/30/supporting-arbitrary-types-in-azure-mobile-services-managed-client-simple-types.aspx
http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/11/supporting-complex-types-in-azure-mobile-services-clients-implementing-1-n-table-relationships.aspx

Resources