2011-12-01 3 views
2

WCF 서비스 인터페이스 :WCF 호출이 실패 <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) 잘못된 요청.

"gt.Data"에 NULL을 전달하면 ... 정상적으로 작동합니다.

참고 : 당신은 그것을 포함 할 수있는 각 클래스에 대한 KnownType 속성 GenericType 속성 필요

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

을 당신은 KnownType() 속성

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

으로 일반적인 장식해야합니다 예를 들어, 당신이 말하는 것 (here보기 ... List<T>에서와 같이 목록)을 문자열의 목록을 선언합니다 빠른 작업 예 :

서비스

[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 가지 유형을 사용할 경우 ... KnownType (typeof (XXX))? –

+0

그냥 "GetKnownType"이 어디서 왔는지 확인하십시오. –

+0

글쎄, 시험 ple [msdn] (http://msdn.microsoft.com/en-us/library/ms730167.aspx)에서 잘못 복사되었습니다 ... – erikH

1

. 예를 들어

:

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

당신이 더 잘 설명 할 수있는 공유 어셈블리를 사용하지 않는 생성 된 ... .부디? –

+0

@ 개발자 : 죄송합니다, 내 vb 구문을 C#으로 변환하고 MSDN 설명서 링크를 찾으려고했습니다. 업데이트 대답 –

+0

어디에 넣어야합니까? –

0

문제는 당신이 그것을 인스턴스화 할 때 GenericTypeT에 대한 유형을 제공하지 않는 것을 수 있습니다. 이 시도 :

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

대신

GenericType gt = new GenericType(); 

그것은 당신이 다른 제네릭 클래스를 인스턴스화 할 때와 동일한 구문입니다

.

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

시도했는데 컴파일 오류가 발생했습니다. 제네릭 형식이 아닌 'MyNamespace.GenericType'은 형식 인수와 함께 사용할 수 없습니다. –

+0

@Developer - 좋아, 이제 혼란 스러울거야. 예제 코드가 부적절하다고 말하는거야? – ChaosPandion

+0

?? 제안 된 변경 사항을 적용하면 오류가 발생합니다. –

관련 문제