2012-05-24 39 views
0

我正在開發一個使用實體框架代碼優先方法和MVC作爲用戶界面的樣本系統。在這裏,我使用WCF服務與用戶界面和系統進行通信。當我在我的Employee實體中使用一個List或一個集合時,會出現以下錯誤。我相信這是由於收集。 「使用IList或ICollection作爲DataMember時的WCF服務錯誤

」接收到http:/localhost/GreetingHost/EmployeeService.svc的HTTP響應時發生錯誤,這可能是由於服務端點綁定不使用HTTP協議造成的,也可能是由於HTTP請求上下文由服務器(可能由於服務關閉)中止,請參閱服務器日誌詳細信息「

這裏是我的實體和服務,其中包括在問候項目:。

Employee.cs

[DataContract] 
[KnownType(typeof(Greeting))] 
public class Employee 
{ 
    [Key()] 
    [DataMember] 
    public string UserId { get; set; } 
    [DataMember] 
    public string Name { get; set; } 
    [DataMember] 
    public int Age { get; set; } 
    [DataMember] 
    [ForeignKey("EmployeeUserID")] 
    public virtual IList<Greeting> Greetings 
    { 
     get 
     { 
      if (this._greetings == null) 
      { 
       this._greetings = new List<Greeting>(); 
      } 
      return _greetings; 
     } 
     set { _greetings = value; } 
    } 
    private IList<Greeting> _greetings; 
} 

Greeting.cs

[DataContract] 
public class Greeting 
{ 
    [Key()] 
    [DataMember] 
    public int GreetingID { get; set; } 
    [DataMember] 
    public string Salutation { get; set; } 
    [DataMember] 
    public string Message { get; set; } 
    [DataMember] 
    public DayStatusWrapper DayStatusWrapper { get; set; } 
    [DataMember] 
    public string EmployeeUserID { get; set; } 
    [DataMember] 
    public virtual Employee Employee { get; set; } 
} 

GreetingDBContext.cs

public class GreetingDBContext :DbContext 
{  
    public DbSet<Greeting> Greetings { get; set; } 
    public DbSet<Employee> Employees { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); 
     base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<Employee>(). 
       HasMany(d => d.Greetings). 
       WithRequired(c => c.Employee). 
       HasForeignKey(c => c.EmployeeUserID).WillCascadeOnDelete(); 
     modelBuilder.Entity<Employee>() 
       .HasKey(c => c.UserId); 
     modelBuilder.Entity<Greeting>() 
       .HasKey(c => c.GreetingID); 
    } 
} 

EmployeeServices.cs

public class EmployeeService 
{ 
    private GreetingDBContext db = new GreetingDBContext(); 

    public void AddEmployee(string UserId, string Name, int Age) 
    { 
     Employee emp = new Employee(); 
     emp.UserId = UserId; 
     emp.Name = Name; 
     emp.Age = Age; 
     db.Employees.Add(emp); 
     db.SaveChanges(); 
    } 

    public Employee getEmployee(string UserId) 
    { 
     Employee selectedEmployee = null; 
     foreach (Employee item in db.Employees) 
     { 
      if (item.UserId == UserId) 
      { 
       selectedEmployee = item; 
       break; 
      } 
     } 
     return selectedEmployee; 
    } 

} 

我使用WCF服務應用程序託管服務秒。下面的代碼是從GreetingHost project.IEmployeeServices.cs

[ServiceContract] 
public interface IEmployeeService 
{ 
    [OperationContract] 
    void AddEmployee(string UserId, string Name, int Age); 

    [OperationContract] 
    Employee getEmployee(string UserId); 
} 

EmployeeService.svc.cs

public class EmployeeService : IEmployeeService 
{ 
    Greeting.EmployeeService serviceImplementation = new Greeting.EmployeeService(); 

    public void AddEmployee(string UserId, string Name, int Age) 
    { 
     serviceImplementation.AddEmployee(UserId,Name,Age); 
    } 

    public WcfGreating.Employee getEmployee(string UserId) 
    { 
     return serviceImplementation.getEmployee(UserId); 
    } 
} 

服務主機成功:

在我的MVC應用服務上面使用的參考:

EmployeeServiceClient employeeService = new EmployeeServiceClient(); 

    public ActionResult Profile(string id) 
    { 
     Employee emp = employeeService.getEmployee(id); 

     return View(emp); 
    } 

從服務中收集員工時,返回上述CommunicationException。誰能幫我?

謝謝!

編輯:

客戶的網站。配置

<system.serviceModel> 
    <bindings> 
     <wsHttpBinding> 
     <binding name="EmployeeService" closeTimeout="00:01:00" openTimeout="00:01:00" 
      receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" 
      transactionFlow="false" hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" 
      textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false"> 
      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
      <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
      <security mode="Message"> 
      <transport clientCredentialType="Windows" proxyCredentialType="None" 
       realm="" /> 
      <message clientCredentialType="Windows" negotiateServiceCredential="true" 
       algorithmSuite="Default" /> 
      </security> 
     </binding> 
     </wsHttpBinding> 
    </bindings> 
    <client> 
     <endpoint address="http://localhost/GreetingHost/EmployeeService.svc" 
     binding="wsHttpBinding" bindingConfiguration="EmployeeService" 
     contract="EmployeeServiceReference.IEmployeeService" name="EmployeeService"> 
     <identity> 
      <servicePrincipalName value="host/7TK3T3J.fareast.corp.microsoft.com" /> 
     </identity> 
     </endpoint> 
    </client> 
    </system.serviceModel> 

服務的配置

<configuration> 

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
</system.web> 

<system.serviceModel> 
    <behaviors> 
    <serviceBehaviors> 
     <behavior name="MyBehaviors" > 
     <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
    </serviceBehaviors> 
    </behaviors> 
    <bindings> 
     <wsHttpBinding> 
      <binding name="wsHttpBindingWithTXFlow" transactionFlow="true" /> 
     </wsHttpBinding> 
    </bindings> 
    <services> 
     <service name="GreetingHost.EmployeeService" behaviorConfiguration="MyBehaviors"> 
      <endpoint address="http://localhost/GreetingHost/EmployeeService.svc" 
       binding="wsHttpBinding" bindingConfiguration="wsHttpBindingWithTXFlow" 
       name="EmployeeService" contract="GreetingHost.IEmployeeService" /> 
      <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" /> 
     </service> 
    </services> 
</system.serviceModel> 

+1

列表大小是多少?檢查服務是否正常工作,當列表包含單個項目,如果是,那麼你必須增加一些設置值在配置 – VJAI

+0

謝謝馬克,它有5個項目,我嘗試過單個項目,但仍然沒有成功.same錯誤返回。您是否需要查看配置文件? – iJay

+0

是的,請發佈客戶端和服務器配置 – VJAI

回答

0

你可以試試這個 -

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

很抱歉這麼晚纔回復;)

0

disable LazyLoading in DbContext

+0

你能詳細解答這個答案嗎?按照現狀,它可能會提供一個解決方案,但它並不能解釋爲什麼該解決方案是必要的,這使得難以學習。 –

+0

在EF中聲明一個'virtual'成員啓用'Lazy Loading'。這是這裏的問題。 –