[Solved] How can I use rest sharp to call this method


You have created a WCF service, but this is not (necessarily) the same as a REST service. With your current configuration, your service is a SOAP service, which you are supposed to send messages to using a special XML format. The easiest way to communicate with it is to generate a service proxy, by right-clicking the “References”-entry in your project and then “Add service reference”. This will create a service client which you can use to call methods on your service. The client handles conversion to the XML format used and such.

However, it is possible to make your service a REST/web service if you want to. There are two configuration changes and some code change required:

  1. Reconfiguring the WCF <bindings>. Currently, you have basicHttpBinding, which as I mentioned is a SOAP 1.1 binding. There are a ton of bindings available (here is a listing from MSDN), one of which is the webHttpBinding.
  2. Adding a WebHttp behaviour on the service so that your service’s methods can be mapped to urls.
  3. You’ll need to add some information for mapping your OperationContract to a Url. This is done via the WebInvoke attribute.

Since I don’t have your full web.config or application code, I’ll attempt to illustrate these changes, but you may need to make some additional changes.

web.config:

<behaviors>
  <serviceBehaviors>
    <behavior name="myBehavior">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors> <!-- Added webHttp behavior -->
    <behavior name="webBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

<services>
  <service name="LocalFarm.WebServices.Internal.FruitFactory"> <!-- Needs to match the Service name in the .svc file -->
    <endpoint address="" contract="LocalFarm.WebServices.Internal.IFruitFactory"
              binding="webHttpBinding" bindingConfiguration="Localfarm_WebHttpBinding" 
              behaviorConfiguration="webBehavior" />
  </service>
</services>

Then in your code:

[ServiceContract]
public interface IFruitFactory
{
    [OperationContract]
    [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetInvoices")]
    FileObject[] GetInvoices(string customerID, string invoiceFolder, string customerName, string[] orderNames);
}

This should allow you to make a GET request to https://portal.localFarm.com/2240AM/136/_vti_bin/internal/fruitfactory.svc/GetInvoices?customerID=123&invoiceFolder=foo...etc.... You might want to switch to POST for easier handling of arrays in this case, though.

Hope this helps, calling with RestSharp should work with the code you originally posted.

1

solved How can I use rest sharp to call this method