ragavan e

ragavan e

  • NA
  • 8
  • 959

Unable to serialize while using Encoding as DataMember in WCF services

Aug 8 2020 3:11 AM
Hi,
 
In WCF services in Datacontract ,I am using Encoding as datamember . This is my service code .
  1. [ServiceContract]  
  2. public interface IService1  
  3. {  
  4.   
  5. [OperationContract]  
  6. void GetData(CompositeType compositeType);  
  7. }  
  8.   
  9. [DataContract]  
  10. public class CompositeType  
  11. {  
  12. [DataMember]  
  13. public Encoding MessageEncoding  
  14. {  
  15. get;  
  16. set;  
  17. }  
  18. }  
  19. }
Client code :
  1. EndpointAddress addr = new EndpointAddress("net.pipe://localhost/Service1");  
  2. ChannelFactory<IService1> chn = new ChannelFactory<IService1>(netNamedPipeBinding, addr);  
  3. service1 = chn.CreateChannel();CompositeType wcfFor = new CompositeType  
  4. {  
  5. MessageEncoding = Encoding.UTF8,  
  6.   
  7. };  
  8. service1.GetData(wcfFor);  
When I tried to hit the service from client ,I am getting below exception
 
There was an error while trying to serialize parameter http://tempuri.org/:compositeType. The InnerException message was 'Type 'System.Text.UTF8Encoding' with data contract name 'UTF8Encoding:http://schemas.datacontract.org/2004/07/System.Text' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or 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 the serializer.'. Please see InnerException for more details.
 
I have also tried by using knowntype attribute and datacontract resolver . Still I am facing this same issue
 
Can we use Encoding as datamember in WCF service?

Answers (1)