English 中文(简体)
当使用 IList 或 ICollection 作为数据成员时 WCF 服务出错
原标题:Error on WCF Service when using IList or ICollection as a DataMember

我开发了一个抽样系统,首先使用实体框架代码法,然后将监查系统作为用户界面。在这里,我使用周转基金服务与用户界面和系统进行沟通。当我使用清单或雇员实体的收藏时,在错误发生后给予。我确信这是因为收集的结果。

" 收到 HTTP 对 http:/ localhost/ GreetingHost/ EmployeeService.svc 的回复时发生错误。 这可能是因为服务端点没有使用 HTTP 协议而具有约束力。 也可能是因为服务器中止了 HTTP 请求上下文( 可能是因为服务关闭) 。 请查看服务器日志了解更多细节 。 "

以下是我的实体与服务,

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

< 坚固 > greetingDBCcontext.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);
    }
}

<强度 > 雇员服务.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 Service 应用程序来托管服务。 这里的代码来自 < strong > GreeetingHost 项目。 iemopleServices. cs.

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

    [OperationContract]
    Employee getEmployee(string UserId);
}

<强力>雇员服务.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);
    }

当招募雇员服役时,上面提到的通讯例外被退回,有人能帮我吗?

谢谢!

<强>编辑:

< 坚固 > 用户端 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>

< 坚固 > services 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>

问题回答

你可以试试这个

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

对迟交答复表示歉意 ;)

DbContext 中禁用 Lazyloading





相关问题
Entity Framework with MySQL connector in c#

I have been trying to get the Entity Framework to work in my web application using MySQL. It works fine on my local pc, but doesn t work when I put it on the server. Since the server is a shared ...

How Do I Create And Update A Many To Many Relationship With EF

I am using the Entity Framework with SQL Server. I have a many to many relationship between 2 tables. I have created a join table with just the primary key fields of the 2 tables. In the designer, the ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Linq to enties, insert foreign keys

I am using the ADO entity framework for the first time and am not sure of the best way of inserting db recored that contain foreign keys. this is the code that i am using, I would appreciate any ...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

ADO.NET Entity Data Model are not precise enough

I run this code: var cos = from k in _db.klienci_do_trasy where k.klient_id == 5 select k; but the query send to database is: SELECT * FROM `klienci_do_trasy` LIMIT 0, 30 why is it for, there ...

热门标签