簡體   English   中英

在 WCF REST 服務中調用 WebGet 時出現錯誤

[英]Getting Error when call WebGet in WCF REST Service

我是 WCF 服務的新手。 我在哪里做錯了。

所以在動態端口創建服務,這樣寫Client在Asp.net查一些網上的參考資料。 但不明白。

服務器

創建服務:

strAdr = "http://" + strHost + ":" + nPort.ToString() + "/WCFService";
Uri adrbase = new Uri(strAdr);

m_svcHost = new WebServiceHost(typeof(WCFService), adrbase);

ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
mBehave.HttpGetEnabled = true;
m_svcHost.Description.Behaviors.Add(mBehave);
m_svcHost.AddServiceEndpoint(typeof(IMetadataExchange), 
MetadataExchangeBindings.CreateMexHttpBinding(), "mex");

WebHttpBinding httpb = new WebHttpBinding();
m_svcHost.AddServiceEndpoint(typeof(IWCFService), httpb, strAdr);
m_svcHost.Open();

Debug.WriteLine("\n\nService is Running as >> " + strAdr);

服務 Class

   [ServiceContract]
   public interface IWCFService 
   {
       [WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json, 
       BodyStyle=WebMessageBodyStyle.Bare)]
       [OperationContract]
       string Test();

       [OperationContract]
       [WebInvoke(UriTemplate = "/StoreDetails", RequestFormat = WebMessageFormat.Json, ResponseFormat = 
       WebMessageFormat.Json, Method = "POST")]
       bool StoreDetails(Info_Data configInfo);
   }

   [AspNetCompatibilityRequirements
           (RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
   public class WCFService : IWCFService
   {
      Info_Data config = new config();
      public string Test()
      {
           Console.WriteLine("MSG Come....");
           return "Hello";
      }

      public bool StoreDetails(Info_Data configInfo)
      {
          config.id = configInfo.id;
          config.name = configInfo.name;
      }
   }

Asp.Net 客戶端

[ServiceContract]

public interface IWCFService
{

    [OperationContract]
    string Test();

    [OperationContract]
    bool StoreDetails(Info_Data configInfo);
}
   
WebChannelFactory<IWCFService> chFactMathClient ;

    public bool CreateService()
    {
        strServiceHost = "localhost";

        string strEPAdr = "http://" + strServiceHost + ":" + intServicePort.ToString() + "/WCFService";
        chFactMathClient = new WebChannelFactory<IWCFService>(new WebHttpBinding(), new Uri(strEPAdr));
       
        System.ServiceModel.Description.WebHttpBehavior behavior = new 
        System.ServiceModel.Description.WebHttpBehavior();

        behavior.DefaultOutgoingRequestFormat = System.ServiceModel.Web.WebMessageFormat.Json;
        behavior.DefaultOutgoingResponseFormat = System.ServiceModel.Web.WebMessageFormat.Json;
        chFactMathClient.Endpoint.Behaviors.Add(behavior);

        mathObj = chFactMathClient.CreateChannel();
      
        return true;
    }

當我請求 WebInvoke 時它正在工作,但為什么它不能與 WebGet 一起工作

我不知道這是訪問服務的正確方式。

chFactMathClient.Test(); //訪問數據

服務器和客戶端之間的合同不匹配導致您的問題。 所以在 Asp.Net 客戶端上添加 WebGet 屬性。

 [WebGet(UriTemplate = "/Test", ResponseFormat = WebMessageFormat.Json, 
       BodyStyle=WebMessageBodyStyle.Bare)]
       [OperationContract]
       string Test();

暫無
暫無

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

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