C# 从';服务器';通过WCF
我有两个WinForms应用程序:“服务器”和“客户端”。在服务器上C# 从';服务器';通过WCF,c#,winforms,wcf,C#,Winforms,Wcf,我有两个WinForms应用程序:“服务器”和“客户端”。在服务器上 private ServiceHost host; private const string serviceEnd = "Done"; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { List<string> sqlList = new List<
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();
}
私有服务主机;
私有常量字符串serviceEnd=“完成”;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
List sqlList=新列表();
foreach(此.richTextBoxSql.Lines中的字符串行)
添加(行);
SqlInfo SqlInfo=新的SqlInfo(sqlList);
主机=新服务主机(
类型(SqlInfo),
新Uri[]{newURI(“net。pipe://localhost") });
host.AddServiceEndpoint(typeof(isqlisting),
新建NetNamedPipeBinding(),
服务端);
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();
}
公共类SqlInfo:isqlisting
{
公共SqlInfo(){}
私有列表;
公共SqlInfo(List-sqlList):this()
{
this.sqlList=sqlList;
}
公共列表PullSql()
{
返回sqlList;
}
}
[服务合同]
公共接口isqlisting
{
[经营合同]
List PullSql();
}
关于我的客户
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();
}
private-isqlisting-pipeProxy{get;set;}
私有常量字符串serviceEnd=“完成”;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
List l=pipeProxy.PullSql();
string s=string.Empty;
foreach(l中的字符串str)
s+=str+“”;
this.richTextBoxSql.AppendText(s.ToString());
}
私有void Form1\u加载(对象发送方、事件参数e)
{
管道工厂=
新渠道工厂(
新建NetNamedPipeBinding(),
新端点地址(
格式(“net。pipe://localhost/{0}(serviceEnd));
pipeProxy=pipeFactory.CreateChannel();
}
问题是,当我使用pipeProxy.PullSql()
从服务器“拉”出List
时,它正在调用public SqlInfo(){}
默认构造函数并设置sqlList=null
如何让此代码返回服务器应用程序上
RichTextBox
中的文本?这是因为您使用的是此类服务主机:
host = new ServiceHost(
typeof(SqlInfo),
new Uri[] { new Uri("net.pipe://localhost") });
您正在传递一个类型,WCF框架猜测它必须创建类型为SqlInfo
的实例来处理请求。尝试传递对构建的SqlInfo
实例的引用,即在您的案例中的SqlInfo
。
,它允许您直接传递实例:
host = new ServiceHost(
sqlInfo,
new Uri[] { new Uri("net.pipe://localhost") });
在这种情况下,我如何传递推荐信?我是新来的。我已经设法做到这是一个非常复杂的方式-我会更新我的问题。非常感谢您抽出时间。。。