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