我将其余的api定义为FruitFactory.svc,
<%@ ServiceHost Debug="true" Language="C#"
Service="LocalFarm.WebServices.Internal.FruitFactory,
LocalFarm.WebServices.Internal, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=23j26b5f532c68dj" %>
web config是,
<bindings>
<basicHttpBinding>
<binding name="Localfarm_BasicHttpBinding" maxReceivedMessageSize="104857600">
<security mode="Transport">
<transport clientCredentialType="InheritedFromHost" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="myBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
地址是,
https://portal.localFarm.com/2240AM/136/_vti_bin/internal/fruitfactory.svc
如何使用rest sharp api对其进行休息调用?
[DataContract]
public class FileObject
{
[DataMember]
public string orderName;
[DataMember]
public byte[] data;
}
[ServiceContract]
public interface IFruitFactory
{
[OperationContract]
FileObject[] GetInvoices(string customerID, string invoiceFolder, string customerName, string[] orderNames);
}
到目前为止试过这个,
var client = new RestClient("https://portal.localFarm.com/2240AM/136/_vti_bin/internal/fruitfactory.svc");
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest(Method.GET);
不确定接下来要做什么......
答案 0 :(得分:3)
您已创建了WCF服务,但这不一定(必须)与REST服务相同。使用当前配置,您的服务是SOAP服务,您应该使用特殊的XML格式发送消息。与之通信的最简单方法是生成服务代理,方法是右键单击项目中的&#34; References&#34; -entry,然后选择&#34;添加服务引用&#34;。这将创建一个服务客户端,您可以使用它来调用服务上的方法。客户端处理转换为使用的XML格式等。
但是,如果您愿意,可以使您的服务成为REST / Web服务。有两个配置更改,需要更改一些代码:
<bindings>
。目前,您有basicHttpBinding
,正如我所提到的那样是SOAP 1.1绑定。有大量绑定可用(here是来自MSDN的列表),其中一个是webHttpBinding
。WebHttp
behaviour,以便您的服务方法可以映射到网址。OperationContract
映射到网址。这是通过WebInvoke
属性完成的。由于我没有完整的web.config
或应用程序代码,我会尝试说明这些更改,但您可能需要进行一些其他更改。
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>
然后在你的代码中:
[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);
}
这应该允许您向https://portal.localFarm.com/2240AM/136/_vti_bin/internal/fruitfactory.svc/GetInvoices?customerID=123&invoiceFolder=foo...etc...
发出GET请求。但是,在这种情况下,您可能希望切换到POST以便更轻松地处理数组。
希望这有帮助,使用RestSharp调用应该使用您最初发布的代码。