[Remoting FAQ]
傳遞 Remoting 參數時遇到的兩種常見錯誤
Version
|
Date
|
Creator
|
Description
|
1.0.0.1
|
2006-4-25
|
鄭昀
@Ultrapower
|
草稿
|
繼續閱讀之前,我們假設您熟悉以下知識:
n
????????
Remoting
[ 現象 1]
我們先來描述一個簡單的錯誤。當你激活遠端
Remoting Objects
時,卻得到了這樣的錯誤提示:
提示信息
|
Type 'Common.BTRequest' in Assembly
'Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
is not marked as serializable.
|
這個錯誤很容易理解。就是你的
Remoting Method
用到了一個輸入參數,這個輸入參數聲明為“
Common.BTRequest
”類,它沒有做序列化。
[
解決
1
]
很簡單。
在你的
BTRequest
類前面加一行:
[
Serializable
]
public
class
BTRequest
即可。
[
背景資料
1
]
Piet Obermeyer
的《
.NET
中的對象序列化
》講解得非常詳細:
微軟資料
|
由正在進行序列化的對象所引用的所有對象都必須標記為
Serializable
(請參閱
基本序列化
)。否則,當序列化程序試圖序列化未標記的對象時將會出現異常。
|
對象僅在創建對象的應用程序域中有效。除非對象是從
MarshalByRefObject
派生得到或標記為
Serializable
,否則,任何將對象作為參數傳遞或將其作為結果返回的嘗試都將失敗。如果對象標記為
Serializable
,則該對象將被自動序列化,并從一個應用程序域傳輸至另一個應用程序域,然后進行反序列化,從而在第二個應用程序域中產生出該對象的一個精確副本。此過程通常稱為按值封送。
|
[ 現象 2]
下面這個錯誤比較詭異,而且不是通常大家所解決掉的那種錯誤。當你激活遠端
Remoting Objects
時,卻得到了這樣的錯誤提示:
提示信息
|
Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot be accessed.
|
最開始被誤導到我的另一篇文章《
[Remoting]dotNet Framework
升級后
Remoting
信道使用的安全問題
》,這篇文章中談到的“
typeFilterLevel="Full"
”解決問題方式在
http://www.ingorammer.com/remotingFAQ/changes2003.html
http://www.codeproject.com/csharp/PathRemotingArticle.asp
http://msdn2.microsoft.com/en-us/library/61w7kz4b.aspx
都論述過了。
但是,這次情況卻不一樣。
我并不是
framework1.0
編譯的東西在
framework1.1
環境下使用,而且把
Remoting
配置文件中修改為:
<
channels
>
???????
<
channel
ref
=
"
http
"
/>
???????
<
serverProviders
>
?????????
<
provider
ref
=
"
wsdl
"
/>
?????????
<
formatter
ref
=
"
binary
"
typeFilterLevel
=
"
Full
"
/>
???????
</
serverProviders
>
</
channels
>
最終也無濟于事。
[
解決
2
]
注意到我
的
Remoting Method
用到了
BTRequest
類
的輸入參數,它卻繼承了
MarshalByRefObject
接口。這就是問題之所在。
也就是說,如果我的一個輸入參數類這么聲明:
public
class
BTRequest
:
MarshalByRefObject
前面加不加“
[
Serializable
]
”的標記都無所謂,都可以讓客戶端得到異常:
提示信息
|
Because of security restrictions, the type System.Runtime.Remoting.ObjRef cannot
?
be accessed.
Server stack trace:
??
at System.Runtime.Serialization.FormatterServices.GetSafeUninitializedObject(
Type type)
??
at System.Runtime.Serialization.Formatters.Soap.ObjectReader.ParseObject(Pars
eRecord pr)
??
at System.Runtime.Serialization.Formatters.Soap.ObjectReader.Parse(ParseRecor
d pr)
??
at System.Runtime.Serialization.Formatters.Soap.SoapHandler.StartChildren()
??
at System.Runtime.Serialization.Formatters.Soap.SoapParser.ParseXml()
??
at System.Runtime.Serialization.Formatters.Soap.SoapParser.Run()
|
只要不讓這個輸入參數類繼承
MarshalByRefObject
接口就好了!
[
背景資料
2
]
Piet Obermeyer
的《
.NET
中的對象序列化
》講到:
微軟資料
|
如果對象是從
MarshalByRefObject
派生得到,則從一個應用程序域傳遞至另一個應用程序域的是對象引用,而不是對象本身。
也可以將從
MarshalByRefObject
派生得到的對象標記為
Serializable
。
遠程使用此對象時,負責進行序列化并已預先配置為
SurrogateSelector
的格式化程序將控制序列化過程,并用一個代理替換所有從
MarshalByRefObject
派生得到的對象。如果沒有預先配置為
SurrogateSelector
,序列化體系結構將遵從下面的標準序列化規則(請參閱
序列化過程的步驟
)。
|
那既然我愿意傳遞對象引用,那么為什么在配置文件上配置
typeFilterLevel
不起作用呢?
服務器端配置:
<
channels
>
???????
<
channel
ref
=
"
http
"
/>
???????
<
serverProviders
>
?????????
<
provider
ref
=
"
wsdl
"
/>
????????
?
<
formatter
ref
=
"
binary
"
typeFilterLevel
=
"
Full
"
/>
???????
</
serverProviders
>
</
channels
>
客戶端配置:
<
channels
>
???????
<
channel
ref
=
"
http
"
/>
?????????
<
clientProviders
>
???????????
<
formatter
ref
=
"
binary
"
/>
?????????
</
clientProviders
>
?????????
<
serverProviders
>
????????
???
<
formatter
ref
=
"
binary
"
typeFilterLevel
=
"
Full
"
/>
?????????
</
serverProviders
>
</
channels
>
?
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=676972
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
