小编Joh*_*ers的帖子

WCF - (504)服务器未返回此请求的响应

我有一个JSONP WCF端点,我试图找出为什么我得到504错误.

HTTP/1.1 504 Fiddler - 接收失败
内容类型:text/html
连接:close
时间戳:11:45:45:9580
ReadResponse()失败:服务器未返回此请求的响应.

我可以在我的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 = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net wcf http-status-code-504

22
推荐指数
3
解决办法
4万
查看次数

如何改进此异常重试方案?

我有一个我正在调用的Web服务方法,它是第三方和我的域之外.由于某种原因,Web服务偶尔会出现网关超时.它的间歇性和在尝试失败后直接调用它可以成功.

现在我有一个编码困境,我有代码应该做的伎俩,但代码看起来像业余时间,你会在下面看到.

这是非常糟糕的代码,还是可以接受的?如果不能接受,我该如何改进呢?

在看的同时请努力保持笔直.

try
{
    MDO = OperationsWebService.MessageDownload(MI);
}
catch
{
    try
    {
        MDO = OperationsWebService.MessageDownload(MI);
    }
    catch
    {
        try
        {
            MDO = OperationsWebService.MessageDownload(MI);
        }
        catch
        {
            try
            {
                MDO = OperationsWebService.MessageDownload(MI);
            }
            catch 
            {
                try
                {
                    MDO = OperationsWebService.MessageDownload(MI);
                }
                catch (Exception ex)
                {
                    // 5 retries, ok now log and deal with the error.
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling

22
推荐指数
3
解决办法
9404
查看次数

如何使用C#发送/接收SOAP请求和响应?

private static string WebServiceCall(string methodName)        
{
    WebRequest webRequest = WebRequest.Create("http://localhost/AccountSvc/DataInquiry.asmx");
    HttpWebRequest httpRequest = (HttpWebRequest)webRequest;             
    httpRequest.Method = "POST";             
    httpRequest.ContentType = "text/xml; charset=utf-8";
    httpRequest.Headers.Add("SOAPAction: http://tempuri.org/" + methodName);
    httpRequest.ProtocolVersion = HttpVersion.Version11;
    httpRequest.Credentials = CredentialCache.DefaultCredentials;
    Stream requestStream = httpRequest.GetRequestStream();              
    //Create Stream and Complete Request             
    StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);

    StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
    soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
    soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
    soapRequest.Append("<GetMyName xmlns=\"http://tempuri.org/\"><name>Sam</name></GetMyName>");
    soapRequest.Append("</soap:Body></soap:Envelope>");

    streamWriter.Write(soapRequest.ToString());             
    streamWriter.Close();              
    //Get the Response    
    HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
    StreamReader srd = new StreamReader(wr.GetResponseStream()); 
    string resulXmlFromWebService = srd.ReadToEnd(); 
    return resulXmlFromWebService;
} …
Run Code Online (Sandbox Code Playgroud)

c# soap web-services asmx

22
推荐指数
1
解决办法
12万
查看次数

根级别的数据无效.第1行,第1位 - 为什么在加载xml文件时出现此错误?

根级别的数据无效.第1行,第1位 - 为什么我在加载xml文件时出现此错误

我的代码:

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.LoadXml("file.xml");
Run Code Online (Sandbox Code Playgroud)

c# xmldocument

22
推荐指数
1
解决办法
5万
查看次数

右键单击以选择ListBox中的项目

我正在尝试制作一个项目列表,您可以通过右键单击并启用上下文菜单来执行多项操作.我已经完成了,没有任何问题.

但我想拥有它,以便当您右键单击某个项目而不是选择当前项目时,选择鼠标所在的项目.

我已经研究了这个和其他相关的问题,并且我尝试使用indexFromPoint(我通过我的研究发现),但每当我右键单击某个项目时,它总是只清除所选项目并且不显示上下文菜单,因为我设置它,以便它没有出现,如果没有选定的项目.

这是我目前使用的代码:

ListBox.SelectedIndex = ListBox.IndexFromPoint(Cursor.Position.X, Cursor.Position.Y);
Run Code Online (Sandbox Code Playgroud)

c# select listbox right-click winforms

22
推荐指数
2
解决办法
3万
查看次数

使用System.IO.Compression在提取文件期间强制替换现有文件?

我使用以下代码来提取文件夹中的所有文件

        using (ZipArchive archive = new ZipArchive(zipStream))
        {
            archive.ExtractToDirectory(location);
        }
Run Code Online (Sandbox Code Playgroud)

但如果存在一个文件,则会抛出异常.是否有任何方法可以告诉Compression API替换现有文件.

我发现一种方法是首先获取所有文件名,然后检查文件是否存在并删除它.但这对我来说是非常昂贵的.

c#

22
推荐指数
7
解决办法
3万
查看次数

来自命令行的autoformat代码

是否可以为解决方案中的所有或特定文件运行自动格式化代码,例如在Visual Studio中的(Ctrl + K,Ctrl + D)格式,但是从它的命令行?或者从解决方案文件的命令行中使用Resharper的清理?

c# resharper autoformatting visual-studio

22
推荐指数
3
解决办法
5139
查看次数

当技能阻抗不匹配时,配对编程是否有效?

例如,具有有限C#.NET经验的有经验的编码人员是否可以成功地与经验丰富的C#.NET编码器配对,其次要目标是使前者能够快速使用C#.NET?

language-agnostic pair-programming

21
推荐指数
3
解决办法
926
查看次数

强制ASP.net webservice返回JSON

我有一个ASP.net Web服务,我用于Web应用程序,它向我返回XML或JSON数据,具体取决于我调用的函数.到目前为止,这一直运作良好,但我遇到了一个问题.我想在我的页面上创建一个"导出"链接,用于下载JSON文件.链接的格式非常简单:

<a href="mywebserviceaddress/ExportFunc?itemId=2">Export This Item</a>
Run Code Online (Sandbox Code Playgroud)

正如您可能想象的那样,这应该导出第2项.到目前为止,这么好,是吗?

问题是因为我并没有特别要求接受的内容类型是JSON,所以ASP.net绝对拒绝发回除XML以外的任何内容,这对于这种情况并不合适.代码基本如下:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public Item ExportItem(int itemId)
    {
        Context.Response.AddHeader("content-disposition", "attachment; filename=export.json"); //Makes it a download

        return GetExportItem(itemId);
    }
Run Code Online (Sandbox Code Playgroud)

尽管我将ResponseFormat指定为JSON,但除非我通过AJAX(使用Google Web Toolkit,BTW)请求此方法,否则我总是返回XML:

    RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "mywebserviceaddress/ExportFunc");
    builder.setHeader("Content-type","application/json; charset=utf-8");
    builder.setHeader("Accepts","application/json");
    builder.sendRequest("{\"itemId\":2}", new RequestCallback(){...});
Run Code Online (Sandbox Code Playgroud)

这很好,但AJAX不会给我一个下载对话框.有没有办法迫使ASP.net给我回JSON,无论数据是如何请求的?在我看来,没有手动覆盖这种行为是一个严重的设计疏忽.


快速回答:

首先,我想说,我认为womp的答案可能是长期更好的方式(转换为WCF),但是deostroll让我得到了我将在不久的将来使用的答案.此外,应该注意的是,这似乎主要是因为我只想下载,在所有情况下都可能无法正常工作.在任何情况下,这里是我最终用来获得我想要的结果的代码:

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public void ExportItem(int itemId)
    {
        Item item = GetExportItem(itemId);            

        JavaScriptSerializer js = new JavaScriptSerializer();
        string str = js.Serialize(item);

        Context.Response.Clear();
        Context.Response.ContentType = "application/json";
        Context.Response.AddHeader("content-disposition", "attachment; filename=export.json");
        Context.Response.AddHeader("content-length", str.Length.ToString());
        Context.Response.Flush();
        Context.Response.Write(str);
    }
Run Code Online (Sandbox Code Playgroud)

请注意 …

json web-services asmx

21
推荐指数
2
解决办法
4万
查看次数

异步编程APM与EAP

异步编程模型和基于事件的异步模式之间的实际区别是什么?

使用哪种方法以及何时使用?

.net c# asynchronous

21
推荐指数
2
解决办法
5537
查看次数