0

I should create wcf service that returns data about users, the interface and DataContract is below:

[ServiceContract]
public interface IUserInfoProvider{
[FaultContract<UserNotFound>]
public UserInfo GetUserInfo(Guid userId)}

[DataContract]
public class UserInfo
{
     [DataMember] public Guid UserId { get; set; } 
     [DataMember] public bool? AdvertisingOptIn { get; set; } 
     [DataMember] public string CountryIsoCode { get; set; }
     [DataMember] public DateTime DateModified { get; set; }
     [DataMember] public string Locale { get; set; }
}

I have no special client for service - requests (get, post) runs from fiddler or rest plugin for browser.

Please, describe how to implement [FaultContract<>] in my case, i saw examples with [FaultContract(typeof(UserNotFound))] but never seen [FaultContract<>]

4

1 回答 1

0

对于迟到的答案感到抱歉,但我遇到了类似的事情,我想分享我的发现:

FaultContract 是可能的:https ://msdn.microsoft.com/en-us/library/ff650547.aspx

来自 MSDN:

为了支持使用自定义故障,WCF 服务使用 FaultContractAttribute 正式指定可以从服务操作返回的故障。FaultContractAttribute 中指定的类型必须可序列化为 DataContract、SerializableAttribute 或 ISerializable。当使用 FaultContract 中定义的自定义故障引发 FaultException 时,客户端应用程序也可以使用 FaultException 泛型类型捕获这些特定故障。

例子:

throw new FaultException<InvalidNameFault>(fault, "Invalid Name!");
于 2016-08-23T16:19:15.060 回答