C# WCF - (504) 服务器没有为此请求返回响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2157110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WCF - (504) The server did not return a response for this request
提问by Keith Barrows
I have a JSONP WCF Endpoint and am trying to track down why I am getting a 504 error.
我有一个 JSONP WCF 端点,并试图找出我收到 504 错误的原因。
HTTP/1.1 504 Fiddler - Receive Failure
Content-Type: text/html
Connection: close
Timestamp: 11:45:45:9580
ReadResponse() failed: The server did not return a response for this request.
HTTP/1.1 504 Fiddler - 接收失败
内容类型:text/html
连接:关闭
时间戳:11:45:45:9580
ReadResponse() 失败:服务器没有为此请求返回响应。
I can set a breakpoint anywhere inside of my Endpoint, step through code, see it successfully gather the data required for the response, hit the final line of code, then as soon as I step out of the WCF call I get a 504 error. This was working last week!
我可以在 Endpoint 内的任何位置设置断点,单步执行代码,看到它成功收集响应所需的数据,点击最后一行代码,然后一旦我退出 WCF 调用,我就会收到 504 错误。 这是上周的工作!
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceContract(Name = "NegotiateService", Namespace = "http://rivworks.com/Services/2009/01/15")]
public class NegotiateService //: svcContracts.INegotiateService
{
public NegotiateService() { }
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public dataObjects.NegotiateSetup GetSetup(string method, string jsonInput)
{
dataObjects.NegotiateSetup resultSet = new dataObjects.NegotiateSetup();
using (RivFeedsEntities1 _dbFeed = new FeedStoreReadOnly(AppSettings.FeedAutosEntities_connString, "", "").ReadOnlyEntities())
{
using (RivEntities _dbRiv = new RivWorksStore(AppSettings.RivWorkEntities_connString, "", "").NegotiationEntities())
{
// Deserialize the input and get all the data we need...
Newtonsoft.Json.Linq.JObject o = Newtonsoft.Json.Linq.JObject.Parse(jsonInput);
string urlRef = String.Format("{0}", o["ref"]).Replace("\"", "");
string clientDate = String.Format("{0}", o["dt"]).Replace("\"", "");
string ProductID = String.Format("({0})", o["productId"]).Replace("\"", "");
string SKU = String.Format("{0}", o["sku"]).Replace("\"", "");
string env = String.Format("{0}", o["env"]).Replace("\"", "");
IList<Product> efProductList = null;
Product workingProduct = null;
vwCompanyDetails workingCompany = null;
bool foundItem = false;
if (!String.IsNullOrEmpty(SKU))
efProductList = _dbRiv.Product.Include("Company").Where(a => a.SKU == SKU).ToList();
else if (!String.IsNullOrEmpty(ProductID))
efProductList = _dbRiv.Product.Include("Company").Where(a => a.ProductId == new Guid(ProductID)).ToList();
foreach (Product product in efProductList)
{
if (String.IsNullOrEmpty(product.URLDomain))
{
var efCompany = _dbRiv.vwCompanyDetails
.Where(a => a.defaultURLDomain != null && a.CompanyId == product.Company.CompanyId)
.FirstOrDefault();
if (efCompany != null && urlRef.Contains(efCompany.defaultURLDomain))
{
foundItem = true;
workingProduct = product;
workingCompany = efCompany;
}
}
else
{
if (urlRef.Contains(product.URLDomain))
{
foundItem = true;
workingProduct = product;
workingCompany = _dbRiv.vwCompanyDetails
.Where(a => a.CompanyId == product.Company.CompanyId)
.FirstOrDefault();
}
}
}
if (foundItem)
{
try
{
// Update the resultSet...
if (workingProduct != null && workingCompany != null)
{
string rootUrl = String.Empty;
try
{
rootUrl = AppSettings.RootUrl;
}
catch
{
rootUrl = env + @"/";
}
resultSet.button = workingProduct.ButtonConfig;
resultSet.swfSource = String.Format(@"{0}flash/negotiationPlayer.swf", rootUrl);
resultSet.gateway = rootUrl;
resultSet.productID = workingProduct.ProductId.ToString();
resultSet.buttonPositionCSS = workingProduct.buttonPositionCSS;
}
}
catch (Exception ex)
{
log.WriteLine(" ERROR: ", ex.Message);
log.WriteLine("STACK TRACE: ", ex.StackTrace);
}
}
}
}
return resultSet;
}
}
My web.config:
我的 web.config:
<!-- WCF configuration -->
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="JsonpServiceBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="RivWorks.Web.Service.NegotiateService">
<endpoint address=""
binding="customBinding"
bindingConfiguration="jsonpBinding"
behaviorConfiguration="JsonpServiceBehavior"
contract="RivWorks.Web.Service.NegotiateService" />
</service>
</services>
<extensions>
<bindingElementExtensions>
<add name="jsonpMessageEncoding" type="RivWorks.Web.Service.JSONPBindingExtension, RivWorks.Web.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bindingElementExtensions>
</extensions>
<bindings>
<customBinding>
<binding name="jsonpBinding" >
<jsonpMessageEncoding />
<httpTransport manualAddressing="true"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>
As I said, the code runs all the way through so I am trying to figure out why it is not sending a response.
正如我所说,代码一直运行,所以我试图弄清楚为什么它没有发送响应。
采纳答案by Keith Barrows
For this particular problem it ended up being my connection string. Being in a web service, it was not pulling from the web site's config file. With a little bit of magic (hard coding) I got the Context to finally activate and the system started working. Not fully through this 504 yet as I have other underlying errors now popping up - will continue this answer as I figure it out.
对于这个特定问题,它最终成为我的连接字符串。在 Web 服务中,它不是从网站的配置文件中提取的。通过一点点魔法(硬编码),我终于激活了上下文,系统开始工作。还没有完全通过这个 504,因为我现在有其他潜在的错误出现 - 将在我弄清楚后继续这个答案。
2/1/2010 - Once I cleared up the connection string errors I found a couple basic EF errors that were very quickly cleaned up. It is now up and running again.
2/1/2010 - 清除连接字符串错误后,我发现了几个基本的 EF 错误,它们很快就被清除了。它现在再次启动并运行。
回答by Tyler
I'm sorry I don't have a direct solution for you, but when chasing down WCF-related issues, I've found that turning on the WCF trace logs, running through the scenario, then going over the logs in SvcTraceViewer.exe helps... you'll get some visibility into the stack which is likely where things are breaking down on you.
很抱歉,我没有直接的解决方案,但是在追查 WCF 相关问题时,我发现打开 WCF 跟踪日志,运行场景,然后查看 SvcTraceViewer.exe 中的日志帮助...您将获得对堆栈的一些可见性,这可能是您遇到问题的地方。
You can use the "WCF Service Configuration Editor" to turn on/off the various log settings and levels.
您可以使用“ WCF 服务配置编辑器”来打开/关闭各种日志设置和级别。
回答by Florin Dumitrescu
I just had a similar issue and tracing was the only way to identify it (as already suggested by @Tyler). I also had an HTTP 504 return from the server and also debugging the service in Visual Studio didn't show any exception. In fact, from the debugger it looked like the service properly returned the response.
我刚刚遇到了类似的问题,追踪是识别它的唯一方法(正如@Tyler 已经建议的那样)。我还从服务器返回了 HTTP 504,并且在 Visual Studio 中调试服务也没有显示任何异常。事实上,从调试器看来,服务正确地返回了响应。
In my particular case the cause of the error was that one of the members of my data contract class was an enum type and the values haven't been marked with the EnumMemberAttribute.
在我的特殊情况下,错误的原因是我的数据协定类的成员之一是枚举类型,并且值没有用 EnumMemberAttribute 标记。
You can find more info about configuring tracing in WCF hereand about enums in WCF services data contracts here.
回答by muruge
I had the same issue couple of times:
我有几次同样的问题:
In one scenario one of the public property (DataMember) only had getter and no setter. Changing that DataMember to have both getter and setter solved the problem.
In the other scenario I was serializing/deserializing EF4 POCO's (with Navigation properties populated) to/from JSON and this caused an recursive loop during deserialization. Changing the POCO's attribute to
[DataContract(IsReference = true)]
helped solve the recursive loop problem, but since DataContractJsonSerializer does not support references I had to switch the format to XML. (P.S.- With WEB API the default JSON serializer will be JSON.NET which will handle reference without problems).
在一种情况下,公共属性之一 (DataMember) 只有 getter 而没有 setter。更改该 DataMember 以同时拥有 getter 和 setter 解决了问题。
在另一个场景中,我将 EF4 POCO(填充了导航属性)序列化/反序列化到 JSON 或从 JSON 序列化/反序列化,这在反序列化过程中导致递归循环。更改 POCO 的属性以
[DataContract(IsReference = true)]
帮助解决递归循环问题,但由于 DataContractJsonSerializer 不支持引用,我不得不将格式切换为 XML。(PS- 使用 WEB API,默认的 JSON 序列化程序将是 JSON.NET,它将毫无问题地处理引用)。
Hint:As others have suggested, WCF Trace Loggingis your friend to solve 504 errors.
提示:正如其他人所建议的,WCF Trace Logging是解决 504 错误的好帮手。
回答by odyth
Hopefully this will help someone. I had a WCF rest service returning JSON and fiddler was giving me a 504, ReadResponse() failed: The server did not return a response for this request.
希望这会帮助某人。我有一个 WCF 休息服务返回 JSON,而 fiddler 给了我一个 504,ReadResponse() 失败:服务器没有为此请求返回响应。
My issue was that I was returning a model like this:
我的问题是我要返回这样的模型:
public class ServerResult
{
public StatusCode Status { get; set; }
public object Data { get; set; }
public static ServerResult CreateServerResult(StatusCode status)
{
return new ServerResult() { Status = status };
}
public static ServerResult CreateServerResult(StatusCode status, object data)
{
return new ServerResult() { Data = data, Status = status };
}
}
and wcf doesn't seem understand how to encode an object. The object I was returning was totally fine just strings and ints. I had to change the response to this for it to work:
wcf 似乎不明白如何编码一个对象。我返回的对象完全没问题,只是字符串和整数。我必须更改对此的响应才能使其正常工作:
public class ServerResult<T>
{
public StatusCode Status { get; set; }
public T Data { get; set; }
public static ServerResult<T> CreateServerResult(StatusCode status)
{
return new ServerResult<T>() { Status = status };
}
public static ServerResult<T> CreateServerResult(StatusCode status, T data)
{
return new ServerResult<T>() { Data = data, Status = status };
}
}
回答by JohnnyFun
If it's any help to anyone, I ran into this trying to return a list of Entity Framework 4 `EntityObject' from Web Api. To fix it, I just made it do an explicit selection, since EntityObject doesn't like to be serialized.
如果它对任何人有帮助,我遇到了这个试图从 Web Api 返回 Entity Framework 4 `EntityObject' 的列表。为了修复它,我只是让它做一个显式选择,因为 EntityObject 不喜欢被序列化。
return Request.CreateResponse(HttpStatusCode.OK, people.Select(p => new {
p.Id,
p.Name,
p.CreateDate
}));
回答by user5962352
Had the same problem and senario as odyth above. In my case it was DateTime
attribute how was NULL
in the respons class, how caused the 504 response by Fiddler. No problems with NULL
string attributes.
与上面的 odyth 有相同的问题和 senario。在我的情况下,它是响应类中的DateTime
属性如何NULL
,如何引起 Fiddler 的 504 响应。NULL
字符串属性没有问题。
public class Brevutskick
{
public string DocumentCode { get; set; }
public string DocumentName { get; set; }
public string Status { get; set; }
public DateTime DateCreated { get; set; }
public string DataTemplate { get; set; }
}