我开发了一个抽样系统,首先使用实体框架代码法,然后将监查系统作为用户界面。在这里,我使用周转基金服务与用户界面和系统进行沟通。当我使用清单或雇员实体的收藏时,在错误发生后给予。我确信这是因为收集的结果。
" 收到 HTTP 对 http:/ localhost/ GreetingHost/ EmployeeService.svc 的回复时发生错误。 这可能是因为服务端点没有使用 HTTP 协议而具有约束力。 也可能是因为服务器中止了 HTTP 请求上下文( 可能是因为服务关闭) 。 请查看服务器日志了解更多细节 。 " strong >
以下是我的实体与服务,
雇员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>