由于系统要求 需要做一webservice将应用服务器的压力分配到其它的机器上,于是写了下面的WebSerivce服务
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using DBCore;
namespace QQWebService
{
/// <summary>
/// QQMessage 的摘要说明。
/// </summary>
[WebService(Namespace="QQWebService")]
public class QQMessage : System.Web.Services.WebService
{
DBCore.DataBaseVisitor.AbsDBHelper dbhelper = new DBCore.DataBaseVisitor.FactoryDBHelper().CreateDBHelper("DataBaseSql",DBCore.DataBaseVisitor.DataBaseConnectionType.Sql);
public QQMessage()
{
//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
InitializeComponent();
}
[WebMethod]
public void SetDBHelperofSQLClient(string conn)
{
this.dbhelper = new DBCore.DataBaseVisitor.JSLSqlHelper();
dbhelper.SetDBConnection = conn;
}
[WebMethod]
public DataSet GetSender(string userid)
{
string sql="select distinct Sender from tOA_Message_Temp_User where IsRead='0' and receiver='"+userid+"'";
return dbhelper.ExecuteDataset(sql);
}
...
[WebMethod]
public DataSet CSelectSys(ArrayList al)
{
if (al == null) return null;
string sql = @"
Select TopicID,Content,Sender,SendTime,IsSend,IsAutoBack,IsRead,Receiver,ReceiverTime,Sender as username
From tOA_Message_Temp_User
where Sender='$0$'
and Receiver='$1$'
and IsRead='$2$'
";
for(int i = 0; i < al.Count; i++)
{
sql = sql.Replace("$"+i.ToString()+"$",al[i] != null ? al[i].ToString() : "");
}
return dbhelper.ExecuteDataset(sql.Replace("/t"," "));
}
#region 组件设计器生成的代码
//Web 服务设计器所必需的
private IContainer components = null;
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
}
}
在系统当中添加web引用,关键位置做了如下处理
xxx.QQClient {
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
/// <remarks/>
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="QQMessageSoap", Namespace="QQWebService")]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(object[]))]
public class QQMessage : System.Web.Services.Protocols.SoapHttpClientProtocol {
/// <remarks/>
public QQMessage() {
this.Url = "http://10.60.0.147/QQWebService/QQMessage.asmx";
}
public QQMessage(string url)
{
this.Url = url;
}
public QQMessage(string url, string conn)
{
this.Url = url;
SetDBHelperofSQLClient(conn);
}
...
在系统引用webservice时做如下类
using System;
using System.Collections;
using System.Data;
using DBCore;
namespace JQHY.jslqq
{
/// <summary>
/// JSLQQSource 的摘要说明。
/// </summary>
public class JSLQQSource
{
static JQHY.QQClient.QQMessage qc = new JQHY.QQClient.QQMessage(System.Configuration.ConfigurationSettings.AppSettings["QQClientSerivce"],System.Web.HttpContext.Current.Application["DataBaseSql"].ToString());
static object[ ] ToArray( System.Collections.ICollection coll )
{
object[ ] result = new object[coll.Count];
int i = 0;
foreach( object obj in coll )
{
result[ i++ ] = obj;
}
return result;
}
public JSLQQSource()
{
}
public DataSet GetSender(string userid)
{
return qc.GetSender(userid);
}
public DataSet CSelectSys(ArrayList al)
{
return qc.CSelectSys(ToArray(al));
}
...
测试发现web service只能访问本地资源,一直找不到问题所在 。
后来将程序部署到别的机器上试发现没有此类问题,寻找很久,终于解决
得如下结论: :-)
复杂的问题往往有着简单的答案
[Web Service于Win2003下访问SQLServer2000记得一定要打上SQLSever.SP3补丁]