我是ADO.net数据服务的新手(一般来说也是.net),我有这个项目,我需要设置一个数据服务,用nHibernate读取和写入数据库,I已经创建了服务:
[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;
}
我已经为服务创建了一个测试:
[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);
}
当我使用浏览器运行服务时(例如:http://localhost:1413/DataService.svc/Factories?Id=54),服务返回:
<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>
但是当我运行单元测试时,我得到了这个例外:
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()
我调试了项目,因此测试连接到服务,服务返回数据,但
在收到xml数据时,我还应该做些什么来将其转换为实际对象?
我做了一个测试,使用string
对象而不是Factory
并且它有效,问题似乎与我的课程有关。 我错过了什么吗?
答案 0 :(得分:0)
最后我解决了它: 我添加了一个服务引用(我没有做过),并将测试更改为:
[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);
}
}
现在它通过了测试:
Reinhardt strasse
1 passed, 0 failed, 0 skipped, took 3.48 seconds (NUnit 2.5.2).
现在我认为这很容易,但是当你不知道时,这很难......