研究这个东西一个多月了,觉得它的逻辑结构设计的真实不错,下面就把自己的一些体会陆陆续续的写出来吧:
首先最重要的文件就是提供webservice的communityService.asmx文件,这在.net中是一个web服务的文件,此文件也是整个社区站点web服务的核心文件,可以通过以下连接它是如何使用soap协议的:
http://www.omencathay.net/communityService.asmx
GetCommunityContent ()
GetFullCommunityContent ()
上面的这两个方法就是整个社区web服务所需要调用的核心方法,其他站点或者本站在调用本站点内容是就是通过此方法实现的,可以从下面的代码来看看它究竟实现的是什么
下面我们来看看源代码:
//下面是所需要调用累的命名空间,这个不用多说了吧
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
using omen.web.Communities.Services;
//自定义命名空间
namespace omen.web.Communities.Services
{
//*********************************************************************
//
// CommunityService Class
//
// The class that implements the community Web service. The
// community Web service exposes content items from this community
// to other community Web sites.
//
//*********************************************************************
public class communityService : System.Web.Services.WebService
/************************************************************/
/*定义一个类继承于WebService类,我们看看WebService类如何定义的及其有哪些方法:
为 XML Web services 定义可选的基类,该基类提供对公共 ASP.NET 对象(如应用程序和会话状态)的直接访问。
有关此类型所有成员的列表,请参阅 WebService 成员
成员:
公共属性
Application 初始化 WebService 类的新实例
Container 获取组件的容器
Context 获取当前请求的 ASP.NET HttpContext,它封装了由 HTTP 服务器用来处理 Web 请求的所有 HTTP 特定的上下文。
这个是最常用的东西
Server 获取当前请求的 HttpServerUtility。
Session 获取当前请求的 HttpSessionState 实例
Site 获取或设置组件的位置
User 获取 ASP.NET 服务器 User 对象。可用于验证用户是否有权执行请求
其它的,请参考msdn,在此不祥述了
/**************************************************************************/
{
public communityService() //构造函数进行初始化
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
#region Component Designer generated code
//Required by the Web Services Designer
private IContainer components = null;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
}
#endregion
public SecurityHeader PasswordHeader; //SoapHeader的实例,当在派生类中被重写时,表示 SOAP 标头的内容
//*********************************************************************
//
// GetFullCommunityContent Method
//
// Returns a DataSet that represents community content.
// 获取社区内容的方法,用一个dataset返回请求社区的内容
//*********************************************************************
[WebMethod()]
[SoapHeader("PasswordHeader", Required=false)]
public DataSet GetFullCommunityContent(string communityName, string sectionPageType, Byte[] lastTimestamp) {
// Get section info from context ,Sectioninfo是一个自定义的类,在以后的文件中会提到
SectionInfo objSectionInfo = (SectionInfo)Context.Items["SectionInfo"];
// Check whether Web service
if (!objSectionInfo.IsSectionWebService)
throw new Exception( "没有作为webservice发布" );
// Check security
if (objSectionInfo.WebServicePassword.Trim() != String.Empty) {
if (PasswordHeader == null)
throw new Exception( "请输入密码!" );
if (PasswordHeader.Password != objSectionInfo.WebServicePassword)
throw new Exception( "密码错误!" );
}
// Make sure that section Page Types match (calling section versus local section)
if (sectionPageType != objSectionInfo.Content)
throw new Exception("Section types do not match."

;
ActivityUtility.RecordMessage
(
"Received service subscription request from " + communityName,
"Received service subscription request from " + communityName
+ ". Returning content from " + objSectionInfo.Name + " section."
);
// Setup the DataAdapter to grab the content
SqlDataAdapter dadGetContent = new SqlDataAdapter("Community_ServiceGetFullCommunityContent", CommunityGlobals.ConnectionString);
dadGetContent.SelectCommand.CommandType = CommandType.StoredProcedure;
dadGetContent.SelectCommand.Parameters.Add("@sectionID", objSectionInfo.ID);
dadGetContent.SelectCommand.Parameters.Add("@sectionPageType", sectionPageType);
dadGetContent.SelectCommand.Parameters.Add("@lastTimeStamp", SqlDbType.Timestamp).Value = lastTimestamp;
DataSet dstContent = new DataSet();
dadGetContent.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dadGetContent.AcceptChangesDuringFill = false;
dadGetContent.Fill(dstContent);
return dstContent;
}
//*********************************************************************
//
// GetCommunityContent Method
//
// Returns a ServiceResponseInfo object that represents a
// a collection of content items.
//
//*********************************************************************
[WebMethod()]
[SoapHeader("PasswordHeader", Required=false)]
[XmlInclude(typeof(ServiceResponseInfoItem))]
public ServiceResponseInfo GetCommunityContent()
{
// Create the response info
ServiceResponseInfo responseInfo = new ServiceResponseInfo();
// Get section info from context
SectionInfo objSectionInfo = (SectionInfo)Context.Items["SectionInfo"];
// Check whether Web service
if (!objSectionInfo.IsSectionWebService)
throw new Exception( "Not published as a Web service" );
// Check security
if (objSectionInfo.WebServicePassword.Trim() != String.Empty) {
if (PasswordHeader == null)
throw new Exception( "请输入密码!" );
if (PasswordHeader.Password != objSectionInfo.WebServicePassword)
throw new Exception( "密码无效!" );
}
// Get the service description
responseInfo.ServiceTitle = objSectionInfo.Title;
responseInfo.ServiceDescription = objSectionInfo.Description;
responseInfo.Items = GetResponseItems(objSectionInfo.ID);
return responseInfo;
}
//*********************************************************************
//
// GetResponseItems Method
//
// Returns an array of ServiceResponseInfoItems that represents
// content pages.
//
//*********************************************************************
public ArrayList GetResponseItems(int sectionID) {
ArrayList colItems = new ArrayList();
ServiceResponseInfoItem responseItem = null;
SqlConnection conPortal = new SqlConnection(CommunityGlobals.ConnectionString);
SqlCommand cmd = new SqlCommand("Community_ServicesGetResponseItems", conPortal);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@communityID", CommunityGlobals.CommunityID);
cmd.Parameters.Add("@sectionID", sectionID);
conPortal.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
responseItem = new ServiceResponseInfoItem();
responseItem.Title = (string)dr["ContentPage_Title"];
responseItem.Link = ContentPageUtility.CalculateFullContentPath(sectionID, (int)dr["ContentPage_ID"]);
responseItem.Description = (string)dr["ContentPage_Description"];
colItems.Add(responseItem);
}
conPortal.Close();
return colItems;
}
}
public class SecurityHeader : SoapHeader {
public string Password;
}
}