如何从 WCF C# return DTO 对象?

How to return DTO object from WCF C#?

我有一个 wcf 服务,我想要 return 一个 dto 对象,然后我想使用 WSDL 参考服务从其他应用程序网络 ASP.net 使用服务,但不能在 dto 客户端中从 wcf 工作 dto应用

并显示此错误:

Cannot implicitly convert type 'ClientApp.ClientWs.Client' to 'CoreApp.DTO.Client

并且 dto "Client" 在两个应用程序中都是相同的

DTO

public class Client
{

    public string status { get; set; }
    public string message { get; set; }
    public List<Sales> listSales { get; set; }

    public Client()
    {
        listSales = new List<Sales>();
    }
}

WCF 服务:

 public Client getClientByID(string id)
 {
        List<Sales> list = null;
        Client clientResponse = null;

        try
        {
            list = new List<Sales>();
            list = bll.getInstance.listSales(id);
            clientResponse = new Client();
            clientResponse.status = "ok";
            clientResponse.message = "success";
            foreach (var item in list)
            {
                clientResponse.listSales.Add(item);
            }


        }
        catch (Exception ex)
        {
            clientResponse = new Client();
            clientResponse.status = "error";
            clientResponse.message = ex.Message;
        }

        return clientResponse;
  }

方法应用客户端:

 public static List<Sales> getByIdWebService(string id)
 {


       List<Sales> list = null;
        ClientWs.ClientWsClient ws = new ClientWs.ClientWsClient;

       Client response = new Client();
       response = ws.getClientByID(id); //show error 

        if (response.status == "error")
        {
            throw new Exception(response.message);
        }
        else
        {
            list = new List<Sales>();
            list = response.listSales();
        }

   }

您可以通过更改代理来解决此问题 class。

  • 它会在您添加服务引用时自动生成
  • 它包含您在客户端应用程序中使用的所有类型和方法

是否必须将 WCF 服务添加到 Web 应用程序作为 WSDL Web 引用?
如果将其添加为真正的服务引用,您可以选择重新使用其他程序集的选项,包括您的 DTO。

编辑:可能有一种方法可以对 WSDL 引用做同样的事情,但它可能不是那么简单……至少,这是我不知道它的借口:)

首先,您可能希望使用 [DataContract][DataMember] 装饰器将 DTO 类 和成员标记为可序列化:

namespace DtoClassLib
{
    [DataContract]
    public class Sales
    {
        [DataMember]
        public string Name { get; set; }
    }

    [DataContract]
    public class Client
    {
        [DataMember]
        public string status { get; set; }

        [DataMember]
        public string message { get; set; }

        [DataMember]
        public List<Sales> listSales { get; set; }

        public Client()
        {
            listSales = new List<Sales>();
        }
    }
}

添加对WCF服务的引用时,select"Add Service Reference"选项,高级设置,select"Reuse types in referenced assemblies"复选框,具体添加使用的DTO程序集通过 WCF 服务和 Web 应用:

注意将您的服务代理正确包装在 using 或 try-finally 块中:

public static List<Sales> getByIdWebService( string id )
{
    List<Sales> list = null;

    using ( WcfSvcRefAsWsdl.ClientWsClient wsdlClient = new WcfSvcRefAsWsdl.ClientWsClient() )
    {
        // The following will not compile
        // DtoClassLib.Client returnClient = wsdlClient.getClientByID( id );
    }

    using ( WcfSvcRef.ClientWsClient wcfClient = new WcfSvcRef.ClientWsClient() )
    {
        // Joy!  \o/
        DtoClassLib.Client client = wcfClient.getClientByID( id );
        list = client.listSales;
    }

    return list;
}