2011-12-01 18 views
2

WCFサービスINTERFACEWCF呼び出しが失敗し<MyType>

[ServiceContract] 
public interface ITest 
{ 
    [OperationContract] 
    int TestCall(GenericType<MyType> x); 

    [OperationContract] 
    int TestAnotherCall(GenericType<MyOtherType> x); 

} 

[DataContract(Name = "GenericType")] 
[KnownType(typeof(List<MyType>))] 
[KnownType(typeof(List<MyOtherType>))] 
public class GenericType<T> 
{ 
    [DataMember] 
    public List<T> Data 
    { 
     get { return data; } 
     set { data = value; } 
    } 
} 

WCFサービスの実装

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
public class Test : ITest 
{ 
    public int TestCall(GenericType<MyType> x) 
    { 
     return x.Data.Count; 
    } 

    public int TestAnotherCall(GenericType<MyOtherType> x) 
    { 
     return x.Data.Count; 
    } 
} 

CLIENT

List<MyType> list = from a in ctx.Table 
        select new MyType (a.Field1, a.Field2, a.Field3).ToList(); 

GenericType gt = new GenericType(); 
gt.Data = list; 

using(WCFClient client = new WCFClient()) 
{ 
    client.TestCall(gt); 
    client.Close(); 
} 

エラー:
リモートサーバーが予期しない応答を返しました:(400)Bad Request。

"gt.Data"にNULLを渡すと正常に動作します。

注:

When I put the mouse over the gt.Data ...the hint shows as MyType[]
Not sure if that's expected.

After some review, I noticed that the Client Service only knows about
the 1st [KnownType] stated, in my case the List. No knowledge of List ....
Is that expected when you put various [KnownType] on the WCF Interface?

答えて

2
あなたは

[DataContract(Name = "GenericType")] 
[KnownType(typeof(MyType))] 
public class GenericType<T> 
{ 
    [DataMember] 
    public List<T> Data 
    { 
     get { return data; } 
     set { data = value; } 
    } 
} 

KnownType()属性を使用してジェネリックを飾るために必要

をクイック実施例:

サービス

[OperationContract] 
GenericType<MyType> GetDataUsingDataContract(GenericType<MyType> composite); 

public class Service1 : IService1 
{ 
    public GenericType<MyType> GetDataUsingDataContract(GenericType<MyType> composite) 
    { 
     composite.Data.First().Stuff = "Test"; 
     return composite; 
    } 
} 

モデル

[DataContract(Name = "GenericType")] 
[KnownType(typeof (MyType))] 
public class GenericType<T> 
{ 
    [DataMember] 
    public List<T> Data { get; set; } 
} 

public class MyType 
{ 
    public string Stuff { get; set; } 
} 

クライアント

var client = new Service1Client(); 

var genericType = new GenericType 
         { 
          Data = new[] 
             { 
              new MyType(), 
             } 
         }; 
var result = client.GetDataUsingDataContract(genericType); 
client.Close(); 

Console.WriteLine(result.Data.First().Stuff); 

Console.ReadLine(); 

この例では、サービス参照を追加し、共有アセンブリ

+0

これは、クライアントからGenericTypeを使って10種類の異なる型を使用すると、10種類のサーバークラスを宣言する必要があることを意味します(KnownType(typeof (XXX))? –

+0

「GetKnownType」はどこから来たのですか? –

+0

(msdn)(http://msdn.microsoft.com/en-us/library/ms730167.aspx)から誤ってコピーされました... – erikH

1

あなたはそれを含めることができるクラスのそれぞれについてKnownType属性でGenericType属性する必要があります。例えば

[KnownType(typeof(List<MyType>)] 
public class GenericType<T> 
+0

を使用しないで生成された方が良い説明することができます... 。お願いします? –

+0

@Developer:申し訳ありませんが、私のVB構文をc#に変換しようとしていて、MSDNのドキュメントリンクを見つけました。回答を更新しました –

+0

どこに置くのですか? –

0

問題は、あなたがそれをインスタンス化するときにGenericTypeTの型を提供していないことが考えられます。これを試してみてください:

GenericType<MyType> gt = new GenericType<MyType>(); 

代わりの

GenericType gt = new GenericType(); 

それはあなたが他の一般的なクラスをインスタンス化するときと同じ構文です。例えば、文字列のリストを宣言するために、(List<T>のように一覧... hereを参照してください)あなたは言うだろう:

List<String> myStrings = new List<String>(); 
+0

コンパイルエラーが発生しました:非ジェネリック型 'MyNamespace.GenericType'は型引数で使用することはできません –

+0

@Developer - これであなたは私のことを混乱させてしまいましたが、あなたのサンプルコードが真実ではないと言っていますか? – ChaosPandion

+0

??私は、 –

関連する問題