代码之家  ›  专栏  ›  技术社区  ›  awmleer

如何在VS2017中将我的WCF服务更改为RESTful服务?

  •  0
  • awmleer  · 技术社区  · 7 年前

    WCF Service Application (Visual C#)

    现在,我正在尝试使其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请求访问我的服务呢?

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Saket Kumar    7 年前

    您可能希望按照以下方式检查配置中的绑定信息:

    <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>
    

    您也可以通过编程方式执行上述操作。