RE: WCF 3.5 and Problem with KnownTypeAttribute



Hi coconet,

Regarding on the "Type not serializable" exception you encountered, it is a
typical error in WCF service when you use base class/interface with derived
classes actually send in WCF service method.

For your case, the problem here is that you only declare the Interface type
in your WCF method( the IPrincipal type which is not a DataContract).
However, WCF service will only expose metadata for types that is decorated
with "DataContract" attribute or any type that is declared as "KnowType".
I saw the you've add "KnowTypeAttribute" in the "TestPrincipal" class, this
is not the correct means of using "KnownTypeAttribute".
"KnownTypeAttribute" should be apply on the base type(base class or
interface) or on a DataContract type which has member of the base
type/interface. See the following reference:

#Data Contract Known Types
http://msdn2.microsoft.com/en-us/library/ms730167.aspx

To address the problem in your case, since you haven't use a base class,
you can add the known type declaration at service contract level. e.g.

=========
[ServiceContract]
[ServiceKnownType(typeof(TestPrincipal ))]
public interface IYourServiceInterface
.....
==============

the following article include all the three means that can be used for
apply "KnownType" info for WCF service's custom data contract:

#Handling Data Contract Object Hierarchies in WCF
http://footheory.com/blogs/bennie/archive/2007/07/28/handling-data-contract-
object-hierarchies-in-wcf.aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
NNTP-Posting-Date: Fri, 29 Feb 2008 10:34:26 -0600
From: coconet <coconet@xxxxxxxxxxxxxxxx>
Newsgroups: microsoft.public.dotnet.framework.aspnet.webservices
Subject: WCF 3.5 and Problem with KnownTypeAttribute
Date: Fri, 29 Feb 2008 11:34:26 -0500



I have an Interface and an implementing class (receiver) and calling
class (sender) in a single DLL. The DLL is referenced in a web site
that received WCF calls and also in a seperate web site that makes
calls to the WCF site .svc. Why would I get an Exception about
KnownTypeAttribute?


InnerException thrown is:

System.Runtime.Serialization.SerializationException: Type
'nstestSecurity.TestPrincipal' with data contract name
'TestPrincipal:http://schemas.datacontract.org/2004/07/nstest.Security'
is not expected. Add any types not known statically to the list of
known types - for example, by using the KnownTypeAttribute attribute
or by adding them to the list of known types passed to
DataContractSerializer..

But my stuff looks like this:

[KnownType(typeof(IPrincipal))]
[DataContract(Name="DashboardPrincipal")]
public class TestPrincipal : IPrincipal
{
....
}

An instance of TestPrincipal is one of two parameters for a class that
uses this interface:

[ServiceContract(Name="IWcfReceiver",Namespace="http://nstest/200804/IWcfRe
ceiver")]
public interface IWcfReceiver
{
[OperationContract]
List<ITestStuff> Do( IPrincipal inputPrincipal , List<ITestStuff>
testList );
}



.



Relevant Pages

  • RE: WCF Client Generation Issue
    ... \par BTW, for your further comments about the WCF document and reference resources, I agree that for some specific topcs or features, the related information is not quite sufficient and centralized that make it a bit difficult to locate. ... \par that library in the service and proxy projects). ... \par> complex type in your WCF service side, it will automatically create a proxy ... \par> The generated ListEquipmentResponse looks like ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Newb to Webservices and WCF
    ... reading books and that never would have helped me solve this. ... I went a couple weeks in getting the basics of WCF, by reading and doing code examples in the book I told you about, Chapter 13 Deploying WCF that is 5 pages long. ... WCF services hosted in IIS 5.1 and IIS 6.0 are ... On those same platforms above there, you don't need IIS to host a WCF service. ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: System.Configuration does not support config files for libraries.
    ... then I suggest you the "sharing type" feature of WCF. ... For WCF service, using "client proxy" is the most convenient means to ... As for the sharing type functionality, it is done by the WCF client ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: WCF error: The request for security token could not be satisfied .
    ... \par From your description, you're exploring some WCF sample applications and met some problem to get the security working, correct? ... \par Microsoft MSDN Online Support Lead ... \par I am trying to use a very simple WCF service and at this point I don't need ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: WCF managed and unmanaged
    ... WCF service uses contract so C++ module has to interact acording to it. ... This is CLR hosting scenario, where unmanaged process hosts CLR that then ... A> Is it possible to do .net windows hosting to the unmanaged module safly? ...
    (microsoft.public.dotnet.framework)

Loading