Type not displayed by WCF

I have a small test web service to emulate something strange that I notice in a real world application. Since the demo demonstrates the same behavior as the application, I will use the demo for brevity.

In short, my service interface file looks like this (as you can see, this is the default WCF service created by VS2008, but I added a new public method (GetOtherType ()) and two new classes below (SomeOtherType and SomeComplexType) SomeOtherType manages a generic list like SomeComplexType

using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServiceTest { [ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); [OperationContract] SomeOtherType GetOtherType(); } [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } [DataContract] public class SomeOtherType { public List<SomeComplexType> Items { get; set; } } [DataContract] public class SomeComplexType { } } 

My service is implemented as follows

 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.Text; namespace WCFServiceTest { public class Service1 : IService1 { #region IService1 Members public string GetData(int value) { throw new NotImplementedException(); } public CompositeType GetDataUsingDataContract(CompositeType composite) { throw new NotImplementedException(); } #endregion #region IService1 Members public SomeOtherType GetOtherType() { throw new NotImplementedException(); } #endregion } } 

The problem is that if I include a service link to this service in an ASP.NET web application, I cannot see SomeComplexType through intellisense. The error is because the type or namespace was not found. However, SomeOtherType can be found (I assume the type is the return type from one of the public methods).

Do I understand correctly that I cannot expose a type from the WCF service if this type is not specified in the method signature of one of my public methods (return type or argument)? If so, how could I iterate over the elements inside the SomeOtherType instance on the client?

Thanks a lot, and I hope this is understandable.

Simon

+4
source share
5 answers

The problem is that if I enable the link to this service service in an ASP.NET web application, I cannot see SomeComplexType through IntelliSense. The error is related to the type or namespace could not be found. However, SomeOtherType can be found (I assume that the type is a return type from one of the public methods).

Am I right in thinking that I cannot expose a type from the WCF service if this type is not specified in the method signature of one of my public methods (return type or argument)? If so, how could I iterate over the elements inside the SomeOtherType instance on the client?

You are absolutely right: your SomeComplexType never used in any of the service methods, and it is also never marked as [DataMember] in any of the types that are really used as parameters in your service methods. Therefore, from the point of view of WCF, it is not needed and will not be displayed in WSDL / XSD for the service.

As Graham already noted, you are using SomeComplexType in one place:

 [DataContract] public class SomeOtherType { public List<SomeComplexType> Items { get; set; } } 

but since the Items element is not marked as [DataMember] , it (and therefore the type it uses) will not be included in your serviceโ€™s WSDL / XSD. Since Items not marked as DataMember , they will also not be in your serialized WCF message, so you do not need to repeat this collection :-)

Most likely what you really want, just add the [DataMember] to the Items property; then it will be included in WSDL / XSD, and it will also be SomeComplexType .

+4
source

It looks like you need the [DataMember] in your SomeOtherType.Items property, i.e.

 [DataMember] public List<SomeComplexType> Items { get; set; } 
+2
source

I'm not an expert on this topic at all, as the shot is in blue: are empty DataContracts discarded by WCF? Try to expose something in ComplexDataType (a little int enough) and see if this has changed.

In addition, I believe that you can check the availability of this type using the built-in wcftestclient (you need to enable metadata sharing for this).

+1
source

We can use the Know type in the Service to access the class and its members if it is not used directly or indirectly in the signature of the operation contract.

+1
source

For those types where you simply want it to be available on the client side, even if you do not use it, it is convenient to make it available on the client side below the attribute class.

  [KnownType(typeof(SomeComplexType))] 
0
source

Source: https://habr.com/ru/post/1304994/


All Articles