2017-08-04 49 views
2

我有一个wcf服务,我想要返回一个dto对象,然后我想从使用WSDL引用服务的其他应用程序web asp.net使用服务,但不能在dto客户端应用程序中从wcf工作dto。如何从WCF C#中返回DTO对象?

,并出示此错误:

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

而且DTO “客户” 是在这两个应用程序

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; 
    } 

方法应用程序客户端T:

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(); 
     } 

    } 
+0

你能在这里展示你的'Sales'类属性。 –

回答

0

你有到WCF服务添加到Web应用程序作为WSDL Web引用?
如果将其添加为真正的服务参考,则可以选择重新使用其他装配(包括DTO)的选项。

编辑:可能有一种方法可以为WSDL引用做同样的事情,但它可能不那么直截了当......至少,这是我不知道它:)

首先借口,你可能希望你的DTO类和成员为可序列化与[DataContract][DataMember]装饰标记:

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服务,选择“添加服务引用”选项,选择高级设置,在引用程序集中选中“重用类型”复选框,并特别添加WCF服务和Web应用程序使用的DTO程序集:

WCF Reuse Assemblies

注意正确的包装您的服务代理在using或尝试-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; 
} 
0

你有型ClientWsClient

ClientWs.ClientWsClient ws = new ClientWs.ClientWsClient; 

然后一个对象类型的responseClient

Client response = new Client(); 

然后你正在试图调用Client类方法与对象ws一个ClientWsClient对象。

response = ws.getClientByID(id); 

ws不是Client对象。

它们是不同的对象类型。

至于:

I have a wcf service, I want return a dto object, then I want consuming service from other app web ASP.net using WSDL reference service, but not work dto from wcf in dto client app.

我不知道你的意思 - 如果你想创建一个单独的模型来显示数据?在这种情况下,对于每个Client,您必须创建一个对应的ClientWsClient对象,该对象的Client主键作为ClientWsClient对象中的标识符。

因此,像:

int id = ClientWsClient.GetId(); 

response = getClientByID(id); 
+0

虽然有点混乱,但我认为“response = ws.getClientByID(id);”只要“客户”数据类型与WCF方法的结果相匹配即可。 –

+0

@VinceI啊是的 - 我会更新我的答案 –

0

您可以通过更改您的代理类解决这个问题。

  • 它会自动生成,当您添加服务引用
  • 它具有所有类型和方法,你正在使用的客户端应用程序