2012-02-27 71 views
0

我试图创建我的第一个WCF宁静服务。 我在读这页:http://www.colossaltechnologies.com/vd/index.php?option=com_content&view=article&id=128%3Awcf-rest-example&Itemid=101 并试图复制代码。WCF服务。启动主机并获取正确的URI

但是,我没有我的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> 

回答

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),而不是本地主机。为了消除.svc扩展名,您需要使用重写规则或ASp.NET路由。

最后,如果你不是在IIS中托管服务,那么检查你的基地址 - 是DNS等设置正确的相同。尝试使用没有任何重写功能的URL。 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(更多示例)

+0

@Floran,如果你看到你的svc文件的标记,那么它将具有属性'service =“TopTrouwen.weddingservice”'。此类名称必须与您的代码隐藏类中的类名匹配。根据您发布的代码,该属性应该读取“service =”RestService.weddingservice“'。当然,确保你没有任何错误地构建你的项目! – VinayC 2012-02-28 04:54:06

+0

即使svc文件的标记显然不正确,就像您提到的那样,我的项目无错地生成了!现在标记为:<%@ ServiceHost Language =“VB”Debug =“true”Service =“RestService.weddingservice”CodeBehind =“weddingservice.svc.vb”%> 我现在得到:类型'RestService.WeddingService',作为ServiceHost指令中的Service属性值提供,或者在配置元素system.serviceModel/serviceHostingEnvironment/serviceActivations中提供。 我能做什么? – Flo 2012-02-28 09:05:43

+0

@Floran,请查看案例 - 是RestService.WeddingService还是RestService.weddingService?该错误仅表明.NET无法找到名为RestService.WeddingService的类型。 – VinayC 2012-02-28 09:24:14

0

只要保持以下的WCF的ABC。 你打算如何主办你的服务? 如果您打算使用IIS托管服务,则基地址标记将被忽略

+0

正确..我将在IIS中承载服务。我现在在我的本地机器上这样做,并将在我的生产服务器上这样做。那么现在怎么办? :) – Flo 2012-02-27 09:42:03