2017-05-14 119 views
1

我已经创建了一个自我托管的wcf服务,并且在http://localhost:11100处有热点。我想用自定义动词来调用服务,例如“VERB”。对于剩余的默认动词,它应该返回方法不允许(405)。WCF服务不返回方法不允许使用GET方法

我已经试过是

请求:

ANYVERB http://localhost:11100 HTTP 1.1 

响应:

“不允许的方法”

请求:

GET http://localhost:11100 HTTP 1.1 

Respon se:

<HTML><HEAD><link rel="alternate" type="text/xml" href="http://127.0.0.1:11100/?disco" /><STYLE type="text/css">#content{ FONT-SIZE: 0.7em; PADDING-BOTTOM: 2em; MARGIN-LEFT: 30px}BODY{MARGIN-TOP: 0px; MARGIN-LEFT: 0px; COLOR: #000000; FONT-FAMILY: Verdana; BACKGROUND-COLOR: white}P{MARGIN-TOP: 0px; MARGIN-BOTTOM: 12px; COLOR: #000000; FONT-FAMILY: Verdana}PRE{BORDER-RIGHT: #f0f0e0 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #f0f0e0 1px solid; MARGIN-TOP: -5px; PADDING-LEFT: 5px; FONT-SIZE: 1.2em; PADDING-BOTTOM: 5px; BORDER-LEFT: #f0f0e0 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: #f0f0e0 1px solid; FONT-FAMILY: Courier New; BACKGROUND-COLOR: #e5e5cc}.heading1{MARGIN-TOP: 0px; PADDING-LEFT: 15px; FONT-WEIGHT: normal; FONT-SIZE: 26px; MARGIN-BOTTOM: 0px; PADDING-BOTTOM: 3px; MARGIN-LEFT: -30px; WIDTH: 100%; COLOR: #ffffff; PADDING-TOP: 10px; FONT-FAMILY: Tahoma; BACKGROUND-COLOR: #003366}.intro{MARGIN-LEFT: -15px}</STYLE><TITLE>RDService Service</TITLE></HEAD><BODY><DIV id="content"><P class="heading1">RDService Service</P><BR /><P class="intro">You have created a service.<P class="intro">To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:</P><BR /><PRE>svcutil.exe <A HREF="http://127.0.0.1:11100/?wsdl">http://127.0.0.1:11100/?wsdl</A></PRE><P>You can also access the service description as a single file:<BR /><PRE><A HREF="http://127.0.0.1:11100/?singleWsdl">http://127.0.0.1:11100/?singleWsdl</A></PRE></P></P><P class="intro" />This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:<BR /><P class="intro"><B>C#</B></P><PRE><font color="blue">class </font><font color="teal">Test 
</font>{ 
<font color="blue"> static void </font>Main() 
    { 
     <font color="teal">HelloClient</font> client = <font color="blue">new </font><font color="teal">HelloClient</font>(); 

<font color="green">  // Use the 'client' variable to call operations on the service. 

</font><font color="green">  // Always close the client. 
</font>  client.Close(); 
    } 
} 
</PRE><BR /><P class="intro"><B>Visual Basic</B></P><PRE><font color="blue">Class </font><font color="teal">Test 
</font><font color="blue"> Shared Sub </font>Main() 
<font color="blue">  Dim </font>client As <font color="teal">HelloClient</font> = <font color="blue">New </font><font color="teal">HelloClient</font>() 
<font color="green">  ' Use the 'client' variable to call operations on the service. 

</font><font color="green">  ' Always close the client. 
</font>  client.Close() 
<font color="blue"> End Sub 
</font><font color="blue">End Class</font></PRE></DIV></BODY></HTML> 

我想获得“方法不允许”的GET动词也。我该如何继续。

我的WCF接口:

namespace sample 
{ 
    [ServiceContract] 
    interface Iinterface 
    { 
     [OperationContract] 
     [WebInvoke(UriTemplate = "/", Method = "MYVERB", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)] 
     XmlElement MyMethod(); 
    } 
} 

WCF的ServiceHost:

namespace sample 
{ 
    class SHClass 
    { 
     private static ServiceHost m_Host; 
     public static void onStart() 
     { 
      try 
      { 

       if (m_Host != null) 
       { 
        m_Host.Close(); 
       } 
      string url= "http://127.0.0.1:11100"; 
        Uri baseAddress = new Uri(url); 
        try 
        { 
         m_Host = new ServiceHost(typeof(sample.class1), baseAddress); 

         //Start the Service 
         m_Host.Open(); 
        } 

       } 
      } 
      catch (Exception ex) 
      { 
       throw new Exception(ex.ToString()); 
      } 
     } 

     public static void onStop() 
     { 
      try 
      { 
       if (m_Host != null) 
       { 
        m_Host.Close(); 
        m_Host = null; 
       } 
      } 
      catch (Exception ex) 
      { 
       throw new Exception(ex.ToString()); 
      } 
     } 
    } 
} 
+0

请将您的代码添加到 –

+0

HTTP中支持*是必需*。 –

+0

我只需要允许自定义动词,不应该允许默认动词。 –

回答

0

你问同样的问题两次。在你的第一个问题中,我回答不可能。但知道我发现问题在哪里。 您的服务帮助页面和端点地址相同。因此当您发送GET请求时,WCF控制器会将其映射到帮助页面。试试这个:GET http://localhost:11100/或更改你的UriTemplate

+0

是否可以更改服务帮助页面的地址。 –

+0

WCF生成的服务页面是硬连线的,我从未听说过任何改变它的技巧或技巧。在这里你可以阅读更多:http://stackoverflow.com/questions/7229579/how-can-i-change-an-html-output-of-wcf-service-with-my-own-content – David

相关问题