首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通用ServiceContract

通用ServiceContract
EN

Stack Overflow用户
提问于 2010-08-17 21:45:24
回答 3查看 6K关注 0票数 1
代码语言:javascript
复制
[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类型'System.Collections.Generic.List`1T1‘不能作为模式类型导出,因为它是一个开放的泛型类型。只有当它的所有泛型参数类型都是实际类型时,才能导出泛型类型。“如果我主持这份合同。

我读到过在ServiceContract中避免泛型是有好处的。但是可以使用T吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-08-17 21:55:06

在这种情况下,您的问题不是ServiceContract中的T,而是用作DataContract的T1。如果您在服务合同执行过程中将T替换为特定类型,则可以在服务合同中使用T。对于数据契约(操作参数和返回类型),您根本不能使用T。你总是需要指定具体的类型。可以使用ServiceKnownTypeAttribute重写您的服务契约,以便用FI_CusipMaster替换T1,并且ServiceKnownType指定从FI_CusipMaster派生的所有可能的类。

编辑:另一种方式是不使用ServiceKnownType,而是使用KnownTypeAttribute,这必须在FI_CusipMaster类型上定义。

致以最好的问候,Ladislav

票数 3
EN

Stack Overflow用户

发布于 2010-08-17 21:47:08

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

票数 1
EN

Stack Overflow用户

发布于 2010-08-17 21:49:03

在您的示例中,T是一个泛型类型。您不能在ServiceContract中使用泛型类型,除非它与定义的类型参数一起使用-就像在class Foo : List<int> { }中一样。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3503067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档