【问题标题】:WCF and Interfaces [duplicate]WCF 和接口 [重复]
【发布时间】:2011-07-22 09:04:46
【问题描述】:

可能重复:
Passing Interface in a WCF Service?

我对 WCF 和接口有疑问。

我有两节课

public interface ICompany {
  string Name { get; set; }
  IAddress Address { get; set; }
}
class Company : ICompany {
  public string Name { get; set; }
  public IAddress Address { get; set; }
}
public interface IAddress {
  string Road { get; set; }
}
class Address: IAddress {
  string Road { get; set; }
}

我的服务返回

[OperationContract]
    Company GetCompany(String name);

但这不起作用,我确定问题出在 Company 类中的 IAddress 上,但这不能以某种方式解决吗?

【问题讨论】:

  • 您能否提供比“这不起作用”更多的细节。你得到什么错误信息?什么时候收到?
  • 请在发帖前做一些研究。刚刚搜索“wcf interface”找到了重复项。
  • 不确定,但请尝试将 ServiceKnownType 属性应用于您的 Webservice 方法的地址
  • 你能澄清你的问题吗?错误信息?

标签: c# wcf interface


【解决方案1】:

你的类必须声明为:

[DataContract]
class Company {
  [DataMember]
  public string Name { get; set; }
  [DataMember]
  public IAddress Address { get; set; }
}

并且您的服务声明和实现为:

[ServiceContract]
public interface ICompanyService
{
   [OperationContract]
   Company GetCompany(string name);
}

public class CompanyService : ICompanyService
{
  public Company GetCompany(string name)
  {
     return new Company { Name = name};
  }
}

只有在此之后,您才能使用此服务。 我建议阅读更多that

【讨论】:

    【解决方案2】:

    我假设您需要在所有内容之前使用 [DataContract] 和 [DataMemebr] 标记需要由客户使用的类。

    【讨论】:

      【解决方案3】:

      您需要在要序列化的类和方法上使用 [DataContract] 和 [DataMemebr] 属性。 WCF 将序列化对 XML 的响应(大部分时间),因此编译器必须能够序列化对象。

      【讨论】:

      • 不正确。不需要DataContract:再次stackoverflow.com/questions/5921635/…,使用ServiceKnownType(但这是重复的)
      • @Polity 在某些情况下可能不需要它。这不会使我的答案不正确
      • 好吧,对不起,不正确的 iment 不能解决这个特殊问题
      猜你喜欢
      • 2015-09-15
      • 2011-05-07
      • 2013-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      相关资源
      最近更新 更多