在C#中使用OData服务不起作用

时间:2018-06-12 17:51:29

标签: c# visual-studio wcf odata crud

我试图理解本教程:http://odata.github.io/odata.net/#OData-Client-Code-Generation-Tool

我可以毫无问题地生成代理。就像它被描述一样。

但是在“使用OData服务”一章中,它不起作用。我完全按照教程中的说明命名了应用程序和代理。 如何在命名空间Microsoft中使用odata?

Screenshot

  • 错误CS0234命名空间“Microsoft”中不存在类型或命名空间名称“OData”(您是否缺少程序集引用?)TrippinApp C:\ TrippinApp \ Program.cs 6 Active
  • 错误CS0246找不到类型或命名空间名称“DefaultContainer”(您是否缺少using指令或程序集引用?)TrippinApp C:\ TrippinApp \ Program.cs 16 Active
  • 错误CS0246找不到类型或命名空间名称“DefaultContainer”(您是否缺少using指令或程序集引用?)TrippinApp C:\ TrippinApp \ Program.cs 16 Active

我用过:

  • Microsoft Visual Studio 2017(试用版)
  • Microsoft.Data.Edm v5.8.3
  • Microsoft.Data.Odata v5.8.3
  • Microsoft.Data.Services.Client v5.8.3
  • System.Spatial v.5.8.3
  • WCF数据服务5.6工具(已安装)

延续:

@Evandro Paula:谢谢你的帮助!在你的帮助下,我再来了一点。但不幸的是,我还没有达到目标。

我安装/更新了:

  • Microsoft Visual Studio 2017(15.7.1)(试用版)
  • Microsoft.Data.Edm v5.8.3(我还没有找到更新的版本)
  • Microsoft.Data.Odata v5.8.3(我还没有找到更新的版本)
  • Microsoft.Data.Services.Client v5.8.3(我还没找到更新的)
  • Microsoft.OData.Client v7.4.4(已安装)
  • Microsoft.OData.Core v7.4.4(已安装)
  • Microsoft.OData.Edm v7.4.4(已安装)
  • Microsoft.Spatial v7.4.4(已安装)
  • System.Spatial v.5.8.3(我还没有找到更新的)
  • WCF数据服务5.6工具(已安装) Screenshot of NuGet

现在结果如下:

Compiling works with Proxy (TrippinProxy.cs), but without Odata example. (Screenshot)

As soon as I copy the code from the tutorial, compiling does't work anymore. (Screenshot)

using System;
using Microsoft.OData.SampleService.Models.TripPin;

namespace TrippinApp
{
    class Program
    {
        static void Main(string[] args)
        {
            DefaultContainer dsc = new DefaultContainer(
                new Uri("http://services.odata.org/V4/(S(fgov00tcpdbmkztpexfg24id))/TrippinServiceRW/"));
            var me = dsc.Me.GetValue();
            Console.WriteLine(me.UserName);
        }
    }
}

来源:http://odata.github.io/odata.net/#OData-Client-Code-Generation-Tool

Now the Namespace Microsoft.OData is found. But not Microsoft.OData.SampleService (Screenshot)

I need a little example. How can I initialize the proxy and add a product (CreateProduct)? Or how can I get the example from the tutorial up and running?

2 个答案:

答案 0 :(得分:2)

首先,将您提到的问题的软件包更新到最新版本。看起来Visual Studio在您的情况下没有使用最新版本。我使用Visual Studio 2017企业版(版本15.7.3)进行此测试。

  • Microsoft.OData.Client(版本7.4.4)
  • Microsoft.OData.Core(版本7.4.4)
  • Microsoft.OData.Edm(版本7.4.4)
  • Microsoft.Spatial(版本7.4.4)

一旦软件包是最新的,您将看到以下构建错误,这与问题https://github.com/OData/lab/issues/80有关:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0234  The type or namespace name 'EdmxReader' does not exist in the namespace 'Microsoft.OData.Edm.Csdl' (are you missing an assembly reference?) ODataClient C:\temp\NET\ODataClient\Connected Services\TrippingService\TrippingProxy.cs 510 Active

此问题的解决方案位于https://github.com/juliopinto15/lab/commit/deb1254301a775eb6771b0bed672dd3f56f37cfe

只需更改代理(例如TrippingProxy.cs)生成的代码行作为方法的一部分 LoadModelFromString()

return global::Microsoft.OData.Edm.Csdl.EdmxReader.Parse(reader);

return global::Microsoft.OData.Edm.Csdl.CsdlReader.Parse(reader);

答案 1 :(得分:0)

就我而言,我删除了 NuGet 缓存文件夹,现在它可以编译了。

%LOCALAPPDATA%\Nuget\v3-cache
相关问题