WCF service: Returning custom objects

后端 未结 3 1425
小鲜肉
小鲜肉 2020-12-06 17:51

I\'m using WCF service in my application. I need to return a custom object in the service class. The method is as follows:

IService.cs:
[OperationContract]
         


        
相关标签:
3条回答
  • 2020-12-06 18:17

    Custom objects are fine, while MS says you don't have to use the [DataContract] or [datamember] attributes anymore, I haven't been successful without them. Try marking you custom object with the attributes and seeing what is going on. You can get more information as to what is explicitly happening by turning on tracing and using svcutil to get trace.

    0 讨论(0)
  • 2020-12-06 18:32

    You should return an instance of a class that is marked with the DataContract attribute:

    [DataContract]
    public class MyClass
    {
        [DataMember]
        public string MyString {get; set;}
    }
    

    Now change your service interface like so:

    [OperationContract]    
    MyClass GetMyClass();  
    

    And your service:

    public MyClass GetMyClass()      
    {     
        return new MyClass{MyString = "Test"};     
    } 
    
    0 讨论(0)
  • 2020-12-06 18:36

    You should return a specific type, not "object". An "object" could be of any type.

    0 讨论(0)
提交回复
热议问题