1
votes

Client side Code

  1. I have created web application and added service reference to the Client Project
  2. I tried creating a client Object like this:

     Service1Client a = new Service1Client();
    

but getting an error message :

  • Could not find default endpoint element that references contract 'ServiceReference_test.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.

Could you please let me know what mistake am I doing, I am new to WCF please help me

WCF service which returns JSON Format:

namespace WcfService1
{

    public class Service1 : IService1
    {

        public string GetData(string Id)
        {
            return ("You entered: " + Id);
        }


    }
}


namespace WcfService1
{
   [ServiceContract]
    public interface IService1
    {
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "/GetData/{Id}",
     RequestFormat = WebMessageFormat.Json,
     ResponseFormat = WebMessageFormat.Json,
     BodyStyle = WebMessageBodyStyle.Wrapped
     )]
        string GetData(string Id);


        // TODO: Add your service operations here
    }

}

Web.Config file

  <system.serviceModel>

    <services>
      <service name="WcfService1.Service1" behaviorConfiguration="ServiceBehaviour">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.IService1"/>
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
      </service>

    </services> 

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="false"  />
        </behavior>

      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <!--<protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>-->    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
1

1 Answers

0
votes

You should add following tags in your client App.config or Web.config:

<system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="WebHttpBinding" />
      </webHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:8733/Design_Time_Addresses/fine_service/FineService/soap"
          binding="webHttpBinding" bindingConfiguration="WebHttpBinding"
          contract="ServiceReference1.IService1" name="WebHttpBinding" />
    </client>
  </system.serviceModel>

if you have this tags in your cofig file then make sure that client endpoint contract name should be the same as it is in your service endpoint. In your case contract name is IService1

EDIT: also see this