在调试代码时发现的问题,疑似Bug,所以斗胆放在首页.
描述:Service端的抽象类在客户端可以编译通过.
还有我在Service端加的注释不能在Client端显示,如何为Service端的类添加注释以辅助客户端使用Service呢?
可以說,Known完全是SOA與OO沖突的產物,是個可憐的棄子!
OO要求繼承父類的東西,但SOA不知道是否把繼承的東西Show給Client.最后WCF決定子類不Show給Client.從而導致了繼承和多態在WCF的丟失,為了彌補這個問題,Known Attribute就產生了.
Known標志著子類可被客戶端識別、使用.
可以使用Known的三個地方
一.基類自身
二.特定操作
三.服務契約
詳細信息參見下面的代碼解釋.
【注】
KnownType不可應用于interface
DataContract也不可以
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
![](/Images/OutliningIndicators/None.gif)
namespace TestKnown_AX
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
[ServiceContract]
//【服務契約處聲明了A0】
[ServiceKnownType(typeof(A0))]
public interface IKnown_AX
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
[OperationContract]
//【特定操作處聲明A1】
[ServiceKnownType(typeof(A1))]
void DoWork(A a);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// This is a abstract class,Please don't new it.
/// </summary>
[DataContract(Name="A",Namespace="http://AX")]
//【基類自身處聲明A2】
//★★★我沒有在任何地方聲明A3是Known類型★★★
[KnownType(typeof(A2))]
public abstract class A
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual void OA()
{}
public abstract void OperateA();
}
![](/Images/OutliningIndicators/InBlock.gif)
//不能繼承父類的序列化功能,必須重新聲明
[DataContract]
public class A0 : A
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void OperateA0()
{ }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override void OperateA()
{ }
}
![](/Images/OutliningIndicators/InBlock.gif)
[DataContract]
public class A1 : A
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void OperateA1()
{ }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override void OperateA()
{ }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
new void OA()
{ }
}
![](/Images/OutliningIndicators/InBlock.gif)
//如果把[DataContract]契約注釋掉服務將不能啟動,報錯
//因為前面已經聲明該類為Known,必須為可序列化
[DataContract]
public class A2 : A
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void OperateA2()
{ }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override void OperateA()
{ }
}
![](/Images/OutliningIndicators/InBlock.gif)
//即使加了[DataContract],客戶端也不會知道它的存在
[DataContract]
public class A3 : A
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
void OperateA3()
{ }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public override void OperateA()
{ }
}
}
![](/Images/OutliningIndicators/None.gif)
客戶端(SR為引用服務的名稱)
namespace Test_AX
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public partial class _Default : System.Web.UI.Page
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SR.IKnown_AX proxy = new SR.Known_AXClient();
proxy.DoWork(new Test_AX.SR.A0());
proxy.DoWork(new Test_AX.SR.A1());
proxy.DoWork(new Test_AX.SR.A2());
//不認識A3類
//proxy.DoWork(new Test_AX.SR.A3());
//是的,你沒看錯【抽象類A】被new了,但編譯不報錯
//但運行時會報錯
proxy.DoWork(new Test_AX.SR.A());
//因為B在項目內,所以編譯會報錯
//B b = new B();
}
}
![](/Images/OutliningIndicators/InBlock.gif)
public abstract class B
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
}
Demo下载
博客园→斧头帮少帮主