0

I'm developing a sample system that use Entity Framework code first approach and MVC as user interface. Here I use WCF services to communicate with user interface and the system. When I use a List or a collection in my Employee entity It given following error. I'm sure that's due to the collection.

" An error occurred while receiving the HTTP response to http:/localhost/GreetingHost/EmployeeService.svc. 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. "

Here are my entities and services that included in Greeting Project :

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;
    }

}

I used WCF Service application to host the services. Here codes are from 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);
    }
}

Service host successfully :

In my MVC application Used above service reference:

    EmployeeServiceClient employeeService = new EmployeeServiceClient();

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

        return View(emp);
    }

When gathering employee from service , above mentioned CommunicationException was returned. Can anyone help me?

Thank You!

Edit :

Clients' Web.config

<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>

Service's config

<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>

4

2 回答 2

0

你可以试试这个——

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

这么晚才回复很抱歉 ;)

于 2015-06-04T14:08:20.710 回答
0

LazyLoading中禁用DbContext

于 2015-06-04T14:14:07.430 回答