开发者

WCF returning custom types

开发者 https://www.devze.com 2023-01-03 12:42 出处:网络
I\'m a newbie to WCF, trying to perform relatively simple task. I\'m trying to return list of objects read from the database but cannot overcome some really annoying exceptions.

I'm a newbie to WCF, trying to perform relatively simple task. I'm trying to return list of objects read from the database but cannot overcome some really annoying exceptions. The question is very simple? What's wrong with the picture?

[ServiceContract]
public interface IDBService
{        
    [OperationContract]
    string Ping(string name);

    [OperationContract]
    InitBDResult InitBD();
}

public InitBDResult InitBD()
        {
            _dc = new CentralDC();
   开发者_运维技巧         InitBDResult result = new InitBDResult();
            result.ord = _dc.Orders.First();
            return result;
        }


[DataContract]
    public class InitBDResult
    {
        //[DataMember]
        //public List<Order> Orders { get; set; }

        [DataMember]
        public Order ord { get; set; }
    }


Based on what you posted:

public InitBDResult InitBD()
{
    _dc = new CentralDC();
    InitBDResult result = new InitBDResult();
    result.ord = _dc.Orders.First();
    return result;
}

Is that method contained in a class that implements the IDBService interface?? That's not quite clear from your post....

[DataContract]
public class InitBDResult
{
        //[DataMember]
        //public List<Order> Orders { get; set; }

        [DataMember]
        public Order ord { get; set; }
}

Is the Order class also marked with [DataContract] and any properties that should be serialized with the [DataMember] attributes??

By default, WCF uses the data contract serializer, and it requires the classes to be returned (all of them) to be marked with [DataContract] and inside those classes, all properties and fields that should be returned in the serialized response to have the [DataMember] attribute.

The [Serializable] attribute doesn't do anything for the default WCF serialization. Read up on WCF serialization in MSDN magazine - highly recommended!

For development, it is often helpful to turn on the exception details from your WCF services, so you get more information about what's gone wrong. To do this, you need to have a service behavior in your config:

 <behaviors>
    <serviceBehaviors>
      <behavior name="debugging">
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
  </behaviors>

and then apply that behavior to your service in your config:

<service name="...." behaviorConfiguration="debugging">

Then you'll get the information about details of your exceptions - not just a generic "something went wrong" exception.


For WCF issues like this, it's useful to use the Service Trace Logging facility. You have to enable it in your config. Then, perform the action that fails and look at the log. It'll most likely give you detailed exception messages about the problem.

More information on MSDN:

http://msdn.microsoft.com/en-us/library/ms732023.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消