如何从WCF C#返回DTO对象?

时间:2017-08-04 01:08:49

标签: c# asp.net service entity dto

我有一个wcf服务,我想要返回一个dto对象,然后我想使用WSDL参考服务从其他应用程序web ASP.net中获取服务,但是不能在dto客户端应用程序中使用wcf工作。

并显示此错误:

  

无法将类型'ClientApp.ClientWs.Client'隐式转换为   “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();
        }

   }

2 个答案:

答案 0 :(得分:0)

You can overcome this issue by changing your proxy class.

  • It will generate automatically, when you adding service reference
  • It has all types and methods that your are using in client app

答案 1 :(得分:0)

您是否必须将WCF服务作为WSDL Web引用添加到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服务的引用时,选择&#34;添加服务引用&#34;选项,高级设置,选择参考组件中的&#34;重用类型&#34;复选框,并专门添加WCF服务和Web应用程序使用的DTO程序集:

WCF Reuse Assemblies

注意在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;
}