2012-12-07 2 views
0

J'ai deux applications WinForms: un 'serveur' et un 'client'. Sur le serveurExtraire des informations de 'Serveur' via WCF

private ServiceHost host; 
private const string serviceEnd = "Done"; 

public Form1() 
{ 
    InitializeComponent(); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    List<string> sqlList = new List<string>(); 
    foreach (string line in this.richTextBoxSql.Lines) 
     sqlList.Add(line); 
    SqlInfo sqlInfo = new SqlInfo(sqlList); 

    host = new ServiceHost(
     typeof(SqlInfo), 
     new Uri[] { new Uri("net.pipe://localhost") }); 

    host.AddServiceEndpoint(typeof(ISqlListing), 
      new NetNamedPipeBinding(), 
      serviceEnd); 

    host.Open(); 
} 

public class SqlInfo : ISqlListing 
{ 
    public SqlInfo() {} 

    private List<string> sqlList; 
    public SqlInfo(List<string> sqlList) : this() 
    { 
     this.sqlList = sqlList; 
    } 

    public List<string> PullSql() 
    { 
     return sqlList; 
    } 
} 

[ServiceContract] 
public interface ISqlListing 
{ 
    [OperationContract] 
    List<string> PullSql(); 
} 

Sur le client je

private ISqlListing pipeProxy { get; set; } 
private const string serviceEnd = "Done"; 

public Form1() 
{ 
    InitializeComponent(); 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    List<string> l = pipeProxy.PullSql(); 
    string s = String.Empty; 
    foreach (string str in l) 
     s += str + " "; 
    this.richTextBoxSql.AppendText(s.ToString()); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    ChannelFactory<ISqlListing> pipeFactory = 
    new ChannelFactory<ISqlListing>(
     new NetNamedPipeBinding(), 
     new EndpointAddress(
     String.Format("net.pipe://localhost/{0}", serviceEnd))); 

    pipeProxy = pipeFactory.CreateChannel(); 
} 

Le problème est que quand je tire la List<string> du serveur à l'aide pipeProxy.PullSql() il appelle le constructeur par défaut public SqlInfo() {} et le réglage sqlList = null.

Comment puis-je obtenir ce code pour renvoyer le texte dans le RichTextBox sur l'application du serveur?

Répondre

2

C'est parce que vous utilisez ce type d'hôte de service:

host = new ServiceHost(
     typeof(SqlInfo), 
     new Uri[] { new Uri("net.pipe://localhost") }); 

vous passez un type et le cadre WCF suppose-il pour créer une instance de type SqlInfo pour traiter la demande. Essayez de transmettre une référence à l'instance SqlInfo construite, c'est-à-dire sqlInfo dans votre cas. Use this overload of ServiceHost, il permet de passer tyou l'instance directement:

host = new ServiceHost(
      sqlInfo, 
      new Uri[] { new Uri("net.pipe://localhost") }); 
+0

** Comment puis-je passer une référence dans ce cas ** Je suis nouveau à cela. J'ai réussi à faire cela est une façon très alambiquée - je vais mettre à jour ma question. Merci beaucoup pour votre temps ... – MoonKnight

+0

@Killercam J'ai mis à jour la réponse –

+0

Thnaks beaucoup pour votre temps ... – MoonKnight

Questions connexes