我从两个不同的终端提供WCf服务。
<service name="AB.Service.AccountService">
<endpoint address="Account" binding="basicHttpBinding" bindingConfiguration="regularBinding" contract="AB.Service.Library.IAccountService" />
<endpoint address="M_Account" binding="webHttpBinding" bindingConfiguration="mobileHttpBinding" contract="AB.Service.Library.IMAccountService" behaviorConfiguration="restBehaviour" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
示例界面:
[StandardFaults]
[ServiceContract]
public interface IAccountService : INAccountService, IMAccountService
{
}
/// <summary>
/// This interface contains methods that are called from only web and desktop clients(only soap endpoint)
/// </summary>
[ServiceContract]
public interface INAccountService
{
[OperationContract]
Task<AccountContract> GetAccountInformation ( Session session );
}
/// <summary>
/// This interface contains methods that are called from both web and desktop and rest clients(soap and rest endpoint)
/// </summary>
[ServiceContract]
public interface IMAccountService
{
[OperationContract,
WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "GetUserType")]
Task<UserType> GetUserType ( string logonName );
[OperationContract,
WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "LoginCheck")]
bool LoginCheck ( Session session );
}
这样,soap和rest客户端都可以使用此服务。
我想知道的是我可以在休息调用中更改返回类型。这意味着让我们在LoginCheck方法中说它将在soap调用中返回true
但在休息调用中我会以某种方式将它包装在泛型类中并且它将返回:
{
success: true,
message: "authorization successful"
}
为什么我愿意这是因为从某些方法中我将[StandartFaults]
合同行为中定义的异常作为业务逻辑的一部分。但是,从其他客户处理此类异常并不容易。因此,我想要一种方法来提供这样的功能。
我的替代解决方案是创建一个独立的仅休息服务,并处理方法中的所有异常并通过将它们包装成我想要的方式返回它们。我并不是真的喜欢这种方法,因为它会降低服务方法的可重用性,同样也会降低可维护性。