2011-01-07 73 views
7

我在WebApplication項目中創建了一個簡單的WCF服務。WCF:不支持帶輸出參數的操作

[ServiceContract(Namespace = "http://my.domain.com/service")] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
public class MyService 
{ 
    [OperationContract] 
    public string PublishProfile(out string enrollmentId, string registrationCode) 
    { 
     enrollmentId = null; 
     return "Not supported"; 
    } 

建 - 一切成功編譯

那之後我試圖在瀏覽器中打開的服務,我有以下錯誤:

Operation 'PublishProfile' in contract 'MyService' specifies an 'out' or 'ref' parameter. Operations with 'out' or 'ref' parameters are not supported

我不能用' out'參數?

這裏有什麼問題?

謝謝

P.S.我使用VS2008 SP1,.NET 3.5

回答

4

在我的情況下,問題是我的ASP.NET應用程序與Visual Studio嚮導中創建的默認服務配置是一種服務類型。端點綁定是「webHttpBinding」。據我現在瞭解,它對REST服務具有約束力,而且他們沒有物理能力來處理輸出參數。對於它們而言,參數不受支持。而我真正需要的是一個「basicHttpBinding的」允許與out參數工作。

非常感謝大家誰幫我弄清楚這一點。

0

我認爲Out參數應該來之後。

它應該是這樣的:

public string PublishProfile(string registrationCode, out string enrollmentId) 

而且,你的字符串設定爲null - 爲什麼不使用string.Empty

+0

那只是一個很短...但也許我需要知道更多爲什麼string.Empty更好 – Budda 2011-01-07 22:14:43

+0

更改'outed'參數的位置並沒有幫助。 – Budda 2011-01-07 22:25:28

+0

http://codeasp.net/forums/asp-net-topics/getting-started-general-asp-net/117/ what-is-difference-between-null-vs-string-empty-vsin-c對於`string.empty`有一個很好的答案。關於你原來的問題,我真的難住你爲什麼會出錯... – VoodooChild 2011-01-08 05:36:51

2

我找到的答案是:

「out參數的想法是,該方法將實例,你在通過空引用Web服務是無狀態的,因此對於作爲參數進入web服務的對象所擁有的句柄與進入web服務服務器端的句柄並不相同,這樣可以防止出現參數。「

Source

1

試試這個:

... 
[OperationContract] 
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped)] 
public string PublishProfile(out string enrollmentId, string registrationCode) 
... 

我相信默認的車身風格(裸)只支持一個返回值。