219 lines
7.3 KiB
C#
219 lines
7.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Xml.Linq;
|
|
using System.Reflection;
|
|
using System.ServiceModel;
|
|
using System.ServiceModel.Channels;
|
|
using System.Runtime.Serialization;
|
|
using System.Xml;
|
|
using System.IO;
|
|
using DeiNiu.Wms.CE.Util;
|
|
using DeiNiu.Wms.CE.utils;
|
|
|
|
namespace DeiNiu.Wms.CE
|
|
{
|
|
public static class LoginInfo
|
|
{
|
|
public static int UserId { get; set; }
|
|
|
|
public static string Password { get; set; }
|
|
|
|
public static string Token { get; set; }
|
|
|
|
public static string Account { get; set; }
|
|
|
|
|
|
public static bool IsRemember { get; set; }
|
|
}
|
|
class WcfHelp
|
|
{
|
|
//static string url = Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0, Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf('\\') + 1) + "settings.xml";
|
|
// static XDocument doc = XDocument.Load (url);
|
|
//static string host;
|
|
//static string port;
|
|
static string svrUrl;
|
|
static string iniFile = Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0,
|
|
Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf('\\') + 1) + "host.ini";
|
|
public static string iniHostPort;
|
|
|
|
static string getPath()
|
|
{
|
|
return Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0,
|
|
Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf('\\') + 1);
|
|
}
|
|
public static void updateConfig(string newHost, string newPort)
|
|
{
|
|
/* try
|
|
{
|
|
svrUrl = "";
|
|
string Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString()) + "\\settings.xml";
|
|
XmlDocument Doc = new XmlDocument();
|
|
Doc.Load(Path);
|
|
XmlNode ip = (XmlNode)Doc.SelectSingleNode("/Root/Host");
|
|
ip.InnerText = newHost +":" +newPort;
|
|
Doc.Save(Path);
|
|
// return true;
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
throw e;
|
|
// return false;
|
|
}
|
|
*/
|
|
// XDocument xmlDoc = XDocument.Load(url);
|
|
// doc.Element("Root").Element("Host").Value = host;
|
|
// doc.Element("Root").Element("Port").Value = port;
|
|
// doc.Root.Element("Port").SetValue(port);
|
|
// doc.Root.Element("Host").Value = host;
|
|
// XmlElement nodePort = (XmlElement)doc.Root.Element("Port");
|
|
// doc.Root.Element("Port").Value = port;
|
|
//doc.Save(url);
|
|
|
|
|
|
svrUrl = "";
|
|
WriteIni(newHost, newPort);
|
|
|
|
// string url = Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(0, Assembly.GetExecutingAssembly().GetName().CodeBase.LastIndexOf('\\') + 1) + "newsettings.xml";
|
|
/*
|
|
XmlDocument xmlDoc = new XmlDocument();
|
|
xmlDoc.Load(url);
|
|
var root = xmlDoc.DocumentElement;//取到根结点
|
|
XmlElement node1 = (XmlElement)xmlDoc.SelectSingleNode("Root/Host");
|
|
node1.InnerText = host ;
|
|
XmlElement node2 = (XmlElement)xmlDoc.SelectSingleNode("Root/Port");
|
|
node2.InnerText = port;
|
|
xmlDoc.Save(url);
|
|
|
|
|
|
|
|
string configFile = @"config.xml";
|
|
XmlDocument xml = new XmlDocument();
|
|
XmlElement element = xml.CreateElement("Root");
|
|
xml.AppendChild(element);
|
|
//---create the <LastAccess> element---
|
|
element = xml.CreateElement("Host");
|
|
element.InnerText = host;
|
|
xml.DocumentElement.AppendChild(element);
|
|
//---create the <LastSearchString> element---
|
|
element = xml.CreateElement("Port");
|
|
element.InnerText = port;
|
|
xml.DocumentElement.AppendChild(element);
|
|
xml.Save(configFile);
|
|
* */
|
|
|
|
}
|
|
|
|
public static void WriteIni(string newHost, string newPort)
|
|
{
|
|
|
|
iniHostPort = newHost + ":" + newPort;
|
|
FileStream fs = new FileStream(iniFile, FileMode.Create);
|
|
//获得字节数组
|
|
byte[] data = System.Text.Encoding.Default.GetBytes(iniHostPort);
|
|
//开始写入
|
|
fs.Write(data, 0, data.Length);
|
|
//清空缓冲区、关闭流
|
|
fs.Flush();
|
|
fs.Close();
|
|
}
|
|
|
|
|
|
public static string ReadIni()
|
|
{
|
|
if (!File.Exists(iniFile))
|
|
{
|
|
WriteIni("","");
|
|
}
|
|
|
|
StreamReader sr = new StreamReader(iniFile, Encoding.Default);
|
|
String line;
|
|
line = sr.ReadLine();
|
|
sr.Close();
|
|
return line;
|
|
|
|
}
|
|
|
|
|
|
public static void writeFile(string file,string content){
|
|
|
|
FileStream fs = new FileStream(file, FileMode.Append);
|
|
//获得字节数组
|
|
byte[] data = System.Text.Encoding.Default.GetBytes(content);
|
|
//开始写入
|
|
fs.Write(data, 0, data.Length);
|
|
//清空缓冲区、关闭流
|
|
fs.Flush();
|
|
fs.Close();
|
|
|
|
}
|
|
|
|
|
|
public static string readSN(){
|
|
|
|
string idFile = getPath() + "deiniu.ini";
|
|
|
|
|
|
if (!File.Exists(idFile))
|
|
{
|
|
#if debug
|
|
writeFile(idFile, Utils.Encrypt(NetUtil.GetMac()));
|
|
#endif
|
|
return "";
|
|
}
|
|
|
|
StreamReader sr = new StreamReader(idFile, Encoding.Default);
|
|
String line;
|
|
line = sr.ReadLine();
|
|
sr.Close();
|
|
return line;
|
|
|
|
}
|
|
|
|
public static string BuildUrl(string svrName)
|
|
{
|
|
if (string.IsNullOrEmpty(iniHostPort))
|
|
{
|
|
//host = doc.Root.Element("Host").Value;
|
|
//port = doc.Root.Element("Port").Value;
|
|
|
|
iniHostPort = ReadIni();
|
|
// iniHostPort = doc.Root.Element("Host").Value;
|
|
}
|
|
if (string.IsNullOrEmpty(svrUrl))
|
|
{
|
|
//svrUrl=string.Format("http://{0}:{1}/{2}.svc", host, port, svrName);
|
|
svrUrl = string.Format("http://{0}/{1}.svc", iniHostPort, svrName);
|
|
}
|
|
|
|
|
|
|
|
return svrUrl;
|
|
}
|
|
|
|
public static BasicHttpBinding getBasicBinding()
|
|
{
|
|
BasicHttpBinding binding= new BasicHttpBinding();
|
|
binding.MaxReceivedMessageSize = int.MaxValue;
|
|
binding.MaxBufferPoolSize = int.MaxValue;
|
|
binding.MaxBufferSize = int.MaxValue;
|
|
return binding;
|
|
}
|
|
|
|
public static EndpointAddress getEndpoint(string svrName){
|
|
// NetDataContractSerializer serializer = new NetDataContractSerializer();
|
|
string remoteAddress = WcfHelp.BuildUrl(svrName); //get address
|
|
EndpointAddress endpoint = new EndpointAddress(remoteAddress);
|
|
//MessageHeader hdUserId = MessageHeader.CreateHeader("UserId", "www.deinu.com", LoginInfo.UserId, serializer);
|
|
//MessageHeader hdToken = MessageHeader.CreateHeader("Token", "www.deinu.com", LoginInfo.Token, serializer);
|
|
// endpoint.Headers.
|
|
//endpoint.Headers.Add(hdUserId);
|
|
//endpoint.Headers.Add(hdToken);
|
|
|
|
return endpoint;
|
|
|
|
}
|
|
}
|
|
}
|