在WCF服务中序列化对象?

时间:2013-11-13 02:18:50

标签: c# wcf rest

我已经尝试了一切。我看过这里,找不到答案。所以我有一个接口

    public interface IUserData
{
    ExchangeVersion ExchangeVersion { get; }
    string UserName { get; set; }
    string EmailAddress { get; set; }
    string Domain { get; set; }
    string Password { get; set; }
    Uri AutodiscoverUrl { get; set; }
    Uri ExchangeUrl { get; set; }

}

具体类

[DataContract]
public class UserData : IUserData
{

    public ExchangeVersion ExchangeVersion
    {
        get { return ExchangeVersion.Exchange2010; }
    }

    [DataMember]
    public string EmailAddress
    {
        get;
        set;
    }

    [DataMember]
    public string Domain
    {
        get;
        set;

    }

    [DataMember]
    public string Password
    {
        get;
        set;
    }

    public Uri AutodiscoverUrl
    {
        get;
        set;
    }

    public Uri ExchangeUrl
    {
        get;
        set;
    }

    [DataMember]
    public string UserName
    {
        get;
        set;
    }
}

我有服务合同

[ServiceContract]
public interface IExchangeServiceWcf
{
    [OperationContract]
    //[WebInvoke(UriTemplate = "LoginToExchange", Method = "GET")]
    [ServiceKnownType(typeof(UserData))]
    bool LoginToExchange(IUserData userData);

}

我有一个服务参考,我在我的测试客户端程序中调用它

ServiceReference1.ExchangeServiceWcfClient client = new ServiceReference1.ExchangeServiceWcfClient();
        bool rez;

        IUserData user = new UserData();
        user.UserName = "*****";
        user.Password = "******";
        user.Domain = "*******";
        user.EmailAddress = "*********";

        rez = client.LoginToExchange(user);

rez 行,出了点问题。我总是遇到异常:尝试序列化参数http://tempuri.org/:userData

时出错

有什么问题?

非常感谢:)

1 个答案:

答案 0 :(得分:0)

在您的界面上试试这个。

[ServiceContract]
[ServiceKnownType(typeof(UserData))]
public interface IUserData
{
  ExchangeVersion ExchangeVersion { get; }
  string UserName { get; set; }
  string EmailAddress { get; set; }
  string Domain { get; set; }
  string Password { get; set; }
  Uri AutodiscoverUrl { get; set; }
  Uri ExchangeUrl { get; set; }
}