如何在客户端编程方式连接到WCF服务? [英] How to programmatically connect a client to a WCF service?

查看:105
本文介绍了如何在客户端编程方式连接到WCF服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个应用程序(客户端)连接到暴露的WCF服务,而不是通过应用程序配置文件,但在code。

I'm trying to connect an application (the client) to an exposed WCF service, but not through the application configuration file, but in code.

我应该如何去这样做呢?

How should I go about doing this?

感谢。

推荐答案

您将不得不使用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();
    }
}

相关资源:

  • How to: Use the ChannelFactory

这篇关于如何在客户端编程方式连接到WCF服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆