【发布时间】:2020-10-26 21:36:03
【问题描述】:
联系人:
这是我的合约界面,带有 Crud Operation 合约。
[ServiceContract]
public interface IWcfCrudServices
{
[OperationContract]
Object addEmployee(Employees employee);
[OperationContract]
object getAllEmployees();
[OperationContract]
Object updateEmployee(Employees employee);
[OperationContract]
object deleteEmployee(Employees employee);
}
逻辑 带有逻辑粗略方法的类,也许我在这里做错了什么?模型工作正常。
public class WcfCrudService : IWcfCrudServices
{
NORTHWNDContext ctx = new NORTHWNDContext();
public Object addEmployee(Employees employee)
{
ctx.Employees.Add(employee);
ctx.SaveChanges();
return ctx.Employees;
}
public object deleteEmployee(Employees employee)
{
Employees employeeForDelete = ctx.Employees.Find(employee.EmployeeId);
if (employeeForDelete != null)
{
ctx.Employees.Remove(employeeForDelete);
ctx.SaveChanges();
}
return ctx.Employees;
}
public object getAllEmployees()
{
return ctx.Employees;
}
public object updateEmployee(Employees employee)
{
Employees employeeForUpdate = ctx.Employees.Find(employee.EmployeeId);
if ( employeeForUpdate !=null)
{
employeeForUpdate.FirstName = employee.FirstName;
employeeForUpdate.LastName = employee.LastName;
employeeForUpdate.Country = employee.Country;
employeeForUpdate.City = employee.City;
employeeForUpdate.Address = employee.Address;
}
return ctx.Employees;
}
}
}
主机 主机工作正常,我这里肯定没有问题。
class Program
{
static void Main(string[] args)
{
Uri baseHttpUrl = new Uri("http://localhost:8090/MyServices");
using (ServiceHost serviceHost = new ServiceHost(typeof(MyServices.WcfCrudService),baseHttpUrl))
{
serviceHost.AddServiceEndpoint(typeof(MyServices.IWcfCrudServices), new BasicHttpBinding(), "CrudAssig");
serviceHost.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine("Service name: "+serviceHost.Description.Name);
Console.WriteLine("Press <ENTER> to terminate the service!");
Console.ReadLine();
}
}
}
客户 我的方法不管我做什么都行不通。
class Program
{
static void Main(string[] args)
{
Uri endpointAdress = new Uri("http://localhost:8090/MyServices/CrudAssig");
EndpointAddress endpoint = new EndpointAddress(endpointAdress);
IWcfCrudServices proxy = ChannelFactory<IWcfCrudServices>.CreateChannel(new BasicHttpBinding(), endpoint);
}
}
接下来要做什么?我在逻辑或客户中弄错了吗?我正在尝试从服务中调用 CRUD 操作的方法,但它不起作用。
【问题讨论】:
-
您好,请看How to Ask。不幸的是,您的问题很可能已关闭,因为它缺少有关您正在尝试做什么以及遇到什么问题的详细信息。
标签: c# wcf methods client northwind