无法打开主机WCF REST服务

我正在尝试实现一些WCF和REST服务来上传我的服务器上的文件,我找到了一些我想要实现的代码,但还没有成功。

我的代码

class Program { static void Main(string[] args) { string address = "http://localhost/UploadService/UploadService.svc/UploadFile/theFile.txt"; HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(address); req.Method = "POST"; req.ContentType = "text/plain"; Stream reqStream = req.GetRequestStream(); string fileContents = "the quick brown fox jumped over the lazy dog."; byte[] bodyContents = Encoding.UTF8.GetBytes(fileContents); reqStream.Write(bodyContents, 0, bodyContents.Length); reqStream.Close(); HttpWebResponse resp; try { resp = (HttpWebResponse)req.GetResponse(); } catch (WebException e) { resp = (HttpWebResponse)e.Response; } Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription); } } public class UploadService : IUploadService { #region IUploadService Members public void UploadFile(string fileName, Stream fileContent) { using (StreamReader fileContentReader = new StreamReader(fileContent)) { string content = fileContentReader.ReadToEnd(); File.WriteAllText(Path.Combine(@"c:\temp", fileName), content); } } #endregion } [ServiceContract] public interface IUploadService { [OperationContract] [WebInvoke(UriTemplate = "UploadFile/{fileName}")] void UploadFile(string fileName, Stream fileContent); } 

Web.Config中

                          

目前,在响应中resp = (HttpWebResponse)req.GetResponse(); 我正进入(状态:

远程服务器返回错误:(400)错误请求。

我该怎么做才能解决这个问题?

您正在从托管服务的同一进程发出服务请求。 我认为它们应该是分开的,例如你在控制台应用程序中的服务主机和在另一个应用程序中的测试