简体   繁体   English

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

[英]How to change my WCF service to RESTful service in VS2017?

I'm using Visual Studio 2017 and I have write a small WCF Service Application (Visual C#) . 我正在使用Visual Studio 2017,并且编写了一个小型WCF Service Application (Visual C#)

Now I'm trying to make it RESTful so that I can access the member function in my web browser. 现在,我正在尝试使其成为RESTful,以便可以在Web浏览器中访问成员函数。

This is my IService1.cs file: 这是我的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();
    }


}

And this is my Web.config file: 这是我的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>

After pressing F5, I visited http://localhost:52110/Service1.svc/getdata in Edge, but what I got is http 400 . 按F5后,我在Edge中访问了http://localhost:52110/Service1.svc/getdata ,但是得到的却是http 400

So, how can I make my service accessible via http request? 那么,如何通过http请求使我的服务可访问?

Thanks in advance. 提前致谢。

You might want to check binding information in your config as per below: 您可能要按照以下方式检查配置中的绑定信息:

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

You can also do the above programmatically. 您也可以通过编程方式执行上述操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM