【问题标题】:How to change my WCF service to RESTful service in VS2017?如何在 VS2017 中将我的 WCF 服务更改为 RESTful 服务?
【发布时间】:2017-12-12 15:42:04
【问题描述】:

我正在使用 Visual Studio 2017,我写了一个小的WCF Service Application (Visual C#)

现在我正在尝试使其成为 RESTful,以便我可以在我的网络浏览器中访问成员函数。

这是我的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后,我在Edge中访问了http://localhost:52110/Service1.svc/getdata,但得到的是http 400

那么,我怎样才能通过 http 请求访问我的服务?

提前致谢。

【问题讨论】:

  • 需要在web.config中添加对应的endpoint或者通过程序添加。向我们展示您的 web.config

标签: c# rest wcf wcf-rest


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2016-01-11
    • 2013-12-20
    • 2012-09-22
    相关资源
    最近更新 更多