簡體   English   中英

如何在瀏覽器中調用WCF REST服務方法

[英]How to call WCF REST service method in a browser

我已經創建了返回JSON格式的WCF REST服務。 我的問題是“我無法在瀏覽器中找到我的運營合同方法?” 在此處輸入圖片說明

以下是我的服務合同

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>

我已通過在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輸出: 在此處輸入圖片說明

使用localhost:62030 / Service1.svc / data / 3。 您使用了錯誤的URI。 在您的情況下,調用格式為帶有uritemplate的服務名稱,URL為localhost:62030 / Service1.svc,而uritemplate為data / id,因此cpmplete URL為localhost:62030 / Service1.svc / data / 3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM