【发布时间】:2013-12-15 06:28:42
【问题描述】:
我按照本教程进行了基本的 WCF 进程间通信: http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication
我有一个客户端和一个服务器,它们都用作控制台应用程序(如教程创建的那样)。
但是当我将 WCF 服务器代码移动到表单中时,它不起作用。
控制台应用程序服务器代码(作为控制台应用程序工作正常):
[ServiceContract]
public interface IModelData
{
[OperationContract]
ArrayList GetData();
}
public class ModelDataServer : IModelData
{
public ArrayList GetData()
{
return new ArrayList(); // Just for testing that it works (before form version)
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(
typeof(ModelDataServer),
new Uri[]{
new Uri("http://localhost:8000")
}))
{
host.AddServiceEndpoint(typeof(IModelData),
new BasicHttpBinding(),
"ModelData");
host.Open();
Console.WriteLine("Service is available. " +
"Press <ENTER> to exit.");
Console.ReadLine();
host.Close();
}
}
}
我了解到,如果您在 UI 线程上启动 ServiceHost,WCF 和 Windows 窗体就会出现问题。 running WCF in windows forms
我的表单代码:
public partial class ServerForm : Form
{
ModelDataServer Server;
public ScraperForm()
{
InitializeComponent();
Server = new ModelDataServer(); // this should start the server?
}
// other stuff
}
[ServiceContract]
public interface IModelData
{
[OperationContract]
ArrayList GetData();
}
[ServiceBehavior(UseSynchronizationContext=false)]
public class ModelDataServer : IModelData
{
ServiceHost Host;
public ModelDataServer()
{
using (ServiceHost Host = new ServiceHost(
typeof(ModelDataServer),
new Uri[]{
new Uri("http://localhost:8000")
}))
{
Host.AddServiceEndpoint(typeof(IModelData),
new BasicHttpBinding(),
"ModelData");
Host.Open();
}
}
public ArrayList GetData()
{
return GetData();
}
public void CloseServer()
{
Host.Close();
}
}
我不相信我的主机在上面的示例中被垃圾收集,因为它是一个字段。我说的对吗?
从我的客户端(使用上述控制台应用程序)调用此服务器时遇到的错误如下:
There was no endpoint listening at http://localhost:8000/ModelData that could accept
the message. This is often caused by an incorrect address or SOAP action. See
InnerException, if present, for more details.
感谢您的帮助。
【问题讨论】:
-
显示你的 web.config 代码