我使用http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd创建了一个 Web 服务。我将它托管在我的 IIS 上并创建了一个测试应用程序,该应用程序部署在 IIS 上并且可以正常工作。现在,我托管了这个 WCF 服务并尝试从 localhost 使用/使用它,但它在以下行中给出了错误:
var response = request.GetResponse() as HttpWebResponse;
错误是
The remote server returned an error: (504) Gateway Timeout.
在提琴手它显示:
Fiddler] ReadResponse() 失败:服务器没有为此请求返回响应。
request header in fiddler is:
POST /Notifications/RecordingCompleted HTTP/1.1
Entity:
Content-Length: 792
Content-Type: application/json
Transport
Connection: Keep-Alive
Expect: 100-continue
代码是:
string serviceBaseUrl = serviceurlhere string conversationId = 2342423 string resourceUrl = ""; 字符串方法=“POST”;string jsonText = "这里是 JSON";
string success = UseHttpWebApproach(serviceBaseUrl, resourceUrl, method, jsonText);
private string UseHttpWebApproach(string serviceUrl, string resourceUrl, string method, string requestBody)
{
string responseMessage = null;
var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
if (request != null)
{
request.ContentType = "application/json";
request.Method = method;
}
//var objContent = HttpContentExtensions.CreateDataContract(requestBody);
if (method == "POST" && requestBody != null)
{
byte[] requestBodyBytes = ToByteArrayUsingJsonContractSer(requestBody);
request.ContentLength = requestBodyBytes.Length;
using (Stream postStream = request.GetRequestStream())
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
}
if (request != null)
{
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
var reader = new StreamReader(responseStream);
responseMessage = reader.ReadToEnd();
}
}
else
{
responseMessage = response.StatusDescription;
}
}
return responseMessage;
}
private static byte[] ToByteArrayUsingJsonContractSer(string requestBody)
{
byte[] bytes = null;
var serializer1 = new DataContractJsonSerializer(typeof(string));
var ms1 = new MemoryStream();
serializer1.WriteObject(ms1, requestBody);
ms1.Position = 0;
var reader = new StreamReader(ms1);
bytes = ms1.ToArray();
return bytes;
}
服务代码为:
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class RecordingCompleted
{
[WebInvoke(UriTemplate = "", Method = "POST")]
public string ProcessCall(string JsonData)
{
return string result
}
}