调用dll时出现异常

本文关键字:异常 dll 调用 | 更新日期: 2023-09-27 18:17:38

我在别人的代码有一个问题。我在一个项目中有代码,该项目调用包含有问题的代码的dll。

我调用的代码如下:

public void SaveCustomTemplate(string templateName, string templateValue)
        {
            var action = "SaveCustom";
            var webCient = new WebClient();
            var address = String.Format(urlFormat, rootUrl, action);
            var queryString = new NameValueCollection();
            queryString.Add("id",templateName);
            queryString.Add("key",apiKey);
            queryString.Add("html",templateValue);
            webCient.QueryString = queryString;
            try
            {
                webCient.UploadString(address,templateValue);
                templatesCache[templateName] = templateValue;
            }
            catch (WebException e)
            {
                var msg = String.Format("Cannot save the template '{0}'. Message:{1}.  InMessage:{2}.  Response:{3}.  Source:{4}.  Stack:{5}.  Status:{6}.  TargetSite:{7}", address, e.Message, e.InnerException.Message, e.Response, e.Source, e.StackTrace, e.Status, e.TargetSite);
                throw new WebException(msg);
            }
        }

正如你所看到的,我在他们的代码中加入了很多错误处理来找到错误的根源。从这里我得到这样的输出:

<div class="alert success" id="SuccessMessages">Cannot save the template 'http://localhost:30001/Templates/SaveCustom'. Message:The underlying connection was closed: An unexpected error occurred on a receive..  InMessage:Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host..  Response:.  Source:System.  Stack:   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadString(Uri address, String method, String data)
   at System.Net.WebClient.UploadString(String address, String data)
   at EmailServices.TemplateLoaders.HttpTemplateLoader.SaveCustomTemplate(String templateName, String templateValue) in C:'Projects'Red2Framework'Red2Framework'EmailServices'TemplateLoaders'HttpTemplateLoader.cs:line 94.  Status:ReceiveFailure.  TargetSite:Byte[] UploadDataInternal(System.Uri, System.String, Byte[], System.Net.WebRequest ByRef)</div>

如有任何帮助,不胜感激。

感谢

萨钦

调用dll时出现异常

您可以使用UploadString作为

reply = webCient.UploadString(address, templateValue);

并在服务器返回异常时跟踪异常。此外,您还必须确保服务器没有等待发布。如果是,则使用

reply = webCient.UploadString(address, "POST", templateValue);
相关文章: