仅当WCF尚未运行时才启动

本文关键字:启动 运行时 WCF 仅当 | 更新日期: 2023-09-27 18:23:57

我的服务引用中有我的WCF,当我使用visual studio运行多项目同时运行我的WCF时,一切都很好,但我现在正在连接多个客户端,如果它们在没有输入数据之前同时启动,那么它就可以工作了。如果其中一个开始输入数据,则另一个开始,然后擦除输入的数据。我已经尝试过拥有它,所以它将从运行我的WPF中的主机开始。不幸的是,我收到一个错误,说httpGetEnabled需要为false,如果这是false,那么我就无法更新我的服务引用,因为它说存在访问问题。我用来运行主机的代码是.

try
{
    ServiceHost host;
    Service1.Service1Client service = new Service1.Service1Client();
    string baseAddress = "http://localhost:59849/Service1.svc";
    host = new ServiceHost(typeof(Service1.Service1Client));
    host.AddServiceEndpoint(typeof(Service1.IService1),new BasicHttpBinding(), baseAddress);
    host.Open();          
    wcfHostId = wcf.generateId();
    textBox5.Text = "" + wcfHostId;
    button5.IsEnabled = false;
}
catch (Exception ex)
{
    MessageBox.Show("Error = " + ex.Message);
} 

编辑所以基本上,我想说的是,当我自己托管一个WPF,并且连接了一个新的客户端时,它正在擦除存储在服务中的所有变量。我在问,这是因为我主持服务的方式吗?

仅当WCF尚未运行时才启动

错误是我试图发现WCF的方式,但没有添加发现端点。

host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());

需要添加到主机部分,而最终的客户端代码是

var ep = "http://" + System.Net.Dns.GetHostName() + ":8732/DatabaseTransfer/Service1/";
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.SendTimeout = new System.TimeSpan(0, 1, 30);
ChannelFactory<IService1> wcfFactory = new ChannelFactory<IService1>(binding, new EndpointAddress(ep));
IService1 wcf = wcfFactory.CreateChannel();