What are the steps to read an object from an ADO.net data service?
I'm new to ADO.net data services (and to .net in general too..), I'm having this project where I need to setup a data service to read and write to a database with nHibernate, I've created 开发者_如何学JAVAthe service:
[WebGet]
[SingleResult]
public Factory Factories(int Id)
{
try
{
Factory[] results = this.CurrentDataSource.Session.Linq<Factory>().Where(g => g.Id.Equals(Id)).ToArray();
return results[0];
}
catch (Exception ex)
{
throw ex;
}
And I've created a test for the service:
[Test]
public void CanReadFactoryDataService()
{
DataServiceContext ctx = new
DataServiceContext(new Uri("http://localhost:1413/DataService.svc"));
var Factories = ctx.Execute<Factory>(
new Uri("Factories?Id=54", UriKind.Relative));
Assert.IsNotNull(Factories);
Factory factory = Factories.First<Factory>();
{
Console.WriteLine(factory.NAME);
}
When I run the service with the browser (eg: http://localhost:1413/DataService.svc/Factories?Id=54), the service returns:
<Factories p1:type="ADODS.Core.Factory">
<FactorY_CODE>abc</FactorY_CODE>
<NAME>Nameds</NAME>
<ADDRESS>Reinhardt strasse</ADDRESS>
<COMPENSATION_MODEL p1:null="true"/>
<B_CODE p1:null="true"/>
<Id p1:type="Edm.Int32">54</Id>
</Factories>
But when I run the unit test I'm getting this exception:
TestCase 'Tests.DataServicesTests.CanReadFactoryDataService'
failed: System.InvalidOperationException : La secuencia no contiene elementos (The sequence contains no elements)
en System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
H:\ADODS\tests\ADODS.Tests\ADODS.Web\DataServicesTests.cs(22,0): en Tests.DataServicesTests.CanReadFactoryDataService()
I debugged the project, so the test connects to the service and the service returns the data, but
is there anything else I should do to convert the xml data into the actual object when I receive it?
I did a test, with a string
object instead of a Factory
and it worked, the problem seems to be with my classes. Am I missing something?
Finally I solved it: I Added a Service reference (Wich I hadn't done), and changed the test to:
[Test]
public void CanReadFactoryDataService()
{
ServiceReference1.DataServiceContext ctx = new
ServiceReference1.DataServiceContext(new Uri("http://localhost:1413/DataService.svc"));
var factories= ctx.Execute<Factory>(
new Uri("Factories?Id=54", UriKind.Relative));
Assert.IsNotNull(factories);
Factory factory = factories.First<Factory>();
{
Console.WriteLine(factory.ADDRESS);
}
}
And now it passes the test:
Reinhardt strasse
1 passed, 0 failed, 0 skipped, took 3.48 seconds (NUnit 2.5.2).
Now I think that was easy, but when you don't know, it's hard...
精彩评论