【问题标题】:Calling a Web Service using WCF channel Factory.Is it possible?使用 WCF 通道工厂调用 Web 服务。这可能吗?
【发布时间】:2013-09-29 06:09:26
【问题描述】:

在我正在处理的项目中,我需要调用 webservices (asmx)。我想使用 wcf 和 channelfactory 调用它们(不添加服务参考)。

有些可能有接口(合同),很多没有。

有没有端到端的例子怎么做?

    var service=ChannelFactory<?>... How do I get the webserviceContract.

当然,这必须是能够调用 web 服务 (asmx) 的常见场景

感谢您的宝贵时间

【问题讨论】:

  • 您可以尝试使用适当的 WCF 装饰和 Web 服务的方法签名创建一个接口,并使用它加上 basicHttpBinding。

标签: wcf channelfactory


【解决方案1】:

为了扩展我的评论,您应该能够创建一个接口,该接口的方法与 asmx 服务中的 Web 服务方法相匹配。例如:

网络服务方法

string GetMessage()

void SendMessage(string message)

int AddNumbers(int x, int y)

服务合同

[ServiceContract]
public interface IServiceName
{

    [OperationContract]
    string GetMessage();

    [OperationContract]
    void SendMessage(string message);

    [OperationContract]
    int AddNumbers(int x, int y)
}

频道工厂

ChannelFactory<IServiceName> serviceFactory =
          new ChannelFactory<IServiceName>(new BasicHttpBinding(), 
                             "http://www.services.com/Service.asmx");

不是 100% 肯定这会奏效,但很容易尝试。此外,您可能希望在服务合同 ([ServiceContract(Namespace = "somenamespace")]) 上设置命名空间以匹配旧版 asmx 服务,否则可能无法处理消息。

【讨论】:

  • 感谢您的时间和回答,我想了想,我想做以下事情:临时添加一个 webreference/serviceref 它生成一切然后复制并删除参考。我的问题与这个方法是我不会在我的场景中工作。用户应该能够只放置一个“URL”,并且这些方法将出现在一个表单中并执行它们。我需要某种 asmx 的动态代理。我需要生成这些接口在运行时
  • 如果服务不同并且事先不知道,那么您需要对它们进行某种发现(甚至不确定 amsx 是否支持)。您最好使用某种 Http API,例如 WebClient 或类似的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-05
  • 1970-01-01
  • 1970-01-01
  • 2019-04-26
相关资源
最近更新 更多