asp.net wcf服务抛出404

时间:2011-05-02 18:29:05

标签: asp.net wcf

我试图实现一个wcf服务但是收到404错误。我想要一个返回JSON的REST服务。

请参阅下面的代码,但是,当我访问:www.domain.nl/api或访问www.domain.nl/api/getobjects时,我得到了404

我错过了什么?

** * web.config

 <system.serviceModel>
   <services>
     <service name="wcf_geolocation">
       <!-- not sure which class I should name here, see my attached code for definition of interface-->
       <host>
         <baseAddresses>
           <add baseAddress="http://www.domain.nl/api" />
           <!--  /api is the URL I want, do I need to configure that in URL rewrite or IIS or...?-->
         </baseAddresses>
       </host>
       <endpoint address="getobjects" binding="wsHttpBinding" contract="Iwcf_geolocation" />
     </service>
   </services>

   <behaviors>
   <serviceBehaviors>
    <behavior name="">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
  <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
 </system.serviceModel>

** * Iwcf_geolocation.vb

Imports System.ServiceModel
Imports System
Imports System.Collections.Generic
Imports System.ServiceModel.Description
Imports System.ServiceModel.Web
Imports System.Text
' NOTE: You can use the "Rename" command on the context menu to change the interface name "Iwcf_geolocation" in both code and config file together.
<ServiceContract()>
Public Interface Iwcf_geolocation

    <OperationContract()>
    Sub DoWork()


    <OperationContract()> _
<WebGet()> _
    Function EchoWithGet(ByVal s As String) As String

    <OperationContract()> _
    <WebInvoke()> _
    Function EchoWithPost(ByVal s As String) As String


End Interface

** * wcf_geolocation.vb

' NOTE: You can use the "Rename" command on the context menu to change the class name "wcf_geolocation" in code, svc and config file together.
Public Class wcf_geolocation
    Implements Iwcf_geolocation

    Public Sub DoWork() Implements Iwcf_geolocation.DoWork
    End Sub

    Public Function EchoWithGet(ByVal s As String) As String Implements Iwcf_geolocation.EchoWithGet
        Return "You said " + s
    End Function

    Public Function EchoWithPost(ByVal s As String) As String Implements Iwcf_geolocation.EchoWithPost
        Return "You said " + s
    End Function

End Class

2 个答案:

答案 0 :(得分:0)

看起来问题在于绑定REST绑定应该是webHttpBinding。我在你的web.config.try中做了一些修改

<system.serviceModel>
    <services>
      <service name="wcf_geolocation" behaviorConfiguration="ServiceBehavior">
        <!-- not sure which class I should name here, see my attached code for definition of interface-->
        <host>
          <baseAddresses>
            <add baseAddress="http://www.domain.nl/api" />
            <!--  /api is the URL I want, do I need to configure that in URL rewrite or IIS or...?-->
          </baseAddresses>
        </host>
        <endpoint address="getobjects"  behaviorConfiguration="JsonBehavior" binding="webHttpBinding" contract="Iwcf_geolocation" />
      </service>
    </services>

    <behaviors>
      <endpointBehaviors>

        <behavior name="JsonBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

修改

您好我已根据您的评论更新了答案。请看一下并告诉我。

Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics
Imports System.ServiceModel
Imports System.ServiceModel.Description
Imports System.ServiceModel.Web
Imports System.Text
' NOTE: You can use the "Rename" command on the context menu to change the interface name "Iwcf_geolocation" in both code and config file together.
<ServiceContract> _
Public Interface Iwcf_geolocation


    <OperationContract> _
    Sub DoWork()

    <OperationContract> _
    <WebGet(RequestFormat := WebMessageFormat.Json, ResponseFormat := WebMessageFormat.Json, UriTemplate := "EchoWithGet/{s}")> _
    Function EchoWithGet(s As String) As String

    <OperationContract> _
    <WebInvoke(Method := "POST", UriTemplate := "EchoWithPost/{s}", BodyStyle := WebMessageBodyStyle.Bare)> _
    Function EchoWithPost(s As String) As String


End Interface





Imports System.Collections
Imports System.Collections.Generic
Imports System.Data
Imports System.Diagnostics

' NOTE: You can use the "Rename" command on the context menu to change the class name "wcf_geolocation" in code, svc and config file together.
Public Class wcf_geolocation
    Inherits Iwcf_geolocation

    Public Sub DoWork()
    End Sub
    Public Function EchoWithGet(s As String) As String
        Return "You said " & s
    End Function

    Public Function EchoWithPost(s As String) As String
        Return "You said " & s
    End Function

End Class

<小时/>                                                                                                                                                                 

    <behaviors>
      <endpointBehaviors>

        <behavior name="JsonBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

如何在浏览器中测试服务

http:// [XXXX.XXX] /ServiceName.svc/EchoWithGet/peter

对于 EchoWithPost 服务,您必须编写代码,或者您可以使用fiddler对其进行测试。

答案 1 :(得分:0)

运行服务器管理器(在任务栏和开始菜单上) 选择要管理的服务器(可能是本地服务器) 向下滚动到&#34;角色和功能&#34;部分。 选择&#34;添加角色或功能&#34;从任务下拉菜单 在&#34;添加角色或功能向导&#34;对话框,单击下面的&#34;功能&#34;在左侧页面列表中。 展开&#34; .Net 3.5&#34;或者&#34; .Net 4.5&#34;,取决于您安装的内容。 (如果你没有,你可以回到&#34;角色&#34;屏幕添加。 在&#34; WCF服务&#34;下,选中&#34; HTTP-Activation&#34;的复选框。如果您知道需要它们(tcp,命名管道等),也可以添加非http类型。 点击&#34;安装&#34;按钮。