WCF服务未返回GET方法不允许使用方法

时间:2017-05-14 09:24:27

标签: c# wcf http

我创建了一个自托管的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动词的“Method Not Allowed”。我该怎么办?

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

1 个答案:

答案 0 :(得分:0)

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