.NET Framework - WCF 3.5 and Problem with KnownTypeAttribute

Asked By coconet
29-Feb-08 11:34 AM
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/IWcfReceiver")]
public interface IWcfReceiver
{
[OperationContract]
List<ITestStuff> Do( IPrincipal inputPrincipal , List<ITestStuff>
testList );
}
BizTalk
(1)
SerializationException
(1)
WCF
(1)
IYourServiceInterface
(1)
Utilities
(1)
KnownTypeAttribute
(1)
KnowTypeAttribute
(1)
ServiceKnownType
(1)
  stchen replied...
02-Mar-08 10:42 PM
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".
interface) or on a DataContract type which has member of the base
type/interface. See the following reference:

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:

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.



--------------------

ceiver")]
  Marc replied...
06-Mar-08 03:21 AM
Hi,

Found a solution to your problem? I'm having a simular issue where I get the knowType exception

/Marc

BizTalk Utilities - Frustration free BizTalk Adapters
http://www.topxml.com/biztalkutilities
  coconet replied...
08-Apr-08 11:14 PM
I got into something else and don't remember what the fix should be,
after some more research I think I got it but did not actually
implement a solution.

I think the problem has to do with my using a List<T> of an Interface
for a return type and for a parameter. Whent he List<T> is serialized,
the receiver expects an array of a concrete type that it can convert
to the list of a type. When passing a list of Interface, the class
that implements it could be "anything" so it doesn't rehydrate
properly.

I think I was being too lazy in hoping I could just pass things around
this way, I am pretty sure that changing the method signature to
take/receive an array of each concrete type (instead of a single
method that gives/expects a List of Interface) would work.
Create New Account
help
My cap kills my brainBizTalk Utilities - Frustration free BizTalk .NET Framework My cap kills my brain BizTalk Utilities - Frustration free BizTalk Adapters http: / / www.topxml.com / biztalkutilities .NET Web Services Discussions Utilities (1) Adapters (1) Asta la vista, wht does it mean? BizTalk Utilities - Frustration free BizTalk Adapters http: / / www.topxml.com / biztalkutilities Alliluja, or smth like that BizTalk Utilities - Frustration free BizTalk Adapters http: / / www.topxml.com / biztalkutilities Vse promolchat nikto ne skazhet BizTalk Utilities - Frustration free BizTalk Adapters http: / / www.topxml.com / biztalkutilities Ne v dengah ne v
WCF Consumes Too Much CPU and Then Hangs .NET Framework Scenario: WPF and ASP.NET clients connect to WCF. WCF connects to SQL Server. System: WCF Server = 8 cores / 32 GB RAM, Windows 2008, .NET3.5 Problem: The Windows Communications Foundation (WCF) Service is started and used approximately 8% - 13% of the CPU resources for each of less than 10 users). Thanks! .NET Web Services Discussions SQL Server (1) ASP.NET (1) WCF (1) WPF (1) Database (1) Consumes (1) Windows (1) NET3.5 (1) You're not 20091016) _ __ __ __ __ _ The message was checked by ESET NOD32 Antivirus. http: / / www.eset.com keywords: WCF, Consumes, Too, Much, CPU, and, Then, Hangs description: Scenario: WPF and ASP.NET clients connect to WCF. WCF connects to SQL Server. System: WCF Server = 8 cores / 32 GB RAM, Windows 2008
how to call wcf service from another wcf service or class library? .NET Framework Hi! I??m having problem consuming a WCF Service. To call this Service, I created my own WCF Service with VS2008 own template. Then I added a Service Reference to the WCF Service to consume. So far so good, the service shows up in the Solution Explorer methods as well. Then I Created a Class to call the Service from my own WCF Service. And everytime I try to create an object I get the same error message web without success. The web.config file shows the correct naming and URL of the WCF service I am trying to call. As a side note, creating a service reference and calling the WCF service from a WinForm app works perfectly. Basically I need to do this from a
Difference between Ajax Enabled WCF service and regular WCF? .NET Framework I am sort of new to WCF and I was wondering the difference between Ajax Enabled WCF service and regular WCF? You're adding, and do not have an interface class. When do you want to use each one? What do the following lines give you below, vs a regular WCF service? [ServiceContract(Namespace = "")] [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] ASP.NET Discussions AspNetCompatibilityRequirementsMode (1) ASP.NET (1) AspNetCompatibilityRequirements (1) WCF (1) Grid (1) Difference (1) Database (1) RequirementsMode (1) It just means that you can call the WCF directly instead of having to go to the WebFrom.aspx.cs or vb codebehind file