2010-07-12 38 views
1

託管WCF服務我有一個WCF REST服務暴露在課堂GreetService的方法:REST方法不能訪問時,在IIS

[ServiceContract] 
public class GreetService 
{ 
    [WebGet(UriTemplate = "greet/{name}")] 
    public String GreetName(string name) 
    { 
     return "Hello " + name; 
    } 
} 

而且,我註冊在Global.asax中的路由:

RouteTable.Routes.Add(new ServiceRoute("GreetService", new WebServiceHostFactory(), typeof(GreetService))); 

現在,當我運行這個直接從Visual Studio中,我能夠利用UriTemplate和調用使用GET調用這個方法來 http://localhost:5432/GreetService/greet/JohnDoe

不過,通過創建它Greet.svc文件部署這IIS7後,我觀察以下行爲:

任何想法爲什麼WebGetAttribute不能在IIS中工作?還是有什麼我做錯了?

編輯: 這是我的web.config文件的ServiceModel一部分駐留在IIS使用的目錄:

<system.serviceModel> 
    <!-- <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> --> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
     Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
     via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 
</system.serviceModel> 

編輯2:爲了完整性這裏是我的全部web.config文件:

<?xml version="1.0"?> 
<configuration> 

    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" 
      type="System.Web.Routing.UrlRoutingModule, 
      System.Web, Version=4.0.0.0, 
      Culture=neutral, 
      PublicKeyToken=b03f5f7f11d50a3a" /> 
    </modules> 
    <handlers> 
     <add name="UrlRoutingHandler" 
     preCondition="integratedMode" 
     verb="*" path="UrlRouting.axd" 
     type="System.Web.HttpForbiddenHandler, 
     System.Web, Version=4.0.0.0, Culture=neutral, 
     PublicKeyToken=b03f5f7f11d50a3a" /> 
    </handlers> 
    </system.webServer> 

    <system.serviceModel> 
    <!--<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>--> 
    <standardEndpoints> 
     <webHttpEndpoint> 
     <!-- 
      Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
      via the attributes on the <standardEndpoint> element below 
     --> 
     <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" /> 
     </webHttpEndpoint> 
    </standardEndpoints> 
    </system.serviceModel> 

</configuration> 
+0

請張貼的web.config,至少system.servicemodel部分:) – 2010-07-13 16:19:54

+0

感謝你的努力,我應該這樣做擺在首位。我正在使用StandardEndpoint,不確定這在行爲方面意味着什麼。 – Masterfu 2010-07-14 19:35:23

+0

你如何特別部署到IIS?你是否將項目的「Web」選項卡更改爲IIS vdir?你真的有IIS偵聽端口5432? (根據您在部署到IIS後嘗試的內容)。正常情況下,在切換到Web選項卡中的本地IIS後,您將打開http:// localhost/MyProject/GreetService/greet/JohnDoe或類似 – 2010-07-15 02:50:54

回答

1

如果已經定義了你的路線是:

new ServiceRoute("GreetService", ..... 

那麼你應該能夠

http://localhost:5432/YourVirtualDirectory/GreetService/greet/JohnDoe 

,如果你的Web應用程序部署到你的IIS根(而不是虛擬目錄)打電話給你服務,這將是:

http://localhost:5432/GreetService/greet/JohnDoe 

當定義一個ServiceRoute,這樣做了擺脫不必指定Greet.svc文件,真正的 - 在ServiceRoute條目已經包含了所有的信息IIS需要實例化服務,並稱之爲 - 不需要有涉及您的網址的* .svc文件(SVC文件基本上包含您的ServiceRoute條目具有的相同信息)。

+0

我將它部署到它自己的IIS內的網站中,而不是虛擬目錄中。我也嘗試省略.svc文件並構建您最後提到的URL,但它不起作用。但是感謝確認這是一條路,我必須犯一些愚蠢的錯誤。 – Masterfu 2010-07-12 19:26:35

1

改變你global.asax.cs行改爲:

RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(GreetService))); 

,並在您web.config以下正確的根<configuration>節點下:

<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"> 
     <add name="UrlRoutingModule" 
      type="System.Web.Routing.UrlRoutingModule, 
      System.Web.Routing, Version=4.0.0.0, 
      Culture=neutral, 
      PublicKeyToken=31BF3856AD364E35" /> 

    </modules> 
    <handlers> 
     <add name="UrlRoutingHandler" 
     preCondition="integratedMode" 
     verb="*" path="UrlRouting.axd" 
     type="System.Web.HttpForbiddenHandler, 
     System.Web, Version=4.0.0.0, Culture=neutral, 
     PublicKeyToken=b03f5f7f11d50a3a" /> 
    </handlers> 
    </system.webServer> 

(一定要確保你使用的正確的.NET版本),看看它爲你做了什麼。

+0

沒有,遺憾的是 - 我只是無法得到它的工作。但是我可以訪問位於我的應用程序目錄中的靜態文件 - 但似乎所有路線都缺失。 首選的方法是將它作爲應用程序直接部署在默認網站下,例如,對吧? – Masterfu 2010-07-12 20:17:18

+0

看我的編輯是否有幫助。這是我的問題。 – 2010-07-12 20:28:14

+0

不,這也沒有幫助,但我認爲這個信息是重要的,因爲我發現它也在其他地方,同時提示將放入部分web.config文件 – Masterfu 2010-07-13 06:12:50

1

注:請張貼的web.config,至少system.servicemodel部分。

您正常使用無論是基於路由的配置或.svc文件,而不是兩個,但這是垂直於你的問題。 FWIW,你應該能夠殺死.svc文件一旦你的服務工作,只是使用的路線。

既然你能夠生成WSDL並調用它,這聽起來像你可能沒有webhttp作爲終點的行爲嗎?

請確保您有這樣定義的終結點行爲(當然可以一個diff名)

<endpointBehaviors> 
     <behavior name="webHttpBehavior"> 
      <webHttp /> 
     </behavior> 
    </endpointBehaviors> 

,然後確保你的服務端點包括behaviorConfiguration =「webHttpBehavior」

+0

我相信webHttpEndpoint已經包含webHttp行爲:http://msdn.microsoft.com/en-us/library/ee816921.aspx – Masterfu 2010-07-14 19:38:54

+0

我現在也添加了我的完整web.config文件 – Masterfu 2010-07-14 20:19:25

0

的問題是機器配置缺少以下部分

<configSections> 
    <sectionGroup name="system.serviceModel" type="System.ServiceModel.Configuration.ServiceModelSectionGroup, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> 
     <section name="standardEndpoints" type="System.ServiceModel.Configuration.StandardEndpointsSection, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
    </sectionGroup> 
</configSections> 

它添加在web.config文件的頂部(的<configuration>開幕標籤後)SH應該解決這個問題。