【问题标题】:Generic ServiceContract通用服务合同
【发布时间】:2010-08-17 13:45:23
【问题描述】:
[ServiceContract]
public interface ISecurities<T> : IPolicyProvider where T: EntityObject 
{
  [OperationContract(Name="GetAllSecurities")]
    IEnumerable<T> GetSecurities();

  [OperationContract]
  IEnumerable<T> GetSecurities<T1>(List<T1> lstIdentifiers) where T1 : FI_CusipMaster;

  [OperationContract]
  T GetSecurity<T1>(T1 lstIdentifiers) where T1 : FI_CusipMaster;
}

//Host
        ///CADIS Contract
        ServiceHost dmHost = new System.ServiceModel.ServiceHost(typeof(GlobalInvestors.FIPA.BLL.UDI.CADISSecurities));

        Uri baseAddress = dmHost.BaseAddresses[0];
        Uri policyAddress = new Uri(baseAddress.AbsoluteUri.Replace(baseAddress.AbsolutePath, ""));

        dmHost.AddServiceEndpoint(
            typeof(GlobalInvestors.FIPA.BLL.IPolicyProvider),
            new System.ServiceModel.WebHttpBinding(),
            policyAddress).Behaviors.Add(new System.ServiceModel.Description.WebHttpBehavior());

        dmHost.Open();

 //App.Config
  <service behaviorConfiguration="UDIBehaviour" name="GlobalInvestors.FIPA.BLL.UDI.CADISSecurities">
    <endpoint binding="basicHttpBinding" contract="GlobalInvestors.FIPA.BLL.UDI.ICADISSecurities" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:1667/CADIS" />
      </baseAddresses>
    </host>
  </service>
  <behavior name="UDIBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>

[ServiceContract]
[ServiceKnownType(typeof(SecurityMasterAdapter))]
public interface ICADISSecurities :ISecurities<SecurityMasterAdapter>
{

}

我收到“InvalidDataContractException Type 'System.Collections.Generic.List`1[T1]' 无法导出为架构类型,因为它是开放的泛型类型。您只能导出泛型类型的所有泛型参数类型是实际的类型。”如果我主持这份合同。

我已经读到在 ServiceContract 中避免泛型是件好事。但是可以用T吗?

【问题讨论】:

  • 端点定义不正确。您已指定 IPolicyProvider,因此您只能获得 IPolicyProvider 中定义的操作,而不是 ISecurities 中定义的操作。
  • 你太棒了!删除了 IPolicyProvider 并且它起作用了。但是 ISecurities 必须实现 IPolicyProvider。如何获得这两项操作?
  • 您必须添加暴露 ISecurities 但指定具体类型的端点。
  • 我有另一个合同 ICADISSecurities 实现了 ISecurities(编辑了代码)。我在配置中公开这个端点

标签: wcf generics servicecontract


【解决方案1】:

在这种情况下,您的问题不是 ServiceContract 中的 T,而是 T1 用作 DataContract。如果在服务合同实施期间将 T 替换为特定类型,则可以在服务合同中使用 T。对于数据协定(操作参数和返回类型),您根本不能使用 T。您始终必须指定具体类型。可以使用 ServiceKnownTypeAttribute 重写您的服务合同,以便将 T1 替换为 FI_CusipMaster 并且 ServiceKnownType 指定从 FI_CusipMaster 派生的所有可能的类。

编辑:另一种方法是不使用 ServiceKnownType 并使用必须在 FI_CusipMaster 类型上定义的 KnownTypeAttribute。

最好的问候,拉迪斯拉夫

【讨论】:

  • 我可以托管合同,但是当我创建代理时泛型方法没有显示(添加服务参考)
  • 能否提供合约实现和主机+配置。不知道你是怎么实现的,很难说。
  • 我已经添加了托管合约和配置部分的代码
【解决方案2】:

正如错误所说,不,您不能使用 T。服务合同需要能够写出处理确定类型的序列化信息。它无法处理导出函数中的开放泛型

【讨论】:

    【解决方案3】:

    在你的例子中,T 一个泛型类型。您不能在 ServiceContract 中使用泛型类型,除非它与已定义的类型参数一起使用 - 如在 class Foo : List&lt;int&gt; { } 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多