我有一个.NET 3.5 WCF服务。它看起来像这样:
namespace FileUploaderWcfRestService
{
[ServiceContract]
interface IUploaderService
{
[OperationContract(IsOneWay = true)]
[WebInvoke(Method = "POST", UriTemplate = "/UploadFile?fileName={fileName}")]
void UploadFile(string fileName, Stream fileContents);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UploaderService : IUploaderService
{
public void UploadFile(string fileName, Stream fileContents)
{
// save to disk code
}
}
}
在web.config中,我的端点设置如下:
<endpoint address=""
binding="webHttpBinding"
contract="FileUploaderWcfRestService.IUploaderService">
在客户端上,我尝试使用WebClient对象调用它,但总是得到错误415(不支持的媒体类型)。
var wc = new WebClient();
string url = "http://localhost:23619/UploaderService.svc/UploadFile?fileName=todo.sdf";
byte[] data = GetBytesFromFile("ToDo.sdf"); // gets the file into a byte array
byte[] resp = wc.UploadData(url, "POST", data);
我尝试了不同的网址变体,但没有任何帮助。 我在这里缺少什么?
答案 0 :(得分:4)
您是否尝试将WebClient上的ContentType标头设置为文件的内容类型?
wc.Headers.Add("Content-Type","application/octet-stream");