2012-05-07 13 views
0

私はこのようなサービス契約があります(ParseWebMessageFormatがために列挙型の解析を包むための方法であることコードでWCFエラーメッセージの返信形式を設定するにはどうすればよいですか?

var selectedFormat = ParseWebMessageFormat(format); 
WebOperationContext.Current.OutgoingResponse.Format = selectedFormat; 

:コードで

[WebGet(UriTemplate = "getdata?key={key}&format={format}")] 
Event[] GetIncidentsXml(string key, string format); 

を、私はこのような応答フォーマットを切り替えています型)

この部分は正常に動作し、渡されたパラメータに応じてXMLまたはJSONを取得します。

ここでは、例外がスローされます。

<ServiceResponse xmlns="http://schemas.datacontract.org/2004/07/IBI.ATIS.Web.ServiceExceptions" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <ErrorCode> 
     <Code>100</Code> 
     <Msg>Invalid Key</Msg> 
    </ErrorCode> 
    <State>fail</State> 
</ServiceResponse> 

戻り値の型:例外がスローされると

var exception = new ServiceResponse 
{ 
    State = "fail", 
    ErrorCode = new ErrorDetail { Code = "100", Msg = "Invalid Key" } 
}; 

throw new WebProtocolException(HttpStatusCode.BadRequest, "Invalid Key.", exception, null); 

、戻り値の型は常に XMLです:渡されています(API)キーが無効である場合、私はこれをやっています変更はサービスメソッドのコードの最初の行であり、例外がスローされる前に発生しています。

リクエストフォーマットに基づいてタイプを返すようにWCFを設定することはできますが、クエリ文字列で渡されたタイプを使用する必要があります。

自動メッセージタイプが設定でオフになっている:

<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" /> 

答えて

0

あなたが何らかの形でデフォルトの応答の形式を指定する必要がありますので。あなたが見ている動作は標準です。特定のヘッダーを表示して、応答形式を決定する必要があります。次に、要求インターセプタを実装して、送信応答形式の必要性を把握します。

質問に答えるには、何も言及されていない場合はデフォルトの書式タイプを使用するか、不適切なリクエストを返す必要があります。 RESTは、SOAPとは異なり、プロトコルよりもパラダイムです。

更新:説明のため。あなたはこのようなことをすることができます。

[ServiceContract()] 
public interface ICustomerService 
{ 
    [OperationContract] 
    [WebGet(UriTemplate ="?format={formatType}",BodyStyle = WebMessageBodyStyle.Bare)] 
    IResponseMessage GetEmCustomers(string formatType); 

} 

public class CustomerService : ServiceBase 
{ 
    IResponseMessage GetEmCustomers(string formatType) 
    { 
    try 
    { 
     var isValid = base.validateformatspecification(formatType);// check to see if format is valid and supported 
     if(!isValid) return base.RespondWithError(formatType,new Error {Msg= "Dude we dont support this format"}); 
     var customers = CustomerProvider.GetAll(); 
     return base.Respond(formatType,customer);// This method will format the customers in desired format,set correct status codes and respond. 
    }catch(Exception ex) 
    { 
     // log stuff and do whatever you want 
     return base.RespondWithError(formatType,new Error{Msg = "Aaah man! Sorry something blew up"});// This method will format the customers in desired format,set correct status codes and respond. 

    }  
    }  
} 

public class ServiceBase 
{ 
    public IResponseMessage Respond<T>(string format,T entity); 
    public IResponseMessage RespondWithError<T>(string format, T errorObject); 

} 

public class Error:IResponseMessage {/*Implementation*/} 

public class GetEmCustomerResponseResource:List<Customer>,IResponseMessage {/* Implementation*/} 

public class GetEmCustomerErrorResponse: IResponseMessage {/* Implementation */} 
+0

私はあなたに従うのか分からない。書式の型は、メソッドのパラメータです。成功した呼び出しは、呼び出しで指定された型として正しく返されます。これは、指定された出力形式で送信されないエラーメッセージだけです。リクエストヘッダーで何を探す必要がありますか? –

+0

したがって、WCFは、エラーをスローすると、設定された出力タイプを無視します。うざい。私はそれが簡単な設定の変更か、私が見落としていたと思っていた。ご協力いただきありがとうございます。 –

関連する問題