【问题标题】:How to customize WCF error behavior如何自定义 WCF 错误行为
【发布时间】:2011-07-08 04:17:01
【问题描述】:

我正在创建一个 WCF 服务来提供 REST-ful Web HTTP 服务。这就是我想要的:

http://myservice/correct/url

这应该以 JSON/XML 格式返回结果

http://myservice/incorrect/url

这应该返回自定义的错误消息,而不是标准的“未找到端点”。错误。

我了解了 IErrorHandler 接口,并扩展了 WebHttpBehavior 类。但不起作用。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    最简单的方法是定义操作以处理“未找到”情况 - 通过使用 '*' 的 UriTemplate(如果没有其他模板与传入请求匹配,它将匹配)。下面的代码展示了它的一个例子。

    public class Post_0424d917_89cd_43c8_be70_5d4c6934b48c
    {
        [ServiceContract]
        public interface ITest
        {
            [WebGet(UriTemplate = "/Echo?text={text}")]
            string EchoGet(string text);
            [WebInvoke(UriTemplate = "/Echo")]
            string EchoPost(string text);
            [WebGet(UriTemplate = "*")]
            Stream ErrorForGet();
            [WebInvoke(UriTemplate = "*")]
            Stream ErrorForPost();
        }
        public class Service : ITest
        {
            public string EchoGet(string text) { return text; }
            public string EchoPost(string text) { return text; }
            public Stream ErrorForPost() { return this.ErrorForGet(); }
            public Stream ErrorForGet()
            {
                WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
                WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
                string result = @"<html>
      <head>
        <title>Resource not found</title>
      </head>
      <body>
        <h1>This resource cannot be found</h1>
      </body>
    </html>";
                return new MemoryStream(Encoding.UTF8.GetBytes(result));
            }
        }
        static void SendRequest(string uri, string method, string contentType, string body)
        {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
            req.Method = method;
            if (!String.IsNullOrEmpty(contentType))
            {
                req.ContentType = contentType;
            }
    
            if (body != null)
            {
                byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
                req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
                req.GetRequestStream().Close();
            }
    
            HttpWebResponse resp;
            try
            {
                resp = (HttpWebResponse)req.GetResponse();
            }
            catch (WebException e)
            {
                resp = (HttpWebResponse)e.Response;
            }
    
            Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
            foreach (string headerName in resp.Headers.AllKeys)
            {
                Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
            }
    
            Console.WriteLine();
            Console.WriteLine(new StreamReader(resp.GetResponseStream()).ReadToEnd());
    
            Console.WriteLine();
            Console.WriteLine("  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  ");
            Console.WriteLine();
        }
        public static void Test()
        {
            string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
            ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
            host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
            host.Open();
            Console.WriteLine("Host opened");
    
            SendRequest(baseAddress + "/Echo?text=hello", "GET", null, null);
            SendRequest(baseAddress + "/Echo", "POST", "application/json", "\"world\"");
    
            SendRequest(baseAddress + "/NotFound", "GET", null, null);
            SendRequest(baseAddress + "/NotFound", "POST", "text/xml", "<body/>");
    
            Console.Write("Press ENTER to close the host");
            Console.ReadLine();
            host.Close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 2016-09-29
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多