[英]Upload File to WCF using RestSharp
我搜索过这个但没有找到答案。 我需要使用RestSharp将文件(图像或PDF)上传到WCF REST服务。 我使用了AddFile()方法,但是没有命中该服务(我在方法的第一行添加了一个断点),返回的响应是一个空字符串。
我尝试了byte []和Stream。 网络服务是:
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void NotesAttachment(Stream input);
我需要服务的示例以及如何从客户端调用它。
在VS 2015上使用C#4.5.1。
请参考我的例子
IServcie1.cs
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Task UploadStream(Stream stream);
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文件
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.