VS 2012添加服务引用为OData Web Api生成不正确的代理类

时间:2014-05-04 22:38:06

标签: c# proxy odata asp.net-web-api

我正在开发一个具有相当简单的web api的小项目。我们决定使用EntitySetController类作为控制器。到目前为止,使用Fiddler2测试web api时所有单词都很棒,我可以使用odata api并在控制器上发出get请求。

下一部分是创建一个简单的客户端来测试代码中的api,所以我创建了一个控制台应用程序,并添加了一个服务引用,并将其指向我的api url。我得到了预期的容器,4个实体类出现在容器下面。问题是2个实体类没有显示使用原始命名空间,2个实体显示。

这是对$ metadata

的回复
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <edmx:DataServices m:DataServiceVersion="3.0" m:MaxDataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <Schema Namespace="IRSI.Utilities.Model" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
      <EntityType Name="Concept">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="Name" Type="Edm.String" />
      </EntityType>
      <EntityType Name="Store">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="StoreNumber" Type="Edm.Int32" Nullable="false" />
        <Property Name="Name" Type="Edm.String" />
        <Property Name="ConceptId" Type="Edm.Int32" Nullable="false" />
      </EntityType>
    </Schema>
    <Schema Namespace="IRSI.Utilities.Electricity.Model" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
      <EntityType Name="ElectricityInvoice">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="InvoiceNumber" Type="Edm.String" />
        <Property Name="InvoiceDate" Type="Edm.DateTime" Nullable="false" />
        <Property Name="Amount" Type="Edm.Decimal" Nullable="false" />
        <Property Name="PreviousReadDate" Type="Edm.DateTime" Nullable="false" />
        <Property Name="CurrentReadDate" Type="Edm.DateTime" Nullable="false" />
        <Property Name="UsageDays" Type="Edm.Int32" Nullable="false" />
        <Property Name="UsagekVA" Type="Edm.Decimal" Nullable="false" />
        <Property Name="FixedCharge" Type="Edm.Decimal" Nullable="false" />
        <Property Name="UsagekWh" Type="Edm.Int32" Nullable="false" />
        <Property Name="RatekWh" Type="Edm.Decimal" Nullable="false" />
        <Property Name="AdditionalUsagekWh" Type="Edm.Int32" Nullable="false" />
        <Property Name="RateAdditionalUsagekWh" Type="Edm.Decimal" Nullable="false" />
        <Property Name="CombustiblePurchase" Type="Edm.Decimal" Nullable="false" />
        <Property Name="CombustibleRate" Type="Edm.Decimal" Nullable="false" />
        <Property Name="EnergyPurchased" Type="Edm.Decimal" Nullable="false" />
        <Property Name="EnergyRate" Type="Edm.Decimal" Nullable="false" />
        <Property Name="OtherCharges" Type="Edm.Decimal" Nullable="false" />
        <Property Name="StoreId" Type="Edm.Int32" Nullable="false" />
      </EntityType>
    </Schema>
    <Schema Namespace="IRSI.Utilities.Water.Model" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
      <EntityType Name="WaterInvoice">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" Nullable="false" />
        <Property Name="InvoiceDate" Type="Edm.DateTime" Nullable="false" />
        <Property Name="InvoiceNumber" Type="Edm.String" />
        <Property Name="Amount" Type="Edm.Decimal" Nullable="false" />
        <Property Name="PreviousRead" Type="Edm.DateTime" Nullable="false" />
        <Property Name="CurrentRead" Type="Edm.DateTime" Nullable="false" />
        <Property Name="UsageDays" Type="Edm.Int32" Nullable="false" />
        <Property Name="Usage" Type="Edm.Decimal" Nullable="false" />
        <Property Name="WaterCharge" Type="Edm.Decimal" Nullable="false" />
        <Property Name="StormDrainCharge" Type="Edm.Decimal" Nullable="false" />
        <Property Name="CCARCharge" Type="Edm.Decimal" Nullable="false" />
        <Property Name="SpecialCharge" Type="Edm.Decimal" Nullable="false" />
        <Property Name="StoreId" Type="Edm.Int32" Nullable="false" />
      </EntityType>
    </Schema>
    <Schema Namespace="Default" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
      <EntityContainer Name="UtilitiesContext" m:IsDefaultEntityContainer="true">
        <EntitySet Name="Concepts" EntityType="IRSI.Utilities.Model.Concept" />
        <EntitySet Name="Stores" EntityType="IRSI.Utilities.Model.Store" />
        <EntitySet Name="ElectricityInvoices" EntityType="IRSI.Utilities.Electricity.Model.ElectricityInvoice" />
        <EntitySet Name="WaterInvoices" EntityType="IRSI.Utilities.Water.Model.WaterInvoice" />
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

生成的代理最终出现在:

IRSI.Utilities.Electricity.Model.ElecricityInvoice;
IRSI.Utilities.Water.Model.WaterInvoice;
IRSI.Utilities.Data.Client.UtilitiesService.Concept;
IRSI.Utilities.Data.Client.UtilitiesService.Store;

其中IRSI.Utilities.Data.Client是控制台应用程序的命名空间

,容器是:

IRSI.Utilities.Data.Client.UtilitiesService.UtilitiesContext;

在Web Api我正在设置这样的路线:

    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.ContainerName = "UtilitiesContext";

        builder.EntitySet<Concept>("Concepts");
        builder.EntitySet<Store>("Stores");
        builder.EntitySet<ElectricityInvoice>("ElectricityInvoices");
        builder.EntitySet<WaterInvoice>("WaterInvoices");

        config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
    }

我是否遗漏了一些告诉客户以不同方式生成代理的内容。理想情况下,它应该是原始命名空间,如模式中的命名空间。接下来最好是完全使用一个模式,就像服务命名空间下的所有实体一样,但不是2的混合。

谢谢,Junicus