如何在没有 svcutil 的情况下创建客户端代理或在 wcf 中添加服务引用?

如何在没有 svcutil 的情况下创建客户端代理或在 wcf 中添加服务引用?

问题描述:

如何在没有 svcutil.exe 的情况下创建客户端代理或在 wcf 中添加服务引用?我想在编译时创建一个客户端代理.

How can I create a client proxy without svcutil.exe or add service reference in wcf? I want to create a client proxy at compile time.

如果您可以访问单独 DLL 中的服务契约(IService 接口),您可以添加对该服务契约 DLL 的引用,然后执行类似的操作:

If you have access to the service contract (the IService interface) in a separate DLL, you can add a reference to that service contract DLL and then do something like:

NetTcpBinding binding = new NetTcpBinding();
EndpointAddress address = new EndpointAddress("net.tcp://localhost:9000/YourService")

ChannelFactory<IService> factory = new ChannelFactory<IService>(binding, address);
IService proxy = factory.CreateChannel();

然后您就拥有了以编程方式创建的代理,您现在可以随意使用它.

and then you have your programmatically created proxy, which you can now use as you wish.