Until now I have used an webservice ASMX which I call from javascript. It has been working fine, but because I needed some control over the serialization, I am trying to switch to DataContracts and WCF. However, I do believe that there is something I have misunderstood, because Im trying to catch faultexceptions from the client code (javascript), but the only errorcode Im receiving is: (tr开发者_如何学Canslated, so might not be accurate)
"The server could not handle the request because of an intern error. You can get more info about the error by activate IncludeExceptionDetailInFaults..."
I have tried to set IncludeExceptionDetailInFauls to true which gives some info, but as far as I understood, the whole point by throwing FaultExceptions is to hide exceptions from the user, and only throw a little info? Im trying with a IErrorHandler, but I have created a simpler example:
The SVC:
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WcfTest
{
// To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
// To create an operation that returns XML,
// add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
// and include the following line in the operation body:
// WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
[OperationContract]
public string DoOk()
{
// Add your operation implementation here
return "Success";
}
[OperationContract]
public string DoFail()
{
FaultReason reason = new FaultReason("Fail <- This is good :)");
throw new FaultException(reason);
return "Success";
}
}
which is included with a scriptmanager, and called by the following javascript:
<a href="java script:void(0);" onclick="DoOk()">OK</a><br />
<a href="java script:void(0);" onclick="DoFail()">Fail</a>
<script>
function DoOk() {
WcfTest.DoOk(
function (result) {
alert("DoOk Success: "+result);
},
function (result) {
alert("DoOk Failed: " + result);
})
}
function DoFail() {
WcfTest.DoFail(
function (result) {
alert("DoFail Success: " + result);
},
function (result) {
alert("DoFail Failed: " + result);
})
}
</script>
and a service model:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WcfTestAspNetAjaxBehavior">
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WcfTestAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="WcfTest">
<endpoint address="" behaviorConfiguration="WcfTestAspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfTest" />
</service>
</services>
</system.serviceModel>
Ive also tried to mark the DoFail method with combinations of:
[FaultContract(typeof(FaultException))]
[FaultContract(typeof(FaultReason))]
Have I misunderstood something? Shouldnt you be able to get exception info back by throwing an FaultException
without having to set IncludeExceptionDetailInFaults
to true?
Useful resources:
- Getting jQuery and WCF to talk
- Error handling with WebHttpBinding for Ajax/JSON
Main steps:
- Create your own error handler by impelemting IErrorHandler interface
- Create subclass of WebHttpBehavior and replace the default error handler with you own error handler
- Plug-in your custom/extended behavior by using a custom ServiceHostFactory or define as behaviorExtension in web.config
精彩评论