简体   繁体   中英

Getting Error when call WebGet in WCF REST Service

I am new in WCF Service. Where i am doing wrong.

Create service in dynamic port so, written in this way Client is in Asp.net check some online references. But not get idea.

Server

Create Service:

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);

Service 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 Client

[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;
    }

When i request to WebInvoke it's working, but why it' not working with WebGet

I don't know it is right way for access service.

chFactMathClient.Test(); //Accessing data

The contract mismatch between the server and the client is causing your problem. So add WebGet attribute on the Asp.Net Client.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM