2011-12-22 35 views
1

我想第一次使用WCF但是我碰到一個問題,我很快就不明白。我還沒有改變自動生成的代碼的原始結構。我得到了這個代碼在網站上工作。使用WCF數據類型

using ServiceReference.ServiceClient wcfClient = new ServiceReference.ServiceClient()) 
{ 
    string data = wcfClient.GetData(32); 
    Label1.Text += data; 
} 

但是當我開始使用此代碼,我得到了一些問題。

ServiceReference.Kund kund; 

using (ServiceReference.ServiceClient wcfClient = new ServiceReference.ServiceClient()) 
{ 
    string data = wcfClient.GetDataUsingDataContract(kund); 
} 

我得到了這個錯誤。我真的不能看到數據類型的任何問題,它不是一個字符串。

無法隱式轉換類型 'Webbshop.ServiceReference.Kund' 到 '字符串'

編輯:

[ServiceContract] 
public interface IService 
{ 
    [OperationContract] 
    string GetData(int value); 

    [OperationContract] 
    Kund GetDataUsingDataContract(Kund kund); 
} 

[DataContract] 
public class Kund 
{ 
    int iD; 
    [DataMember] 
    public int ID 
    { 
     get { return iD; } 
     set { iD = value; } 
    } 
} 

回答

1

,你能否告訴我們的服務合同(即IMyService接口,或任何它叫你的情況)你有?

通常,WCF服務生成將具有一個方法GetData返回字符串的示例應用程序,以及示出如何返回一個複雜的數據類型

[ServiceContract] 
public interface IService1 
{ 
    [OperationContract] 
    string GetData(int value); 

    [OperationContract] 
    CompositeType GetDataUsingDataContract(CompositeType composite); 
} 

在這裏的第二方法:第二方法 - 在你的改動之後 - 返回一個Kund。當然,如果你調用它返回一個Kund第二種方法,你不能只分配整個Kund爲字符串....你會做這樣的事情:

ServiceReference.Kund kund; 

using (ServiceReference.ServiceClient wcfClient = new ServiceReference.ServiceClient()) 
{ 
    Kund returnedKund = wcfClient.GetDataUsingDataContract(kund); 

    // then assign whatever properties from `data` you need to your string ..... 
    string data = returnedKund.ID.ToString(); // or something..... 
} 
+0

我只是改變了數據類型中接口 [ServiceContract] public interface IService { [OperationContract] string GetData(int value); [OperationContract] Kund GetDataUsingDataContract(Kund kund); // TODO: Add your service operations here } [DataContract] public class Kund { int iD; [DataMember] public int ID { get { return iD; } set { iD = value; } } } Frozendragon 2011-12-22 17:25:00

+0

現在我看到了問題,我試圖重新設置字符串的返回值,這很明顯,我錯過了它。 xD – Frozendragon 2011-12-22 17:30:43

+0

@Frozendragon:在那裏,做到了這一點 - 有時它顯得非常明顯,在試圖弄清楚的時候你只是忽略它:-)對我們所有人來說,我很確定:-) – 2011-12-22 17:31:47