回答
您将不得不使用ChannelFactory类。
下面是一个例子:
var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost/myservice");
var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint);
IMyService client = null;
try
{
client = myChannelFactory.CreateChannel();
client.MyServiceOperation();
((ICommunicationObject)client).Close();
}
catch
{
if (client != null)
{
((ICommunicationObject)client).Abort();
}
}
相关资源:
太好了,谢谢。 作为补充,下面是如何让IMyService对象在您的应用程序中使用:http://msdn.microsoft.com/en-us/library/ms733133.aspx – Andrei 2010-05-31 12:46:03
您应该将'client'强制转换为'IClientClient'为了关闭它。 – Dyppl 2011-05-25 06:03:32
在我的例子中,我假设'IMyService'接口继承自[System.ServiceModel.ICommunicationObject](http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx)。我修改了示例代码以使其更清晰。 – 2011-05-25 09:58:03
你也可以做什么 “服务引用” 生成的代码确实
public class ServiceXClient : ClientBase<IServiceX>, IServiceX
{
public ServiceXClient() { }
public ServiceXClient(string endpointConfigurationName) :
base(endpointConfigurationName) { }
public ServiceXClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress) { }
public ServiceXClient(string endpointConfigurationName, EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress) { }
public ServiceXClient(Binding binding, EndpointAddress remoteAddress) :
base(binding, remoteAddress) { }
public bool ServiceXWork(string data, string otherParam)
{
return base.Channel.ServiceXWork(data, otherParam);
}
}
哪里IServiceX是WCF服务合同
那么你的客户端代码:
var client = new ServiceXClient(new WSHttpBinding(SecurityMode.None), new EndpointAddress("http://localhost:911"));
client.ServiceXWork("data param", "otherParam param");
- 1. 如何以编程方式添加WCF客户端端点?
- 2. 通过HTTPS以编程方式连接到WCF服务
- 3. 将客户端连接到服务器端的方法
- 4. .net-WCF我们如何以编程方式创建客户端?
- 5. 如何以编程方式断开连接到Solace设备的客户端?
- 6. 如何监视客户端与WCF服务的连接?
- 7. 如何在客户端连接时通知主机WCF服务?
- 8. 将WCF服务公开给asmx客户端和WCF客户端
- 9. 如何将聊天客户端连接到游戏服务器?
- 10. 如何将客户端数据附加到服务器连接?
- 11. 如何将多个客户端连接到服务器
- 12. 如何将客户端连接记录到gRPC服务器?
- 13. 连接PuTTY客户端到EC2服务?
- 14. 将客户端连接到便携式PosgreSQL服务器
- 15. 查找连接到WCF服务的客户端的IP /名称?
- 16. 通过Android客户端连接到WCF服务
- 17. 检查所有客户端连接到wcf服务
- 18. 检测当客户端连接到WCF服务
- 19. 与WCF客户端连接
- 20. 客户端连接或服务器端?
- 21. WCF服务客户端
- 22. WCF服务器自动连接到客户端当连接被中止
- 23. 以编程方式创建WCF客户端
- 24. 匿名客户端连接到WCF
- 25. 以编程方式将Silverlight连接到WCF
- 26. 如何以编程方式为客户端和服务器设置baseAddress?
- 27. 以编程方式将数据添加到客户端
- 28. 如何知道羽毛客户端何时连接到服务
- 29. 您如何知道客户端何时连接到服务器?
- 30. WCF客户端到WSE 3.0服务
对于任何搜索这件事,看看这样的回答:http://stackoverflow.com/a/839941/592732 – MarioVW 2014-03-17 18:18:28