2011-08-10 13 views
9

में WebHttp सहायता आउटपुट को अनुकूलित करें webHttp बाइंडिंग और helpEnabled = "true" के साथ एक सेवा के लिए, सहायता जानकारी ठीक से उत्पन्न हो रही है। यह सोच रहा था कि आउटपुट को किसी भी तरह से नियंत्रित करना संभव है या नहीं। नीचे वे बदलाव हैं जिन्हें मैं बनाना चाहता हूं।डब्ल्यूसीएफ

  1. JSON मदद निकालें के रूप में सेवा JSON उत्पादन का समर्थन नहीं करता। इसे वहां रखने से उपयोगकर्ताओं को भ्रमित हो सकता है। कम से कम, एक नोट आउटपुट करने का एक तरीका है कि JSON समर्थित नहीं है।
  2. उदाहरण के लिए दिखाए गए उत्तरों के लिए, "स्ट्रिंग सामग्री" से बेहतर उदाहरण का उपयोग करें जहां वास्तविक मूल्य नमूने निर्दिष्ट किए जा सकते हैं। अच्छा होगा अगर मैं ऑब्जेक्ट की आपूर्ति कर सकता हूं जिसका उपयोग उदाहरण के रूप में क्रमबद्ध होने के लिए किया जाता है।

उत्तर

5

डब्ल्यूसीएफ वेबहट्प एंडपॉइंट्स के लिए सहायता पृष्ठ को अनुकूलित नहीं किया जा सकता है। हालांकि, आप इसे अपने स्वयं के कस्टम पेज से बदल सकते हैं। इसे कार्यान्वित करना आसान है, लेकिन यह लिखने के लिए बहुत सारे कोड हैं। नीचे दिया गया उदाहरण दिखाता है कि यह एक सरल सेवा के लिए कैसे किया जा सकता है।

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(); 
    } 
} 
+0

इसके लिए समय लेने के लिए धन्यवाद। इसे पूरी तरह से कस्टम करना ऐसा नहीं है जिसे मैं ढूंढ रहा था। मैंने देखा है कि एक और दृष्टिकोण जवाब संदेश और परिवर्तन का निरीक्षण करना है, जो भी आदर्श नहीं है। एक ऐसे तरीके की तलाश में था जहां इसे गुण या कॉन्फ़िगरेशन द्वारा नियंत्रित किया जा सके। ऐसा लगता है कि ऐसा कोई रास्ता नहीं है। – fozylet

+0

एचएम, मैं वास्तव में हमारी सेवा में काम करने के लिए 'वेबगेट (UriTemplate = "/ help") प्राप्त करने में सक्षम नहीं हूं। मेरी सेवा में परिभाषित अन्य यूआरआई ठीक काम करते हैं, लेकिन वह नहीं। – ladenedge