【问题标题】:How to call WCF REST service method in a browser如何在浏览器中调用 WCF REST 服务方法
【发布时间】:2017-06-10 16:13:19
【问题描述】:

我创建了返回 JSON 格式的 WCF REST 服务。 我的问题是“我无法在浏览器中找到我的 Operation Contract 方法?”

以下是我的服务合同

using System.ServiceModel;
using System.ServiceModel.Web;
namespace WcfService1
{

    [ServiceContract]
    public interface IService1
    {

            [OperationContract]
            WcfService1.Service1.Person GetData(string id);

    }


}

Service1.svc.cs

using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{

    public class Service1 : IService1
    {
      [WebInvoke(Method = "GET",
                    ResponseFormat = WebMessageFormat.Json,
                    UriTemplate = "data/{id}")]
        public Person GetData(string id)
        {
            // lookup person with the requested id 
            return new Person()
            {
                Id = Convert.ToInt32(id),
                Name = "Leo Messi"
            };
        }


        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}

下面是一个端点

<system.serviceModel>
    <services>
      <service name="WcfService1.Service1">
        <endpoint address="http://localhost:62030/Service1.svc" binding="webHttpBinding" contract="WcfService1.IService1"/>
      </service>
    </services>
    <client>
      <endpoint name="JsonService1" address="http://localhost:62030/Service1.svc" binding="webHttpBinding" contract="WcfService1.IService1"></endpoint>
    </client>
    <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="webHttpBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="false" />
  </system.serviceModel>

【问题讨论】:

    标签: json rest wcf wcf-endpoint webhttpbinding


    【解决方案1】:

    我已经通过在 service1.cs 中添加下面的 Fsactory 解决了

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    

    Service1.cs

    <%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
    

    Json 输出:

    【讨论】:

      【解决方案2】:

      使用 localhost:62030/Service1.svc/data/3。您使用了错误的 URI。调用的格式是带有uritemplate的服务名称,在你的例子中,url是localhost:62030/Service1.svc,uritemplate是data/id,所以cpmplete url是localhost:62030/Service1.svc/data/3

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-24
        • 1970-01-01
        • 1970-01-01
        • 2014-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-26
        相关资源
        最近更新 更多