A RIA Domain service has this method...
public virtual CmsDealer GetCmsDealer(string id)
{
return this.Context.CmsDealerSet.FirstOrDefault(p => p.Id == id);
}
The Wcf service has this...
public CmsDealer GetDealer(string id)
{
return domainservice.GetCmsDealer(id);
}
When called with a nonexistent Id, the GetCmsDealer() call barfs with an exception about
Unable to cast object of type 'System.Object[]' to type 'RIAServicesLibrary.Web.Models.CmsDealer[]'.
It was my impression that it would simply retun a null, that I could test for later. Yet somehow that isn't happening. All the examples I find show something like this working fine.
Any insight helpful, I am a little baffled and a bit of a newb about EF, RIA and Wcf.
Updated: The problem is that I need to track the failure all the way dowwn to Wcf so that the application that ultimately calls GetDealer can find out that there was no solution. Ideally, I need to do that without leaving uncaught exceptions around :)
A try / catch solution inside GetCmsDealer will allow me to detect the failure, but there is still no way to rerutn a "Empty" CmsDealer object without faking a number of contraints (it seems entity is picky about it).
So I can try / catch in GetDealer as well, but I still dont have a valid CmsDealer object to return.
Thanks!
UPDATED
For those who were wondering how it played out, the final routine looked like this...
public virtual IQueryable<CmsDealer> GetCmsDealerSetOrEmpty(string id)
{
return this.Context.CmsDealerSet
.Include("CmsItemState")
.Where(p => p.Id == id)
;
}
The Wcf wrapper is now back to being a simple pass through. On the client I check the Count() of the result, if it is 0 then I can knwo there was no record, if it's 1 then a simple First() gets me the record.
I am willing to bet there is a better way.