Web Service应用举例 及一问题的解决方法
由于系統要求?需要做一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補丁]
轉載于:https://www.cnblogs.com/bigmouthz/archive/2006/04/19/379260.html
總結
以上是生活随笔為你收集整理的Web Service应用举例 及一问题的解决方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跃我T-Rex 2迎来重要更新:支持路线
- 下一篇: 在statspack显示完整的SQL