在WCF服务上404。启动主机并获取正确的URI

时间:2012-02-27 08:21:24

标签: asp.net rest

我正在尝试创建我的第一个WCF restful服务。 我正在阅读此页面:http://www.colossaltechnologies.com/vd/index.php?option=com_content&view=article&id=128%3Awcf-rest-example&Itemid=101 并试图复制代码。

但是,我没有在单独的应用程序中使用我的WCF服务,而是将其添加到我现有的Web应用程序项目中。 在我当前项目的范围之外不需要该服务。我想要实现的是,可以从需要开发的移动应用程序(iOS / Android / Windows Phone)调用此休息服务。

我注意到上面教程中的这一步:“为服务创建一个主机” 现在,我想知道的是:确实需要运行该代码来启动主机,如果是这样的话:何时以及如何在生产服务器上运行它?

似乎我确实需要对主持人做点什么,因为现在我去了:www.test.com/api/job/bob(我也试过www.test.com/job/bob)。 我得到了404.

在我的代码下面,我希望任何人都能看到我做错了什么。我没有使用正确的URI吗?我错过了什么?

Iweddingservice.vb

Imports System.ServiceModel
Imports System.Web
Imports System.IO
Imports System.Runtime.Remoting.Activation
Imports System.Collections.Generic

Namespace RestService
<ServiceContract()>
Public Interface Iweddingservice

    <OperationContract()> _
<Web.WebGet(UriTemplate:="job/{name}")> _
    Function DoJob(name As String) As String

End Interface
End Namespace

** weddingservice.svc.vb **

Imports System.ServiceModel
Imports System.ServiceModel.Web
Imports System.IO
Imports System.ServiceModel.Activation
Imports System.Web.Script.Serialization
Imports System.Collections.Generic
Imports System.Xml
Imports System.Net

Namespace RestService
Public Class weddingservice
    Implements Iweddingservice

    Public Function DoJob(name As String) As String Implements Iweddingservice.DoJob
        Return String.Format("Hello, {0}", name)
    End Function


End Class

End Namespace

** web.config **

<rewrite>
<rules>
<rule name="api access 2">
 <match url="^api/job/$" />
 <action type="Rewrite" url="weddingservice.svc/api/job/" appendQueryString="true" />
</rule>
</rules>
</rewrite>

<system.serviceModel>
  <services>
    <service name="RestService.weddingservice">
      <host>
        <baseAddresses>
          <add baseAddress="http://www.test.com/api"/>
        </baseAddresses>
      </host>
      <endpoint binding="webHttpBinding" contract="RestService.Iweddingservice" />
    </service>
  </services>
</system.serviceModel>

2 个答案:

答案 0 :(得分:1)

您是否尝试在IIS中托管服务?如果是,那么您不能提供地址部分 - 它将隐含在服务文件位置。例如,如果您的服务文件weddingservice.svc位于根位置,则使用

等配置
   <system.serviceModel>
     <services>
        <service name="WeddingService">
            <endpoint binding="webHttpBinding" contract="RestService.Iweddingservice"
                      behaviorConfiguration="webHttp"/>
        </service>
     </services>
     <behaviors>
        <endpointBehaviors>
            <behavior name="webHttp">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
     </behaviors>
  </system.serviceModel>

现在,您可以在http://localhost/weddingservice.svc说明您的服务,在http://localhost/weddingservice.svc/job处使用您的方法。您可以拥有网站支持的任何主机地址(例如www.xyz.com),而不是localhost。要消除.svc扩展,您需要使用重写规则或ASp.NET路由。

最后,如果您没有在IIS中托管服务,请检查您的基本地址 - 是否正确设置DNS等。尝试使用没有任何重写的网址,例如www.test.com/weddingservice.svc/job

编辑:您似乎仍然坚持这一点。我建议您按照以下分步教程进行操作:

这个应该让你开始: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

这有点详细,涵盖了很多内容: http://blogs.msdn.com/b/endpoint/archive/2010/01/07/getting-started-with-wcf-webhttp-services-in-net-4.aspx

以下是其他有用的链接:

http://msdn.microsoft.com/en-us/library/bb412172.aspx
http://msdn.microsoft.com/en-us/library/dd203052.aspx
http://msdn.microsoft.com/en-us/library/dd699772.aspx(更多样本)

答案 1 :(得分:0)

继续关注WCF的ABC。 您打算如何托管您的服务? 如果您计划使用IIS托管服务,则将忽略基本地址标记