WCF普通的用途还是比较简单的,之间看到登陆别的网站可以用QQ登陆我就奇怪了,都不相关的东西也可以通用?原来这货是用的WEBSERVEICE,在微软早期也有这东西,现在搞成WCF了。
我做了一个通过代理查ORACLE的相当于中间层,不多话,直接代码,调用WCF的时候会自动产生两个示例的方法,我加了一个SEDT的方法。
公开出去的方法类:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
using
System.Data;
namespace
WcfServiceForOracle
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType
GetDataUsingDataContract(CompositeType composite);
[OperationContract]
System.Data.DataSet
sedt(string oraStr);
}
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
服务类:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Runtime.Serialization;
using
System.ServiceModel;
using
System.ServiceModel.Web;
using
System.Text;
using
System.Data;
namespace
WcfServiceForOracle
{
public class Service1 : IService1
{
Access acc = new Access();
public string GetData(int value)
{
return string.Format("You
entered: {0}", value);
}
public CompositeType
GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public DataSet sedt(string oraStr)
{
System.Data.DataSet sedt =
new DataSet();
sedt=acc.GetOraSeReuslt(oraStr);
return sedt;
}
}
}
客户端调用代码:
当然必须先加入WCF的服务引用,WCF运行之后会产生一个公用的网址,引用之后即可调用。
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Oracle.DataAccess.Client;
using
System.Data;
using
System.Windows.Forms;
using
System.IO;
using
System.Data.OleDb;
namespace
StatisticalProcessControlSystem
{
class Access
{
ServiceReference1.Service1Client
WS = new
ServiceReference1.Service1Client();
public
static string oracleConStr()
{
string oraStr = "Data
Source=(Description=(Address_list=(Address=(Protocol=TCP)(Host=1.11.11.)(port=1521)))(connect_data=(SERVER=Dedicated)(Service_name=mycim)));user
id=111;password=111$11;";
return oraStr;
}
public
DataTable
GetOraSeReuslt(string
oracleStr)
{
try
{
DataSet ds = new DataSet();
DataTable oracleDT =
new DataTable();
if (WS.State ==
System.ServiceModel.CommunicationState.Closed)
{
WS.Open();
}
ds = WS.sedt(oracleStr);
if (ds!=null)
oracleDT = ds.Tables[0];
//WS.Close();
return oracleDT;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(),
"ERROR!");
return null;
}
}
}
}
以上就OK了,改一下字节传输配置即大功告成:
服务器端: