【问题标题】:WCF Service not returning Method not allowed for GET methodWCF 服务未返回 GET 方法不允许的方法
【发布时间】:2017-10-13 05:23:29
【问题描述】:

我创建了一个自托管的 wcf 服务,并且如果在 http://localhost:11100 上很热。我想用自定义动词调用服务,例如“VERB”。对于剩余的默认动词,它应该返回 Method not allowed(405)。

我试过的是

请求:

 ANYVERB http://localhost:11100 HTTP 1.1

回应:

“方法不允许”

请求:

 GET http://localhost:11100 HTTP 1.1

回应:

<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 服务主机:

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

【问题讨论】:

  • 请添加您的代码
  • HTTP 中需要支持 GET。
  • 我只需要允许自定义动词,不应该允许默认动词。

标签: c# wcf http


【解决方案1】:

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

【讨论】:

  • 服务帮助页的地址可以改吗
  • WCF 生成的服务页面是硬连线的,我从来没有听说过任何技巧或技术来改变它。在这里你可以阅读更多:stackoverflow.com/questions/7229579/…
猜你喜欢
  • 2010-09-07
  • 2012-04-21
  • 1970-01-01
  • 2018-07-21
  • 2017-08-25
  • 2016-09-20
  • 2011-03-04
  • 1970-01-01
相关资源
最近更新 更多