2011-05-24 69 views
6

我想一個MessageContract添加到我的WCF服務,類似於這個問題是怎麼回事WCF服務:WCF: using streaming with Message Contracts使用MessageContract的崩潰,在啓動時

這裏是例外,我得到: 操作「UploadFile '無法加載,因爲它具有System.ServiceModel.Channels.Message類型的參數或返回類型或具有MessageContractAttribute和其他不同類型參數的類型。當使用System.ServiceModel.Channels.Message或具有MessageContractAttribute的類型時,該方法不得使用任何其他類型的參數。

這裏是我的合同:

[ServiceContract] 
public interface IFile 
{ 
    [OperationContract] 
    bool UploadFile(FileUpload upload); 
} 
[MessageContract] 
public class FileUpload 
{ 
    [MessageHeader(MustUnderstand = true)] 
    public int Username { get; set; } 
    [MessageHeader(MustUnderstand = true)] 
    public string Filename { get; set; } 
    [MessageBodyMember(Order = 1)] 
    public Stream ByteStream { get; set; } 
} 

而這裏的,我用我的app.config綁定配置:

<netTcpBinding> 
    <binding name="TCPConfiguration" maxReceivedMessageSize="67108864" transferMode="Streamed"> 
     <security mode="None" /> 
    </binding> 
    </netTcpBinding> 

現在,我認爲這可能有一些與我正在使用的綁定類型有關,但我不完全確定。

+0

如果添加行'Debugger.Launch();'在你的Windows服務主體項目在創建服務主機實例之前,那麼你就可以調試您的實例的啓動問題,並能夠看到什麼是例外。配置文件的一些更多細節也是有用的。 – 2011-05-24 06:03:48

+0

@Sam:謝謝你的提示。這條線以後對我來說將會非常有用。無論如何,這裏是我從中得到的異常消息:**無法加載操作'UploadFile',因爲它具有System.ServiceModel.Channels.Message類型的參數或返回類型或具有MessageContractAttribute和其他不同參數的類型類型。當使用System.ServiceModel.Channels.Message或具有MessageContractAttribute的類型時,該方法不能使用任何其他類型的參數。** – rafale 2011-05-24 06:15:12

回答

7

從註釋看來,你有這樣的問題,即一旦你開始使用消息協定,你必須使用它的所有參數,這意味着你的方法不能返回布爾它必須返回另一個消息協定,如說FileUploadResult。

嘗試將其更改爲返回void並查看它是否加載,並確定它是否將其更改爲返回被歸類爲消息合約的類。

關於this MSDN page的第一條注意事項就此問題發出警告,幷包含可能提供更多信息的鏈接。

+1

將UploadFile()返回類型從'bool'更改爲'void'工作。 – rafale 2011-05-24 07:12:52

0

這基本上意味着特定操作是在任何下列組合使用消息合同類型和原語類型的組合:

MixType1: Contract type and primitive types as operation parameters 
MixType2: Contract type as a parameter and primitive type as return type 
MixType3: Primitive type as a parameter and Contract type as return type 

任何上面列出將產生錯誤的場景。

更多細節:http://www.codeproject.com/Articles/199543/WCF-Service-operations-can-t-be-loaded-due-to-mixi