本文介绍了在 WCF 中自定义 WebHttp 帮助输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有 webHttpBinding 和 helpEnabled="true" 的服务,可以正确生成帮助信息.想知道是否有可能以某种方式控制输出.以下是我想要进行的更改.

  1. 删除 JSON 帮助,因为该服务不支持 JSON 输出.把它放在那里可能会让用户感到困惑.至少,有一种方法可以输出一条说明不支持 JSON 的注释.
  2. 对于显示的示例回复,使用比字符串内容"更好的示例,其中可以指定实际值样本.如果我可以提供用于序列化的对象作为示例,那就太好了.
解决方案

无法自定义 WCF WebHttp 端点的帮助页面.但是,您可以将其替换为您自己的自定义页面.实现起来很容易,但是要写很多代码.下面的例子展示了如何为一个简单的服务做到这一点.

公共类 StackOverflow_7005187{[数据合约]公开课 MyDC{[数据成员]public string str = "字符串";}[服务合约]公共接口 ITest{[网络调用]字符串 EchoString(字符串文本);[网络获取]int Add(int x, int y);[网络调用]MyDC EchoDC(MyDC 输入);[WebGet(UriTemplate = "/help")]消息 GetMainHelpPage();[WebGet(UriTemplate = "/help/operations/EchoDC")]消息 GetOperationsEchoDCHelpPage();//其他未实现操作的帮助}公共类服务:ITest{公共字符串 EchoString(字符串文本){返回文本;}public int Add(int x, int y){返回 x + y;}公共 MyDC EchoDC(MyDC 输入){返回输入;}公共消息 GetMainHelpPage(){字符串页面 = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><头><title>http://localhost:8000/Service</title>上的操作<style>BODY { 颜色:#000000;背景颜色:白色;字体系列:Verdana;左边距:0px;边距顶部:0px;} #content { margin-left: 30px;字体大小:.70em;填充底部:2em;} A:link { 颜色:#336699;字体粗细:粗体;文字装饰:下划线;A:访问过{颜色:#6699cc;字体粗细:粗体;文字装饰:下划线;} A:active { 颜色:#336699;字体粗细:粗体;文字装饰:下划线;} .heading1 { 背景色:#003366;边框底部:#336699 6px 实心;颜色:#ffffff;字体系列:Tahoma;字体大小:26px;字体粗细:正常;边距:0em 0em 10px -20px;填充底部:8px;padding-left: 30px;padding-top: 16px;} pre { font-size:small;背景颜色:#e5e5cc;填充:5px;字体系列:Courier New;边距顶部:0px;边框:1px #f0f0e0 实心;空白:预包装;空白:-预包装;自动换行:断字;} table { 边框折叠:折叠;边框间距:0px;font-family: Verdana;} table th { border-right: 2px white solid;边框底部:2px 白色实心;字体粗细:粗体;背景色:#cecf9c;} 表 td { 右边框:2px 白色实心;边框底部:2px 白色实心;背景颜色:#e5e5cc;}</style><身体><div id=""内容""><p class=""heading1"">http://localhost:8000/Service</p>上的操作<p>该页面描述了该端点的服务操作.</p><表格><tr><th>Uri</th><th>方法</th>描述</tr><tr><td>添加</td><td title=""http://localhost:8000/Service/Add?x={X}&amp;y={Y}""><a rel=""operation"" href=""help/operations/Add"">GET</a></td><td>Service at http://localhost:8000/Service/Add?x={X}&amp;y={Y}</td></tr><tr><td>EchoDC</td><td title=""http://localhost:8000/Service/EchoDC""><a rel=""operation"" href=""help/operations/EchoDC"">POST</a></td><td>服务位于 http://localhost:8000/Service/EchoDC</td></tr><tr><td>EchoString</td><td title=""http://localhost:8000/Service/EchoString""><a rel=""operation"" href=""help/operations/EchoString"">POST</a></td><td>服务在 http://localhost:8000/Service/EchoString</td></tr>

</html>";返回 WebOperationContext.Current.CreateStreamResponse(new MemoryStream(Encoding.UTF8.GetBytes(page)),"文本/html");}公共消息 GetOperationsEchoDCHelpPage(){字符串页面 = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><头><title>参考 http://localhost:8000/Service/EchoDC</title><style>BODY { 颜色:#000000;背景颜色:白色;字体系列:Verdana;左边距:0px;边距顶部:0px;} #content { margin-left: 30px;字体大小:.70em;填充底部:2em;} A:link { 颜色:#336699;字体粗细:粗体;文字装饰:下划线;A:访问过{颜色:#6699cc;字体粗细:粗体;文字装饰:下划线;} A:active { 颜色:#336699;字体粗细:粗体;文字装饰:下划线;} .heading1 { 背景色:#003366;边框底部:#336699 6px 实心;颜色:#ffffff;字体系列:Tahoma;字体大小:26px;字体粗细:正常;边距:0em 0em 10px -20px;填充底部:8px;padding-left: 30px;padding-top: 16px;} pre { font-size:small;背景颜色:#e5e5cc;填充:5px;字体系列:Courier New;边距顶部:0px;边框:1px #f0f0e0 实心;空白:预包装;空白:-预包装;自动换行:断字;} table { 边框折叠:折叠;边框间距:0px;font-family: Verdana;} table th { border-right: 2px white solid;边框底部:2px 白色实心;字体粗细:粗体;背景色:#cecf9c;} 表 td { 右边框:2px 白色实心;边框底部:2px 白色实心;背景颜色:#e5e5cc;}</style><身体><div id=""内容""><p class=""heading1"">参考 http://localhost:8000/Service/EchoDC</p><p></p><p xmlns=""http://www.w3.org/1999/xhtml""><b>网址:</b><span class=""uri-template"">http://localhost:8000/Service/EchoDC</span></p><p xmlns=""http://www.w3.org/1999/xhtml""><b>HTTP 方法:</b><span class=""method"">POST</span></p><表格><tr><th>消息方向</th><th>格式</th><th>身体</th></tr><tr><td>请求</td><td>Xml</td><td><a href=""#request-xml"">示例</a></td></tr><tr><td>请求</td><td>Json</td><td><a href=""#request-json"">示例</a></td></tr><tr><td>响应</td><td>Xml</td><td><a href=""#response-xml"">示例</a></td></tr><p><a name=""#request-xml"">以下是一个示例请求 Xml 正文:</a><pre class=""request-xml"">&lt;StackOverflow_7005187.MyDC xmlns=""http://schemas.datacontract.org/2004/07/WcfForums"">&lt;str>这是修改后的字符串内容&lt;/str>&lt;/StackOverflow_7005187.MyDC></pre></p><p><a name=""#request-json"">以下是一个示例请求 Json 正文:</a><pre class=""request-json"">{""str"":""JSON 格式的一些内容""}

</p><p><a name=""#response-xml"">以下是示例响应 Xml 正文:</a><pre class=""response-xml"">&lt;StackOverflow_7005187.MyDC xmlns=""http://schemas.datacontract.org/2004/07/WcfForums"">&lt;str>另一个修改过的XML内容&lt;/str>&lt;/StackOverflow_7005187.MyDC></pre></p>

</html>";返回 WebOperationContext.Current.CreateStreamResponse(new MemoryStream(Encoding.UTF8.GetBytes(page)),"文本/html");}}公共静态无效测试(){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 { HelpEnabled = false });主机.Open();Console.WriteLine("主机打开");Console.Write("按回车键关闭主机");Console.ReadLine();主机.关闭();}}

For a service with webHttpBinding and helpEnabled="true", the help information is getting generated properly. Was wondering if it is possible to control the output somehow. Below are the changes I would like to make.

  1. Remove the JSON help as the service doesn't support JSON output. Having it there might be confusing to users. At least, have a way to output a note saying JSON is not supported.
  2. For the example replies shown, use a better example than "String content" where actual value samples can be specified. Would be good if I can supply the object that is used for being serialized as example.
解决方案

The help page for WCF WebHttp endpoints cannot be customized. You can, however, replace it with your own custom page. It's easy to be implemented, but it's a lot of code to write. The example below shows how this can be done for a simple service.

public class StackOverflow_7005187
{
    [DataContract]
    public class MyDC
    {
        [DataMember]
        public string str = "The string";
    }
    [ServiceContract]
    public interface ITest
    {
        [WebInvoke]
        string EchoString(string text);
        [WebGet]
        int Add(int x, int y);
        [WebInvoke]
        MyDC EchoDC(MyDC input);
        [WebGet(UriTemplate = "/help")]
        Message GetMainHelpPage();
        [WebGet(UriTemplate = "/help/operations/EchoDC")]
        Message GetOperationsEchoDCHelpPage();
        // help for other operations not implemented
    }
    public class Service : ITest
    {
        public string EchoString(string text)
        {
            return text;
        }

        public int Add(int x, int y)
        {
            return x + y;
        }

        public MyDC EchoDC(MyDC input)
        {
            return input;
        }

        public Message GetMainHelpPage()
        {
            string page = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
  <head>
    <title>Operations at http://localhost:8000/Service</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id=""content"">
      <p class=""heading1"">Operations at http://localhost:8000/Service</p>
      <p>This page describes the service operations at this endpoint.</p>
      <table>
        <tr>
          <th>Uri</th>
          <th>Method</th>
          <th>Description</th>
        </tr>
        <tr>
          <td>Add</td>
          <td title=""http://localhost:8000/Service/Add?x={X}&amp;y={Y}"">
            <a rel=""operation"" href=""help/operations/Add"">GET</a>
          </td>
          <td>Service at http://localhost:8000/Service/Add?x={X}&amp;y={Y}</td>
        </tr>
        <tr>
          <td>EchoDC</td>
          <td title=""http://localhost:8000/Service/EchoDC"">
            <a rel=""operation"" href=""help/operations/EchoDC"">POST</a>
          </td>
          <td>Service at http://localhost:8000/Service/EchoDC</td>
        </tr>
        <tr>
          <td>EchoString</td>
          <td title=""http://localhost:8000/Service/EchoString"">
            <a rel=""operation"" href=""help/operations/EchoString"">POST</a>
          </td>
          <td>Service at http://localhost:8000/Service/EchoString</td>
        </tr>
      </table>
    </div>
  </body>
</html>";
            return WebOperationContext.Current.CreateStreamResponse(
                new MemoryStream(Encoding.UTF8.GetBytes(page)),
                "text/html");
        }


        public Message GetOperationsEchoDCHelpPage()
        {
            string page = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"">
<html xmlns=""http://www.w3.org/1999/xhtml"">
  <head>
    <title>Reference for  http://localhost:8000/Service/EchoDC</title>
    <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
  </head>
  <body>
    <div id=""content"">
      <p class=""heading1"">Reference for  http://localhost:8000/Service/EchoDC</p>
      <p></p>
      <p xmlns=""http://www.w3.org/1999/xhtml"">
        <b>Url: </b>
        <span class=""uri-template"">http://localhost:8000/Service/EchoDC</span>
      </p>
      <p xmlns=""http://www.w3.org/1999/xhtml"">
        <b>HTTP Method: </b>
        <span class=""method"">POST</span>
      </p>
      <table>
        <tr>
          <th>Message direction</th>
          <th>Format</th>
          <th>Body</th>
        </tr>
        <tr>
          <td>Request</td>
          <td>Xml</td>
          <td>
            <a href=""#request-xml"">Example</a></td>
        </tr>
        <tr>
          <td>Request</td>
          <td>Json</td>
          <td>
            <a href=""#request-json"">Example</a>
          </td>
        </tr>
        <tr>
          <td>Response</td>
          <td>Xml</td>
          <td>
            <a href=""#response-xml"">Example</a></td>
        </tr>
      </table>
      <p>
        <a name=""#request-xml"">The following is an example request Xml body:</a>
        <pre class=""request-xml"">&lt;StackOverflow_7005187.MyDC xmlns=""http://schemas.datacontract.org/2004/07/WcfForums""&gt;
  &lt;str&gt;This is a modified string content&lt;/str&gt;
&lt;/StackOverflow_7005187.MyDC&gt;</pre>
      </p>
      <p>
        <a name=""#request-json"">The following is an example request Json body:</a>
        <pre class=""request-json"">{
    ""str"":""Some content in JSON""
}</pre>
      </p>
      <p>
        <a name=""#response-xml"">The following is an example response Xml body:</a>
        <pre class=""response-xml"">&lt;StackOverflow_7005187.MyDC xmlns=""http://schemas.datacontract.org/2004/07/WcfForums""&gt;
  &lt;str&gt;Another modified XML content&lt;/str&gt;
&lt;/StackOverflow_7005187.MyDC&gt;</pre>
      </p>
    </div>
  </body>
</html>";
            return WebOperationContext.Current.CreateStreamResponse(
                new MemoryStream(Encoding.UTF8.GetBytes(page)),
                "text/html");
        }
    }
    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 { HelpEnabled = false });
        host.Open();
        Console.WriteLine("Host opened");

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

这篇关于在 WCF 中自定义 WebHttp 帮助输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:30