简体   繁体   English

如何在没有svcutil.exe的情况下使用WCF服务?

[英]How to consume WCF services without svcutil.exe?

I have found 2 ways of consuming a WCF service without the help from svcutil.exe : 在没有svcutil.exe帮助的情况下,我找到了两种使用WCF服务的方法:

  • ClientBase<IService>
  • ChannelFactory<IService>

I know that ClientBase probably uses ChannelFactory . 我知道ClientBase可能使用ChannelFactory But I'm talking about choosing between writing: 但我正在谈论在写作之间做出选择:

public sealed class ServiceClient 
    : ClientBase<IService>, IService
{
    ReturnType IService.MethodName(ParameterType parameterName)
    {
        return Channel.MethodName(parameterName);
    }
}

// later
IService client = new ServiceClient();
var result = client.MethodName(parameterName);

or 要么

ChannelFactory<IMyService> channelFactory = new ChannelFactory<IMyService>();
channelFactory.Open();

var channel = channelFactory.CreateChannel(); 
var result = channel .MethodName(parameterName);

channelFactory.Close();

Which one should I choose? 我应该选择哪一个?

What are your criteria for choosing? 您选择的标准是什么?

Functionally, in the end, both approaches work just fine and basically give you the same result. 从功能上来说,两种方法最终都能正常工作,基本上可以给你相同的结果。

What do you want to base your decision on? 你想做什么决定?

My advice: pick the style that you feel more comfortable with! 我的建议:选择你觉得更舒服的风格! The approach with the ChannelFactory<IService> probably requires you to write less and less mundane code - so maybe that would be a little advantage for that approach. 使用ChannelFactory<IService>可能需要你编写越来越少的普通代码 - 所以这可能对这种方法有点优势。

Both approaches require that you have .NET on both ends of the channel, and that service and client share a common assembly with the service and operation contracts in them - since the client must know at least the service interface in order to be able to use either of the two ways of connecting to the service. 这两种方法都要求您在通道的两端都安装.NET,并且该服务和客户端共享一个包含服务和操作合同的公共程序集 - 因为客户端必须至少知道服务接口才能使用连接服务的两种方式之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM