[英]Upload File to WCF using RestSharp
I have searched for this but did not find the answer. 我搜索过这个但没有找到答案。 I need to upload a file ( image or PDF) to a WCF REST service using RestSharp. 我需要使用RestSharp将文件(图像或PDF)上传到WCF REST服务。 I have used AddFile() method, but the service is not hit ( I added a break-point on the very first line of the method), and the returned response is an empty string. 我使用了AddFile()方法,但是没有命中该服务(我在方法的第一行添加了一个断点),返回的响应是一个空字符串。
I tried both byte[] and Stream. 我尝试了byte []和Stream。 The web services is : 网络服务是:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void NotesAttachment(Stream input);
Please I need example for both the service and how to call it from the client. 我需要服务的示例以及如何从客户端调用它。
Using C# 4.5.1 on VS 2015. 在VS 2015上使用C#4.5.1。
Please refer to my example 请参考我的例子
IServcie1.cs IServcie1.cs
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Task UploadStream(Stream stream);
Service1.svc.cs Service1.svc.cs
public async Task UploadStream(Stream stream)
{
using (stream)
{
using (var file = File.Create(Path.Combine(HostingEnvironment.MapPath("~/Uploads"), Guid.NewGuid().ToString() + ".png")))
{
await stream.CopyToAsync(file);
}
}
}
Web.config Web.config文件
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="httpbinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="None">
</security>
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
<binding name="mybinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
<security mode="Transport">
<transport clientCredentialType="None"></transport>
</security>
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxDepth="2147473647" maxNameTableCharCount="2147483647" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<!--http and https are all supported.-->
<add binding="webHttpBinding" scheme="http" bindingConfiguration="httpbinding" />
<add binding="webHttpBinding" scheme="https" bindingConfiguration="mybinding"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Client call example. 客户呼叫示例。 Take the Postman as an example. 以邮递员为例。
Feel free to let me know if there is anything I can help with. 如果有什么我可以帮忙,请随时告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.