WCF OperationContract与类型对象的参数

时间:2017-01-20 03:01:14

标签: c# wcf

我正在使用服务和数据合同在我的Windows应用程序中托管WCF服务

[ServiceContract]
[FaultContract(typeof(object))]
[ServiceKnownType(typeof(busSample))]
public interface IUPSBusinessTier
{

    [OperationContract]
    string TestMethod1(string astrName);

    [OperationContract]
    void TestMethod2(busSample abusSample);

    [OperationContract]
    busSample TestMethod3(string astrName);

    [OperationContract]
    string TestMethod4(object astrName);
}

[DataContract]
public class busSample 
{

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

    public busSample()
    {

        this.istrName = "ABC";
    }
}

在使用WCFTestClient测试服务时,得到的错误如“反序列化器不知道要反序列化的类型。检查被序列化的类型与输入代码具有相同的合同,此类型被反序列化。”

1 个答案:

答案 0 :(得分:0)

我认为你只需要将类中的fault contract属性移动到方法上。

   [ServiceContract]
//[FaultContract(typeof(object))]
[ServiceKnownType(typeof(busSample))]
public interface IUPSBusinessTier
{
    [FaultContract(typeof(object))]
    [OperationContract]
    string TestMethod1(string astrName);

    [FaultContract(typeof(object))]
    [OperationContract]
    void TestMethod2(busSample abusSample);