来自主机应用程序的WCF调用函数

来自主机应用程序的WCF调用函数,wcf,wcf-hosting,Wcf,Wcf Hosting,我最近才接触WCF,并试图找出实现我的需求的最佳方法。 我有一个承载WCF服务的应用程序,其代码如下: Uri u1 = new Uri("http://localhost:8732/Client1/WcfServiceLibrary1/Service1/"); Uri u2 = new Uri("http://localhost:8732/Client1/WcfServiceLibrary1/Service1/mex"); WSHttpBinding binding = new WSHttpB

我最近才接触WCF,并试图找出实现我的需求的最佳方法。 我有一个承载WCF服务的应用程序,其代码如下:

Uri u1 = new
Uri("http://localhost:8732/Client1/WcfServiceLibrary1/Service1/"); Uri
u2 = new
Uri("http://localhost:8732/Client1/WcfServiceLibrary1/Service1/mex");
WSHttpBinding binding = new WSHttpBinding(); 

sHost = new ServiceHost(typeof(WcfServiceLibrary1.Service1), u1);
ServiceMetadataBehavior meta = new ServiceMetadataBehavior();
meta.HttpGetEnabled = true;
sHost.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), binding, u1); 
sHost.Description.Behaviors.Add(meta); sHost.Open();
我可以在客户端应用程序上创建服务引用,并调用此服务上的方法,没有问题。使用下面的代码

remoteService.Service1Client client = new remoteService.Service1Client(); 
remote.Text = client.GetData(3);
我也可以在没有服务引用的情况下调用方法

EndpointAddress myEndpoint = new EndpointAddress("http://localhost:8732/Client1/WcfServiceLibrary1/Service1/");
WSHttpBinding myBinding = new WSHttpBinding();

ChannelFactory<IService1> ServiceConnectionFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);
IService1 serviceConnection = ServiceConnectionFactory.CreateChannel();
如果我试图在主机应用程序中执行相同的代码,就会出现下面的错误

请求通道在之后等待答复时超时 00:01:00. 增加传递给请求调用的超时值或 增加绑定上的SendTimeout值。分配给……的时间 此操作可能是较长超时的一部分

应用程序如何消费和使用当前托管的WCF服务?我需要在自己的线程中打开服务吗


这个想法是让主机在客户端连接之前触发一些初始化。

您应该能够使用托管它的程序提供的服务。您确定当您尝试使用它时,主机仍处于连接状态,即未调用ServiceHost.Close吗?你能把代码贴在你尝试过的地方并得到报告的错误吗?我在这里找到了答案