使用WCF传输DataContract对象的问题:“ System.ServiceModel.Channels.ServiceChannel ...错误”异常

时间:2018-07-24 10:40:11

标签: c# .net wcf datacontract datamember

在开发我的 WCF 项目时,发生了一些数据传输问题。 具体来说,我有一个类 User 和一个枚举 SportType

[DataContract]
public enum SportType {
    [EnumMember]
    None,
    [EnumMember]
    Football,
    [EnumMember]
    Basketball,
    [EnumMember]
    Hockey,
    [EnumMember]
    All
};

[DataContract]
public class User
{
    // Data fields
    [DataMember]
    public int Id { get; set; } = -1;
    [DataMember]
    public string Name { get; set; } = string.Empty;
    [DataMember]
    public string HashMark { get; set; } = string.Empty;
    [DataMember]
    public string UID { get; set; } = string.Empty; 
    [DataMember]
    public string LastIP { get; set; } = string.Empty; 
    [DataMember]
    public string IsOnline { get; set; } = "-";
    [DataMember]
    public string ScreenshotsCount { get; set; } = "0";
    [DataMember]
    public DateTime RegDate { get; set; } = new DateTime();

    [DataMember]
    public string FootballLicenseFinalDay { get; set; } = string.Empty;
    [DataMember]
    public string BasketballLicenseFinalDay { get; set; } = string.Empty;
    [DataMember]
    public string HockeyLicenseFinalDay { get; set; } = string.Empty;

    [DataMember]
    public string Login { get; set; } = string.Empty;
    [DataMember]
    public string Password { get; set; } = string.Empty;
    [DataMember]
    public string Email { get; set; } = string.Empty;

    [DataMember]
    public bool IsBlocked {
        get
        {
            return BlockReason == "";
        }
    }
    [DataMember]
    public string BlockReason { get; set; } = string.Empty; 

    [DataMember]
    public List<SportType> PurchasedSportTypes { get; set; } = new List<SportType>();

    // Logic fields
    [DataMember]
    public string RegDateString
    {
        get
        {
            return RegDate.ToShortDateString();
        }
    }

    [DataMember]
    public string IsFootball
    {
        get
        {
            string isPurchased = "-";

            try
            {
                isPurchased = PurchasedSportTypes.Contains(SportType.Football) ? "Av" : "-";
            }
            catch { }

            return isPurchased;
        }
    }
    [DataMember]
    public string IsBasketball
    {
        get
        {
            string isPurchased = "-";

            try
            {
                isPurchased = PurchasedSportTypes.Contains(SportType.Basketball) ? "Av" : "-";
            }
            catch { }

            return isPurchased;
        }
    }
    [DataMember]
    public string IsHockey
    {
        get
        {
            string isPurchased = "-";

            try
            {
                isPurchased = PurchasedSportTypes.Contains(SportType.Hockey) ? "Av" : "-";
            }
            catch { }

            return isPurchased;
        }
    }


    // Help methods
    public static SportType GetSportTypeByString(string sportName)
    {
        return "";
    }
}

当我从客户端在 WCF端点上进行身份验证时,如果登录和传递成功,则服务器会向我返回此 User 对象的实例。 而且,实际上,这就是问题所在。

public User Auth(string login, string pass, string hid /* Hardware ID */)
{ return new User(); } // Current WCF Method code

这是我的WCF身份验证方法。我已经注释了所有的Auth代码,只保留了此返回的空User实例。 但是,此WCF方法仍会引发异常,并将System.ServiceModel.Channels.ServiceChannel状态设置为Faulted。实际上,使用此 User 类的其他方法也将ServiceChannel状态设置为Faulted。但是,其余的WCF方法都可以正常工作(它们的签名中没有 User 对象)。

客户端上,我使用以下方法调用该方法:

                    var factory = new System.ServiceModel.DuplexChannelFactory<IServer>(
                    new InstanceContext(new CallbackMaster()),
                        new WSDualHttpBinding()
                        {
                            ReceiveTimeout = new TimeSpan(0, 0, 0, 90, 0),
                            SendTimeout = new TimeSpan(0, 0, 0, 60, 0),
                            ClientBaseAddress = new Uri("http://" + IPAddressHelper.GetLocalIP() + ":8890")
                        },
                        new EndpointAddress("http://" + IPAddressHelper.GetLocalIP() + ":8891/Server"));

                var proxy = factory.CreateChannel();
                User user = ServerProxy.Auth("login","12345",""); // Exception goes here

异常消息和StackTrace:

消息: 无法使用通信对象System.ServiceModel.Channels.ServiceChannel进行连接,因为他处于故障状态。

StackTrace:    ·System.ServiceModel.Channels.CommunicationObject.ThrowIfFaulted()    •System.ServiceModel.Channels.ServiceChannel.Call(字符串操作,布尔型单向,ProxyOperationRuntime操作,Object [] ins,Object [] outs,TimeSpan超时)    ·System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage方法调用,ProxyOperationRuntime操作)    •System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息) 异常重新抛出为[0]:    ·System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)    System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32类型)    •Administrator.ServiceReference.IServer.Auth(字符串登录,字符串传递,字符串隐藏)    在... \ View \ MainWindow.xaml.cs:строка111

中为Administrator.MainWindow。 d.MoveNext()

该如何解决?

0 个答案:

没有答案