2017-08-04 22 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

您可以通過更改您的代理類解決這個問題。

  • 它會自動生成,當您添加服務引用
  • 它具有所有類型和方法,你正在使用的客戶端應用程序