无法打开主机WCF REST服务 [英] Cannot open host WCF REST Services
本文介绍了无法打开主机WCF REST服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想实现一些WCF和REST服务上载文件我的服务器上,我发现了一些代码,我想实现的,但还没有成功。
I am trying to implement some WCF and REST services to upload a file on my server, and I have found some code which I am trying to implement, but no success yet.
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
Web.Config
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehavior" name="UploadService">
<endpoint address="" binding="webHttpBinding" contract="IUploadService" behaviorConfiguration="RestBehavior">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug httpHelpPageEnabled="false" includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
目前,在响应 RESP =(HttpWebResponse)req.GetResponse ();
我越来越:
远程服务器返回错误:(400)错误的请求
The remote server returned an error: (400) Bad Request.
我应该怎么做才能解决这个问题?
What should I do to fix this?
推荐答案
您正在从托管服务的同一进程的服务请求。我觉得他们应该是独立的,例如在一个控制台应用程序服务的主机,而在另一个测试。
You are making a service request from the same process that is hosting the service. I think they should be separate, e.g. your service host in a console app and your test in another.
这篇关于无法打开主机WCF REST服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文