系统架构需要使用Web service来降低耦合性。但是,现场布置的时候,WebService的地址是不固定的。
可以使用修改exe文件的对应的exe.config中的设置来达到目的。
exe.config文件是一个XML配置文件,其中描述了Web Service的地址。节点@"configuration/system.serviceModel/client/endpoint" 中的"address"属性的值就是Web Service的地址。例如, "http://192.168.0.100/helloWorld/Service.asmx"。
创建一个类DynamicURL来进行Web Service地址修改操作。
class DynamicURL
using System.Diagnostics;
using System.Xml;
using System.IO;
using System.Windows.Forms;
namespace TestWSDL
{
public class DynamicURL
{
static public string LoadURL()
{
string exeConfigFile = Process.GetCurrentProcess().MainModule.FileName + ".config";
if (File.Exists(exeConfigFile))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(exeConfigFile);
XmlNode xn = xmlDoc.SelectSingleNode(@"configuration/system.serviceModel/client/endpoint");
if (xn != null)
{
XmlElement xe = (XmlElement)xn;
if (xe != null)
{
return xe.GetAttribute("address");
}
}
}
else
{
MessageBox.Show(string.Format("找不到文件'{0}'", exeConfigFile));
}
return string.Empty;
}
static public long SaveURL(string URL)
{
string exeConfigFile = Process.GetCurrentProcess().MainModule.FileName + ".config";
if (File.Exists(exeConfigFile))
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(exeConfigFile);
XmlNode xn = xmlDoc.SelectSingleNode(@"configuration/system.serviceModel/client/endpoint");
if (xn != null)
{
XmlElement xe = (XmlElement)xn;
if (xe != null)
{
xe.SetAttribute("address", URL);
xmlDoc.Save(exeConfigFile);
return 0;
}
}
}
else
{
MessageBox.Show(string.Format("找不到文件'{0}'", exeConfigFile));
}
return -1;
}
}
}
为了测试,建议Web Service, HelloWorld. 里面有方法HelloWorld,返回字符串"Hello World"。 拷贝HelloWorld成HelloWorld2,然后将HelloWorld的返回值设置成"This is the second Hello world Web Service." 以便调用的时候,可以区别两个Web Service。
在新建的窗体上添加2个按钮和2个TextBox控件。
往工程中添加服务引用 ServiceReference1,VS2008会自动添加 "http://192.168.0.100/helloWorld/Service.asmx"上服务描述。
Form1为测试窗体。
测试窗体
using System;
using System.Windows.Forms;
namespace TestWSDL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBoxURL.Text = DynamicURL.LoadURL();
}
private void buttonModifyURL_Click(object sender, EventArgs e)
{
if (0 == DynamicURL.SaveURL(this.textBoxURL.Text))
{
MessageBox.Show("修改Web Service的URL成功");
}
}
private void buttonCallWebService_Click(object sender, EventArgs e)
{
this.textBoxResult.Text = "";
ServiceReference1.ServiceSoapClient client = new TestWSDL.ServiceReference1.ServiceSoapClient();
this.textBoxResult.Text = client.HelloWorld();
}
}
}
测试步骤:
在IIS中为两个Web Services分别设置虚拟目录HelloWorld和HelloWorld2.
运行程序,点击调用按钮查看调用结果,再将URL地址从http://192.168.0.100/helloWorld/Service.asmx"修改成http://192.168.0.100/helloWorld2/Service.asmx"。点击"修改Web Service地址",再调用Web Service查看结果。
两次Web Service调用结果分别为 "Hello World"和"This is the second Hello world Web Service." 。
可以看到修改Web Service地址的效果了。
如何使用Nhibernate动态连接Oracle数据库 关于如何使用Nhibernate配置连接其支持的数据源,网上一些相关的文档已经详细作了说明,同时在其2.0帮助文档中也有相关的描述。一般常用的有两种方式:
一、使用配置文件(.config)连接数据库
下面以使用Nhibernate2.2连接Oracle10数据库为例,给出连接配置信息如下:
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">User ID=hzga;Password=hzga;Data Source=orcl;Persist Security Info=True;</property>
<property name="show_sql">false</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>
二、通过提供一个IDictionary实例连接数据库
下面还是以使用Nhibernate2.2连接Oracle10数据库为例,给出连接配置信息如下:
cfg = new Configuration();
IDictionary<string, string> connProps = new Dictionary<string, string>();
connProps.Add("connection.driver_class", "NHibernate.Driver.OracleClientDriver");
connProps.Add("connection.connection_string", "User ID=hzga;Password=hzga;Data Source=orcl;Persist Security Info=True;");
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'");
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect");
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
cfg.SetProperties(connProps);
cfg.AddAssembly(AssemblyName);
sessions = cfg.BuildSessionFactory();
三、如何使用Nhibernate动态直接连接Oracle数据库
上述两种方式都可以成功连接到Oracle数据库,但是前提条件是运行程序时需要安装Oracle客户端。在程序发布部署的时候安装Oracle客户端是一件非常麻烦的事件,那么有没有一种比较好的方法可以直接连接Oracle数据库,同时又不必安装Oracle客户端呢。
答案是肯定的,下面提供两种方式直接连接Oracle数据库:
1、通过OleDB和Oracle公司的驱动
cfg = new Configuration();
IDictionary<string, string> connProps = new Dictionary<string, string>();
connProps.Add("connection.driver_class", "NHibernate.Driver.OleDbDriver");
connProps.Add("connection.connection_string", "Provider=OraOLEDB.Oracle.1;User ID=hzga;Password=hzga;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.13.148)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))");
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'");
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect");
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
cfg.SetProperties(connProps);
cfg.AddAssembly(AssemblyName);
sessions = cfg.BuildSessionFactory();
2、通过OLEDB和微软公司的Oracle驱动
cfg = new Configuration();
IDictionary<string, string> connProps = new Dictionary<string, string>();
connProps.Add("connection.driver_class", "NHibernate.Driver.OleDbDriver");
connProps.Add("connection.connection_string", " Provider=MSDAORA.1;User ID=lportal;Password=lportal;Data Source=(DESCRIPTION = (ADDRESS_LIST= (ADDRESS = (PROTOCOL = TCP)(HOST = zhbrserver)(PORT = 1521))) (CONNECT_DATA = (SERVICE_NAME = orcl)))");
connProps.Add("query.substitutions", "true 1, false 0, yes 'Y', no 'N'");
connProps.Add("dialect", "NHibernate.Dialect.Oracle10gDialect");
connProps.Add("proxyfactory.factory_class", "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu");
cfg.SetProperties(connProps);
cfg.AddAssembly(AssemblyName);
sessions = cfg.BuildSessionFactory();