【问题标题】:Problems Building a WCF CRUD operations project构建 WCF CRUD 操作项目的问题
【发布时间】: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


【解决方案1】:

可以通过服务引用的方式调用wcf服务来检测服务器是否有错误。您可以先为您的服务器发布元数据,然后添加服务引用,并通过服务引用调用服务器。

您可以查看此演示以将元数据添加到您的服务:

<configuration> 

  <system.serviceModel> 

    <services> 

      <service name="Metadata.Example.SimpleService"> 

        <endpoint address="" 

                  binding="basicHttpBinding" 

                  contract="Metadata.Example.ISimpleService" /> 

      </service> 

    </services> 

    <behaviors> 

  

    </behaviors>  

  </system.serviceModel> 

</configuration> 

你也可以参考这个链接:

How to: Publish Metadata for a Service Using a Configuration File

通过服务引用调用服务可以检查服务端是否有错误。如果服务调用没有问题,那么代码的逻辑可能是错误的。

【讨论】:

    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-03
    • 1970-01-01
    相关资源
    最近更新 更多