Monday, May 9, 2011

WCF with JSON

Hi, I have a solution in visual studio 2010, which includes a WCF service project and a console appication.

The problem is that I want to add a json endpoint to my service, then call it from my consolle application. This is what I wrote,

 //The namespace is WcfService public interface IService1   {    ....      [OperationContract]     string sampleJson();   }   public Service1 : IService1   ...   public string sampleJson()     {       string data = "10";       return data;     } 

This is my web.config (the useful part)

 <system.serviceModel>   <behaviors>    <endpointBehaviors>     <behavior name="jsonBehavior">      <enableWebScript />     </behavior>    </endpointBehaviors>        <serviceBehaviors>     <behavior name="WcfService.JsonBehavior">      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->      <serviceMetadata httpGetEnabled="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>      <services>    <service behaviorConfiguration="WcfService.JsonBehavior" name="WcfService.Service1">     <endpoint behaviorConfiguration="jsonBehavior" address="" binding="webHttpBinding" contract="WcfService.IService1"/>    </service>   </services>      <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  </system.serviceModel> 

 

In the console application I added a service reference to the above service, and this code to main method:

static void Main(string[] args)     {       ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();        Console.WriteLine(client.sampleJson());       Console.ReadLine();

When i run the solution, I have an error which says: InvalidOperationException,  "could not find default endpoint element t hat references contract
'ServiceReference1.IService1' in the ServiceModel client
configuration section. This might be because no configuration file was found
for your application, or because no endpoint element matching this contract
could be found in the client element."

 

I'm sure I'm missing something stupid :)

Source: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/3ad5d996-f3de-4ff2-92f6-9b6080646fe4

chinese architecture application development architecture information about architecture architecture tours chicago types of architecture

No comments:

Post a Comment