2011-06-30 9 views
9

一部のクラスに[MessageContract]属性を持つWCFサービスを開発しています。MessageContractAttributeとその他のタイプの異なるパラメータを持つメッセージまたはタイプ

私は以下のエラーメッセージを取得サービスを実行しようとすると:それはパラメータを持っているので

操作「ProcessOperation」がロードできなかったのか、型System.ServiceModel.Channels.Messageの型を返しますMessageContractAttributeと異なる型の他のパラメータを持つ型です。 System.ServiceModel.Channels.Messageを使用する場合、またはMessageContractAttributeで型を指定する場合、このメソッドは他のタイプのパラメータを使用してはなりません。

関連性はありませんが、すべてのサービスには[MessageContract]が必要ですか?

答えて

11

いいえ、これはメソッドに複数のパラメータがあり、そのうちのいくつかはメッセージではないことを意味します。インターフェイスをサービスに投稿してみてください。

このblog postは説明する:

...問題はそのメッセージの契約は、他のパラメータの型と同時に使用することはできませんです。この場合、操作の戻り値は文字列です。戻り値は単なる別の出力パラメータなので、この操作では、メッセージコントラクトメッセージをプリミティブパラメータ型と混合しています。これは、メッセージコントラクトがSOAPメッセージのレイアウトを制御し、これらの追加パラメータでシステムが混在しないようにするために失敗します。

重要な注意:ところで

、あなたがメッセージをミックスしようときに取得エラーメッセージがはこのようになります契約します。

+2

http://blogs.msdn.com/b/drnick/archive/2006/11/27/mixing-message-contract-attributes.aspx –

0

解決済み!

文字列を返すことができません。クライアントに返信するGreetingオブジェクトがあります。

using System; 
using System.ServiceModel; 
using System.Net.Security; 

namespace com.blogspot.jeanjmichel.model 
{ 
    [MessageContract] 
    public class Greeting 
    { 
     private String userGreeting; 

     private void SetGreeting() 
     { 
      DateTime now = DateTime.Now; 

      if (now.Hour >= 7 && now.Hour <= 11) 
      { 
       this.userGreeting = "Good morning"; 
      } 
      else if (now.Hour >= 12 && now.Hour <= 17) 
      { 
       if (now.Hour == 12 || now.Hour == 13) 
       { 
        this.userGreeting = "Good afternoon, it's lunch time!"; 
       } 
       else 
       { 
        this.userGreeting = "Good afternoon"; 
       } 
      } 
      else if (now.Hour >= 18 && now.Hour <= 20) 
      { 
       this.userGreeting = "Good evening"; 
      } 
      else 
      { 
       this.userGreeting = "Good night"; 
      } 
     } 

     [MessageBodyMember(Order = 1, ProtectionLevel = ProtectionLevel.EncryptAndSign)] 
     public String UserGreeting 
     { 
      get { return this.userGreeting; } 
     } 

     public Greeting() 
     { 
      this.SetGreeting(); 
     } 
    } 
} 

using System; 
using System.ServiceModel; 
using com.blogspot.jeanjmichel.model; 

namespace com.blogspot.jeanjmichel.services.contract 
{ 
    [ServiceContract(Namespace = "http://jeanjmichel.blogspot.com/services/v0.0.1")] 
    public interface IGetGreeting 
    { 
     [OperationContract] 
     Greeting GetGreeting(Credential credential); 
    } 
} 

using System; 
using System.ServiceModel; 
using com.blogspot.jeanjmichel.services.contract; 
using com.blogspot.jeanjmichel.model; 

namespace com.blogspot.jeanjmichel.services 
{ 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, 
        Namespace = "http://jeanjmichel.blogspot.com/services/v0.0.1")] 
    public class GetGreetingService: IGetGreeting 
    { 
     public Greeting GetGreeting(Credential credential) 
     { 
      if (String.IsNullOrEmpty(credential.Token)) 
      { 
       throw new FaultException("Inform the security phrase, and try again."); 
      } 
      else 
      { 
       if (credential.Token.Equals("[email protected]")) 
       { 
        Greeting g = new Greeting(); 
        return g; 
       } 
       else 
       { 
        throw new FaultException("Wrong password."); 
       } 
      } 
     } 
    } 
} 
2

これは、基本的に特定の操作は、以下の組合せのいずれかでメッセージの契約タイプとプリミティブ型の組み合わせを使用していることを意味する:

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 

上記のシナリオのいずれかがエラーを生成します。

0

混合型のプリミティブ(文字列など)とMessageContractの他のタイプ、つまり戻り値と文字列パラメータとして1つのクラスに問題がある場合、これを解決する方法の1つはMessageContractからDataContractに切り替えることでした。

これを解決する別の方法は、プリミティブ型をプロパティとして保持するクラスを作成して、戻り値とパラメータの両方でMessageContractを実装できるようにすることです。

関連する問題