2017-07-08 36 views
0

我使用的是Visual Studio 2017,我寫了一個小的WCF Service Application (Visual C#)如何將我的WCF服務更改爲VS2017中的RESTful服務?

現在我試圖讓它成爲RESTful,以便我可以在我的Web瀏覽器中訪問成員函數。

這是我IService1.cs文件:

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

namespace WcfService { 
    [ServiceContract] 
    public interface IService1 { 

     [OperationContract] 
     [WebGet(UriTemplate="/getdata",RequestFormat =WebMessageFormat.Xml,ResponseFormat =WebMessageFormat.Xml,BodyStyle =WebMessageBodyStyle.Bare)] 
     //[WebGet] 
     string GetData(); 
    } 


} 

這是我Web.config文件:

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

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5.2" /> 
    <httpRuntime targetFramework="4.5.2"/> 
    </system.web> 
    <system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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> 
    <protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https" /> 
    </protocolMapping>  
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to avoid disclosing web app folder information. 
     --> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

</configuration> 

按F5鍵後,筆者走訪了在邊緣http://localhost:52110/Service1.svc/getdata,但我得到的是http 400

那麼,如何通過http請求使我的服務可訪問?

在此先感謝。

+0

您需要在web.config中添加相應的端點或以編程方式添加它。告訴我們你的web.config –

回答

0

您可能要檢查您的配置綁定信息按如下:

<system.serviceModel> 
    <services> 
     <service name="WcfService.Service1" behaviorConfiguration="serviceBehavior"> 
       <endpoint address="" 
          binding="webHttpBinding" 
          contract="WcfService.IService1" 
          behaviorConfiguration="web"></endpoint> 
      </service> 
    </services> 
    <behaviors> 
      <serviceBehaviors> 
       <behavior name="serviceBehavior"> 
        <serviceMetadata httpGetEnabled="true"/> 
         <serviceDebug includeExceptionDetailInFaults="false"/> 
      </behavior> 
      </serviceBehaviors> 
      <endpointBehaviors> 
       <behavior name="web"> 
        <webHttp/> 
       </behavior> 
      </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
</system.serviceModel> 

你也可以做上面的程序。