1

私はEFでWCFサービスを書いています。私の問題は、顧客エンティティの子を返そうとしたときに発生します。サンプルコードの下:

[DataContract]
public class Customer
{
        [Key]
        [DataMember]
        public int CustomerID { get; set; }

        [DataMember]
        public string FirstName { get; set; }

// Customer has a collection of BankAccounts
        [DataMember]
        public virtual ICollection<BankAccount> BankAccounts { get; set; }
}

[DataContract(IsReference = true)]
    public class BankAccount
    {
        [Key]
        [DataMember]
        public int BankAccountID { get; set; }

        [DataMember]
        public int Number { get; set; }

        // virtual property to access Customer
        //[ForeignKey("CustomerID")]
        [Required(ErrorMessage = "Please select Customer!")]
        [DataMember]
        public int CustomerID { get; set; }

        [DataMember]
        public virtual Customer Customer { get; set; }
    }

私が得るエラー:

An error occurred while receiving the HTTP response to http://localhost:8732/Design_Time_Addresses/MyServiceLibrary/MyService/. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

私が呼び出すサービス機能:

public Customer GetCustomer(int customerId)
        {
            var customer = from c in dc.Customers
                           where c.CustomerID == customerId
                           select c;
            if (customer != null)
                return customer.FirstOrDefault();
            else
                throw new Exception("Invalid ID!");

        }

私はそれをデバッグしようとしましたが、この関数は Customer とその子 BankAccounts を返します。遅延読み込みも無効にしました。この行をコメントアウトすると、

public virtual ICollection<BankAccount> BankAccounts { get; set; } 

顧客クラスを作成すると、BankAccount を取得できないことを除いてすべてが機能し、Customer のみが返されます。私はWCFが初めてなので、助けてください。ありがとう。

だから私は問題を解決する方法を見つけました。BankAccount からの顧客参照を IgnoreDataMember としてマークする必要がありました

[IgnoreDataMember]
public virtual Customer Customer { get; set; }

MyDbContext コンストラクターで ProxyCreation を無効にします。

this.Configuration.ProxyCreationEnabled = false;
4

1 に答える 1

0

この関数は積極的な読み込みを使用しないため、顧客とそのアカウントの両方を返すには、遅延読み込みを有効にする必要があります。遅延読み込みを無効にする場合は、次を使用する必要があります。

dc.Customers.Include("BankAccounts")

とにかく、あなたの主な問題は、デフォルトの WCF シリアライゼーションを壊す循環参照 (顧客がアカウントを参照し、アカウントが顧客を後方参照している) です。それらの存在についてシリアライザーに通知する必要があります。

于 2012-05-08T10:17:39.177 に答える