本文介绍了WCF 4.0自托管Json服务中的自定义HTTP状态时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨专家, 我有一个WCF自托管JSon服务, 我想从我的服务返回自定义HTTP状态代码。 设置HTTP状态的代码如下所示。 public void SetResponseHttpStatus(HttpStatusCode statusCode) { try { OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; response.StatusCode = statusCode; // WebOperationContext context = WebOperationContext.Current; // context.OutgoingResponse.StatusCode = statusCode; } catch (例外) {} } 但以上代码是对象引用.........的例外; 我可以解决任何问题......? 请尽快帮助我解决方案 public class MyService:IMyService { // GENERAL / GLOBAL DECLARATION private OutgoingWebResponseContext响应; // 这是您的服务功能,将在呼叫服务期间执行 public string MyServiceFunction(BLAA ..,BLAA ...) { // 指定对象全球声明 response = WebOperationContext.Current.OutgoingResponse ; SetResponseHttpStatus(HttpStatusCode.OK); } private void SetResponseHttpStatus(HttpStatusCode statusCode) { 尝试 { // SET STATUS TO RESPONSE response.StatusCode = statusCode; } catch (例外) {} } } Hi Experts,I have one WCF Self Hosted JSon Service,I want to return custom HTTP Status code from my service.My code for set HTTP Status is as under.public void SetResponseHttpStatus(HttpStatusCode statusCode){ try { OutgoingWebResponseContext response = WebOperationContext.Current.OutgoingResponse; response.StatusCode = statusCode; //WebOperationContext context = WebOperationContext.Current; //context.OutgoingResponse.StatusCode = statusCode; } catch (Exception) {}}But Above code is giving exception of "Object Reference .........";May I Have any solution...?Please help me as soon as possible 解决方案 public class MyService: IMyService{// GENERAL/GLOBAL DECLARATIONprivate OutgoingWebResponseContext response;// THIS IS YOUR WEB SERVICE FUNCTION WHICH WILL EXECUTE WHILE CALLING SERVICEpublic string MyServiceFunction(BLAA.., BLAA...){ // ASSIGN OBJECT TO GLOBAL DECLARATION response = WebOperationContext.Current.OutgoingResponse; SetResponseHttpStatus(HttpStatusCode.OK);}private void SetResponseHttpStatus(HttpStatusCode statusCode){ try { // SET STATUS TO RESPONSE response.StatusCode = statusCode; } catch (Exception) {}}} 这篇关于WCF 4.0自托管Json服务中的自定义HTTP状态时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-27 01:46