C# 在WCF REST服务中调用WebGet时获取错误

C# 在WCF REST服务中调用WebGet时获取错误,c#,asp.net,web-services,wcf,C#,Asp.net,Web Services,Wcf,我是WCF服务的新手。 我做错了什么 在动态端口中创建服务,这样写 客户端位于Asp.net中 查看一些在线参考资料。但我不知道 服务器 创建服务: strAdr = "http://" + strHost + ":" + nPort.ToString() + "/WCFService"; Uri adrbase = new Uri(strAdr); m_svcHost = new WebServiceHost(typeof(WCFS

我是WCF服务的新手。 我做错了什么

在动态端口中创建服务,这样写 客户端位于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);
服务等级

   [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;
    }
[服务合同]
公共接口IWCFService
{
[经营合同]
字符串测试();
[经营合同]
bool StoreDetails(信息和数据配置信息);
}
网络频道;ActMathClient;
公共boolcreateservice()
{
strServiceHost=“localhost”;
string strEPAdr=“http://”+strServiceHost+:“+intServicePort.ToString()+”/WCFService”;
chFactMathClient=new-WebChannelFactory(new-WebHttpBinding(),new-Uri(strEPAdr));
System.ServiceModel.Description.WebHttpBehavior=新建
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();
返回true;
}
当我请求WebInvoke时,它正在工作,但为什么它不能与WebGet一起工作呢

我不知道这是访问服务的正确方式


chFactMathClient.Test()//访问数据

服务器和客户端之间的契约不匹配导致了您的问题。因此,在Asp.Net客户端上添加WebGet属性

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

你是对的。它起作用了。非常感谢你