2011-07-22 40 views
1

我不確定如何去這,WCF SQL插入GUID轉換錯誤

我想用WCF添加唯一標識符爲我的用戶,當我去一個GUID添加到我的客戶的DataContext它拋出這個錯誤

錯誤2
參數1:無法從 '的System.Guid' 轉換爲 'ServiceFairy.client' C:\用戶\約翰\文檔\ Visual Studio的 2010 \項目\ PremLeague \ ServiceFairy \ Service1.svc .cs

有什麼機會可以幫忙?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 
using System.Data.SqlClient; 
using System.Diagnostics; 

namespace ServiceFairy 
{ 
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class  name "Service1" in code, svc and config file together. 
public class Service1 : IService1 
{ 
    public List<match> GetAllMatches() 
    { 
     matchtableDataContext context = new matchtableDataContext(); 
     var matches = from m in context.matches orderby m.Date select m; 
     return matches.Take(100).ToList(); 
    } 

    public List<premtable> GetTable() 
    { 
     premContext context = new premContext(); 
     var table = from user in context.premtables orderby user.ID select user; 
     return table.Take(100).ToList(); 
    } 

    public Dictionary<Guid, Uri> _clientUris = new Dictionary<Guid, Uri>(); 

    public void Subscribe(Guid clientID, string uri) 
    { 
     ClientsDBDataContext context = new ClientsDBDataContext(); 
     context.clients.Insert(clientID);   
    }  
+3

ServiceFairy? ... –

+0

您的問題不清楚 –

+4

您認爲這與WCF有什麼關係? –

回答

2

的問題是在你的訂閱方法在這一行:

context.clients.Insert(clientID); // error: clientID is the wrong type 

你傳遞一個類型的clientID GUID來插入()的時候,而不是你應該通過ServiceFairy.client類型的對象。

看起來你應該創建一個新的client對象和保存是:

var client = new ServiceFairy.client() { ClientID = clientID }; // TODO: set other properties 
context.clients.Insert(client); 

由於TODO表示,你也應該被設置其他client性能。

+0

感謝您的幫助Keith我真的很感謝這一點,我將從這一點着手,我知道有一個問題,我沒有通過正確的對象,我編輯前的原始問題我想知道如何解決這個問題 –

+1

約翰沒問題。還要確保你使用clientID所做的是正確的。它假定Subscribe的調用者想要設置clientID,但通常這些字段在Insert中自動生成。不確定你的設計需要什麼。 – Keith

2

誤差如下:

錯誤2
參數1:不能從 '的System.Guid' 轉換到 'ServiceFairy.client' C:\用戶\約翰\文件\ Visual Studio中 2010 \項目\ PremLeague \ ServiceFairy \ Service1.svc.cs 36 44 ServiceFairy

讀這篇文章,我們有:

  1. 參數1存在問題。在描述的行檢查代碼,我們可以看到第一個參數是'clientId'。
  2. 'clientId'對象的類型是'System.Guid'。
  3. 第一個對象需要是'ServiceFairy.client'類型。
  4. 系統無法神奇地將'System.Guid'轉換爲'ServiceFairy.client'。

因此,解決方案是自己弄清楚如何獲取'ServiceFairy.client'對象。

+0

謝謝,我明白了錯誤,但我不知道如何去做,我知道我不能通過GUID,而不是試圖解釋我在做什麼,顯示代碼更容易, –