如何在运行时通过 URL 使用 WCF Web 服务? [英] How to consume WCF web service through URL at run time?

查看:34
本文介绍了如何在运行时通过 URL 使用 WCF Web 服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 URL 访问服务中公开的所有方法.如果假设 URL 是:

I want to access all the methods exposed in the service through the URL. if suppose the URL will be :

http://localhost/MyService/MyService.svc

我如何访问方法:

  1. 如果我有一个 ServiceReference
  2. 如果没有服务参考该怎么办.

推荐答案

为了使用 WCF 服务,您需要创建一个 WCF 客户端代理.

In order to use a WCF service, you will need to create a WCF client proxy.

在 Visual Studio 中,您可以右键单击项目并从上下文菜单中选择添加服务引用".输入您要连接的 URL,如果该服务正在运行,您应该会为您生成一个客户端代理文件.

In Visual Studio, you would right-click on the project and pick the "Add Service Reference" from the context menu. Type in the URL you want to connect to, and if that service is running, you should get a client proxy file generated for you.

此文件通常包含一个名为 MyServiceClient 的类 - 您可以实例化该类,并且您应该可以看到该客户端类上的所有可用方法.

This file will typically contain a class called MyServiceClient - you can instantiate that class, and you should see all the available methods on that client class at your disposal.

如果您不想在 Visual Studio 中添加服务引用,您可以通过执行 svcutil.exe 命令行工具来获得相同的结果 - 这也将为您的客户端代理类.

If you don't want to add a service reference in Visual Studio, you can achieve the same result by executing the svcutil.exe command line tool - this will also generate all the necessary files for your client proxy class for you.

马克

更新:
如果您想在运行时初始化客户端代理,您绝对可以这样做 - 您需要决定使用哪个绑定(传输协议),以及要连接到哪个地址,然后您可以这样做:

UPDATE:
if you want to initialize a client proxy at runtime, you can definitely do that - you'll need to decide which binding to use (transport protocol), and which address to connect to, and then you can do:

BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8888/MyService");

MyServiceClient serviceClient = new MyServiceClient(binding, address);

但即使在这种情况下,您也需要先使用添加服务引用"或 svcutil.exe 工具导入并创建代理客户端.

But even in this case, you need to have imported and created the proxy client first, by using the "Add Service Reference" or svcutil.exe tools.

这篇关于如何在运行时通过 URL 使用 WCF Web 服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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