.net2.0主程序app.config中增加
<startup> <requiredRuntime version="4.0" safemode="true"/> </startup> </configuration>
.net4.0环境生成的dll中相关配置不能写入.net2.0主程序的config文件,领导给的原因是不能影响主程序环境下的发布配置。dll库如何调用自己的配置文件?使用ini配置文件
配置参数取值,配置文件优先于数据库配置
public static string baseUrl { get { try { return System.Configuration.ConfigurationManager.AppSettings["yesfpbaseUrl"].ToString(); } catch { string sql = "yesfp_intersys_qry"; DataTable dt = BusiOperator.DoTransOld(sql); if (dt == null || dt.Rows.Count == 0) { return ""; } else { return dt.Rows[0]["yesfpbaseUrl"].ToString(); } } } }
public static string baseUrl { get { string s = ContentValue("", "yesfpbaseUrl"); if (s == "") { string sql = "yesfp_intersys_qry"; DataTable dt = BusiOperator.DoTransOld(sql); if (dt == null || dt.Rows.Count == 0) { return ""; } else { return dt.Rows[0]["yesfpbaseUrl"].ToString(); } } else { return s; } } }
其中ContentValue方法
/// 读取INI文件 /// </summary> /// <param name="section">节点名称</param> /// <param name="key">键</param> /// <param name="def">值</param> /// <param name="retval">stringbulider对象</param> /// <param name="size">字节大小</param> /// <param name="filePath">文件路径</param> /// <returns></returns> [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retval,int size,string filePath); /// <summary> /// 自定义读取INI文件中的内容方法 /// </summary> /// <param name="Section">键</param> /// <param name="key">值</param> /// <returns></returns> private static string ContentValue(string Section, string key) { if (Section == "") { Section = strSec; } StringBuilder temp = new StringBuilder(1024); GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath); return temp.ToString(); }
net2.0主程序调用.net4.0环境发布的webservice
2.1 发布的程序支持post方法,在配置文件中增加以下
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
2.2 通过SoapUI这个工具来获取的POST请求数据
public static string eInvoicePost(string url, string requestdatas, string reqparam)
{
Encoding encoding = Encoding.UTF8;
string responseData = String.Empty;
string jiex = "";
WebRequest request = HttpWebRequest.Create(Local_ServUrl);
string param = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\">"
+ "<soapenv:Header/>"
+ " <soapenv:Body>"
+ " <tem:HttpPost>"
+" <!--Optional:-->"
+ " <tem:gsjp>" + gsjp + "</tem:gsjp>"
+ " <!--Optional:-->"
+ " <tem:url>" + url + "</tem:url>"
+ " <!--Optional:-->"
+ " <tem:requestdatas>" + requestdatas + "</tem:requestdatas>"
+ " <!--Optional:-->"
+ " <tem:param>" + reqparam + "</tem:param>"
+ " </tem:HttpPost>"
+ " </soapenv:Body>"
+ "</soapenv:Envelope>";
//MessageBox.Show(param);
byte[] bs = Encoding.UTF8.GetBytes(param);
request.Method = "POST";
request.ContentType = "text/xml;charset=UTF-8";
request.ContentLength = bs.Length;
request.Headers["SOAPAction"] = "http://tempuri.org/HttpPost";
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd().ToString();
XmlDocument xmldoc = new XmlDocument();//xml操作文档类
xmldoc.LoadXml(responseData);
jiex = xmldoc.DocumentElement.InnerText;
}
}
//MessageBox.Show(responseData);
responseData = jiex;
return responseData;
}
另,&符号进行转义 &
param = param.Replace("&", "&");