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

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

问题描述

我正在尝试将应用程序(客户端)连接到公开的 WCF 服务,但不是通过应用程序配置文件,而是通过代码.

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

我应该怎么做?

推荐答案

您必须使用 ChannelFactory 类.

这是一个例子:

var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost/myservice");
using (var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint))
{
    IMyService client = null;

    try
    {
        client = myChannelFactory.CreateChannel();
        client.MyServiceOperation();
        ((ICommunicationObject)client).Close();
        myChannelFactory.Close();
    }
    catch
    {
        (client as ICommunicationObject)?.Abort();
    }
}

相关资源:

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

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