FaultContract<T> - what does it means?

139 Views Asked by At

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<>]

1

There are 1 best solutions below

0
On

Sorry about the late answer, but I faced something similar and I would like to share what I've found:

FaultContract is possible: https://msdn.microsoft.com/en-us/library/ff650547.aspx

From MSDN:

To support the use of custom faults, WCF services use the FaultContractAttribute to formally specify faults that can be returned from a service operation. Types specified in a FaultContractAttribute must be serializable as DataContract,SerializableAttribute, or ISerializable. When a FaultException is thrown using a custom fault defined in FaultContract, client applications can also catch these specific faults using the FaultException generic type.

Example:

throw new FaultException<InvalidNameFault>(fault, "Invalid Name!");