2015-09-26 114 views
1

我有要求我想通過REST提供PDF的WCF服務庫。 例如我有這樣的網址:localhost:8732/service1/reports/ok 而且我得到一個PDF作爲迴應。它是本地文件系統中的一個固定文件。 這是我當前的代碼:無法在IIS中正確託管WCF服務。錯誤404

Service.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace WcfJsonRestService 
{ 

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] 
    public class Service1 : IService1 
    { 

     public Stream GetReport(string value) 
     { 
      WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf"; 
      FileStream f = new FileStream("C:\\invoice.pdf", FileMode.Open); 
      int length = (int)f.Length; 
      WebOperationContext.Current.OutgoingResponse.ContentLength = length; 
      byte[] buffer = new byte[length]; 
      int sum = 0; 
      int count; 
      while ((count = f.Read(buffer, sum, length - sum)) > 0) 
      { 
       sum += count; 
      } 
      f.Close(); 
      return new MemoryStream(buffer); 
     } 

    } 
} 

IService.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 

namespace WcfJsonRestService 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. 
    [ServiceContract] 
    public interface IService1 
    { 
     // TODO: Add your service operations here 

     [OperationContract(Action = "*")] 
     [WebInvoke(Method = "GET", //Este metodo si esta en POST, te dice que metodo no permitido 
      UriTemplate = "reports/{value}")] 
     Stream GetReport(string value); 
    } 

    // Use a data contract as illustrated in the sample below to add composite types to service operations. 
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfJsonRestService.ContractType". 
    [DataContract] 
    public class CompositeType 
    { 
     bool boolValue = true; 
     string stringValue = "Hello "; 

     [DataMember] 
     public bool BoolValue 
     { 
      get { return boolValue; } 
      set { boolValue = value; } 
     } 

     [DataMember] 
     public string StringValue 
     { 
      get { return stringValue; } 
      set { stringValue = value; } 
     } 
    } 
} 

的App.config

<?xml version="1.0" encoding="utf-8" ?> 

<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="WcfJsonRestService.Service1"> 
     <endpoint address="http://localhost:8732/service1" 
       binding="webHttpBinding" 
       contract="WcfJsonRestService.IService1"/> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <startup> 
    <supportedRuntime version="v4.1" sku=".NETFramework,Version=v4.1"/> 
    </startup> 
</configuration> 

請不要講究那個誤導性的名字「WcfJsonRestService」這個名字後來會被更改爲更恰當的名字...

當我在Visual Studio 2013中運行這一切時(沒有找到任何服務元數據的Microsoft WCF服務主機的警告除外),一切正常。當我訪問http://localhost:8732/service1/somerandomstring時,瀏覽器打開pdf。 (請注意這是目前我的文件系統上的一個固定目錄...)

問題是當我嘗試發佈或主機時。我遵循幾個關於在IIS中託管的教程,但沒有成功。 我應該怎麼做才能做到這一點?

操作系統:Windows 8.1 .NET框架4

+0

什麼錯誤/問題得到確切的,當你在IIS上發佈? –

+0

我收到請求的404錯誤 –

+0

URL地址:\t的http://本地主機:8732 /服務1 /報告/ OK 魯塔德accesofísica:\t C:\的Inetpub \ wwwroot的\ PDFprueba \服務1 \ \報告確定 –

回答

1

我試圖填充我的機器上的問題,我知道了。我也找到了解決方案。在IIS中託管之後。您訪問該網站的網址爲http://localhost/<name of the site while hosting to iis>/<namespaceofyourservice>.<servicename>/<method name>。 因此,對於我的情況它成爲http://localhost/WcfJSONPDF/WcfJsonRestService.Service1.svc/reports/ok。這裏「ok」是傳遞給web方法的字符串。 我知道我們不得不在URL中傳遞名稱空間,但現在你必須接受。我會尋找一些解決方法。以下是url定義的圖片:url definition
以下是將您的Wcf Service Library項目發佈到IIS7的步驟。請看下面圖片:Image
Click on Build-->Publish Webservice-->Name the url(website name)-->Click on publish button-->Click publish on Local IIS
就是這樣。我希望你會得到所需的輸出。
如果您仍然有一些問題,然後更新App.config與以下之一:

<?xml version="1.0"?> 
<configuration> 
    <system.serviceModel> 
    <services> 
     <service name="WcfJsonRestService.Service1" behaviorConfiguration="Metadata"> 
     <endpoint address="" 
       binding="webHttpBinding" 
       contract="WcfJsonRestService.IService1" behaviorConfiguration="Restbeh"/> 
     <endpoint name="mex" 
       address="mex" 
       binding="mexHttpBinding" 
       contract="IMetadataExchange" /> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:80/service1" /> 
      </baseAddresses> 
     </host> 
     </service> 
    </services> 
    <behaviors> 
     <endpointBehaviors> 
     <behavior name="Restbeh"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
     <serviceBehaviors> 
     <behavior name="Metadata"> 
      <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
    <startup> 
    <supportedRuntime version="v4.1" sku=".NETFramework,Version=v4.1"/> 
    </startup> 
</configuration> 
+0

哇。我會在星期一檢查它,我希望可以標記這個答案是正確的。謝謝。 –

+0

@Mr_LinDowsMac好的,歡迎:)。 –

+0

@Mr_LinDowsMac:它對你有幫助嗎?任何更新?問題是什麼? –

0

可能是一個權限問題,在IIS託管時,你需要給讀取權限到您的網站下到該文件夾​​及其內容運行的身份。您正在使用哪種身份將取決於您使用的是哪個版本的IIS以及您如何配置應用程序池。您可以通過查看應用程序池的高級設置並向下滾動至標識來查看它的身份。然後檢查這個用戶的pdf文件夾的安全設置。

0

我前幾天偶然發現了這個問題:這是由於這是需要漢勒這種類型的呼叫缺少處理程序映射。有幾種方法可以解決該問題,例如手動執行控制檯命令ServiceModelReg.exe。我在下面提出的解決方法更爲複雜,但它具有修復特定問題而不改變Web服務器的默認行爲的優點,因此可能會帶來潛在的副作用。

  • 打開服務器管理器接口機管理,通常同時存在於任務欄開始菜單
  • 轉至儀表板並選擇添加角色或功能以打開向導。
  • 選擇基於角色或基於功能的安裝類型以及要處理的服務器,即本地/本地服務器。
  • 轉到特點部:從前有,擴大.NET Framework 3.5的特點節點和/或.NET Framework 3.5的特點節點,這取決於你所安裝的:如果你有兩個,你應執行以下步驟兩次(針對其中的每一個)。
  • 展開WCF服務部分(如果可用),然後選擇HTTP激活(請參見下面的屏幕截圖)。
  • 繼續操作直至完成嚮導,然後單擊安裝

enter image description here

一旦安裝完成後,你應該能夠在沒有404錯誤以後再招致運行WCF服務。

有關此特定問題以及如何解決該問題的其他信息,還可以使用read the following post