请各位大虾帮我分析一下一段代码是否存在问题?

星月神话oO 2015-06-07 06:37:50
编写了一个接口提供平板调用,在本地测试是没有问题的,可以生成txt文件,但是更新到客户服务器却没有生成txt文件,但又没有报错。好怪,自己怀疑是使用了线程导致的?对线程不了解,第一次使用!
WebServiceMethod.SyncComInfo 这个方法用来获取Json,数据量较大执行比较久,然而使用了线程。
/// <summary> 同步企业信息(包括相关的证书信息)
/// 编码字段:Wat_ComInfo:Com_Des,Com_Brand;Wat_BreedWay:Bre_ProductType,Bre_OtherDes
/// </summary>
/// <param name="AreaCode">行政区号</param>
/// <returns></returns>
[WebMethod]
public string SyncComInfo(string AreaCode)
{
SyncCompanyInfo yci = new SyncCompanyInfo();
return yci.SaveComInfoToTXT(AreaCode);
}

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
using MMFrame;
using FrameWork;
using FrameWork.Components;
using FrameWork.Facade;
using System.Xml;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using System.Collections;
using System.Text;
using System.Diagnostics;
using System.Threading;

/// <summary>
///SyncComInfo 的摘要说明
/// </summary>
public class SyncCompanyInfo
{
public SyncCompanyInfo()
{
//
//TODO: 在此处添加构造函数逻辑
//
}

public string SaveComInfoToTXT(string AreaCode)
{
string GUID = Common.GetGUID;
string Dir = System.Web.HttpContext.Current.Server.MapPath(Common.UpLoadDir);
string txtDir = Common.CreateDirectory(Dir, "SyncComInfo\\" + GUID);
System.Threading.Thread t = new System.Threading.Thread(() =>
{
WriteComInfoToTXT(AreaCode, txtDir);
});
t.IsBackground = true;
t.Start();
return "Public/SyncComInfo/" + GUID;
}

/// <summary> 企业信息写入TXT文件
///
/// </summary>
/// <param name="AreaCode"></param>
/// <returns></returns>
public void WriteComInfoToTXT(string AreaCode, string txtDir)
{
//try
//{
StreamWriter sr = null;
StreamWriter sw = null;
string strJson = "";
string txtPath = "";
if (!string.IsNullOrEmpty(AreaCode))
{
StringBuilder sbSQL = new StringBuilder();
sbSQL.AppendLine("select ValueID from Sys_FieldValue");
sbSQL.AppendLine("where V_F_Key = 'ProvinceCityZoneKey' and V_ParValueID = '922'");
sbSQL.AppendLine("and V_Code = '" + AreaCode + "'");
int recordCount = 0;
DataTable dt = new DataTable();
dt = BusinessSystem.getDataList(sbSQL.ToString(), out recordCount).Tables[0];
if (recordCount > 0)
{
//市级
string valueId = dt.Rows[0]["ValueID"].ToString();
sbSQL = new StringBuilder();
sbSQL.AppendLine("select V_Code from Sys_FieldValue");
sbSQL.AppendLine("where V_F_Key = 'ProvinceCityZoneKey' and V_ParValueID = '" + valueId + "'");
recordCount = 0;
dt = new DataTable();
dt = BusinessSystem.getDataList(sbSQL.ToString(), out recordCount).Tables[0];
for (int i = 0; i < recordCount; i++)
{
string code = dt.Rows[i]["V_Code"].ToString();
txtPath = txtDir + "\\" + code + ".txt";
strJson = WebServiceMethod.SyncComInfo(code);
StreamWriter srA = File.CreateText(txtPath);
srA.Close();
//写入TXT
StreamWriter swA = new StreamWriter(txtPath, false, System.Text.Encoding.Default);
swA.WriteLine(strJson);
swA.Close();
}
}
else
{
//区县级
txtPath = txtDir + "\\" + AreaCode + ".txt";
strJson = WebServiceMethod.SyncComInfo(AreaCode);
sr = File.CreateText(txtPath);
sr.Close();
//写入TXT
sw = new StreamWriter(txtPath, false, System.Text.Encoding.Default);
sw.WriteLine(strJson);
sw.Close();
}
//创建一个txt文件,用于平板判断Json是否生成完成
string txtIsFinish = txtDir + "\\" + "IsFinish.txt";
StreamWriter srF = File.CreateText(txtIsFinish);
srF.Close();
StreamWriter swF = new StreamWriter(txtIsFinish, false, System.Text.Encoding.Default);
swF.WriteLine("HelloWorld.");
swF.Close();
}
//}
//catch (Exception ex)
//{
// result = "False|" + ex.Message;
// FileTxtLogs.WriteLog("同步企业信息(" + AreaCode + ")出错!提示:" + ex.Message);
//}
}
}
...全文
101 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
WM_JAWIN 2015-06-08
  • 打赏
  • 举报
回复
1)有可能是你的目录配置错误了,生成的不知那个目录下去了 2)先把多线程去掉,如果有错,你就可以在IE上看到了(否则就是你说的,不报错)
zyug 2015-06-08
  • 打赏
  • 举报
回复
第一,个人认为你不必建多线程 第二,建议你查查权限,是否该目录有足够的写入权限,如果别人的服务器不让你调,,可以试试写到自己的数据库来判断是否成功执行。
Justin-Liu 2015-06-08
  • 打赏
  • 举报
回复
没生成文件应该是因为没有权限
江南小鱼 2015-06-07
  • 打赏
  • 举报
回复
恢复WriteComInfoToTXT的try catch,新启线程发生异常,不会影响到主线程的。
遥望那些年 2015-06-07
  • 打赏
  • 举报
回复
新创建的线程好像默认是IIS的权限。你看看IIS是不是有对应文件夹的修改权限

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧