简体   繁体   English

使用ChannelFactory调用wcf服务的好处是什么

[英]what is the advantage of using ChannelFactory to call wcf service

if i know the service url then i can click on add reference and add the service url to create proxy at client side to consume it but without creating proxy at client side we can consume and call service with the help of ChannelFactory. 如果我知道服务URL,则可以单击添加引用并添加服务URL以在客户端创建代理以使用它,但是无需在客户端创建代理,我们可以在ChannelFactory的帮助下使用和调用服务。

so i like to know when people will go to use ChannelFactory to create proxy at runtime and what is the advantage? 所以我想知道人们何时会在运行时使用ChannelFactory创建代理,这有什么好处?

if i want that if other people know my service url then also they will not be able to add my service as add service reference....how to enable this feature? 如果我想让其他人知道我的服务网址,那么他们也将无法将我的服务添加为添加服务参考。...如何启用此功能? i want other people will not be able to create proxy at their end if they know my service url....is it possible. 我希望其他人如果知道我的服务网址,将无法在其末尾创建代理。 i want that people always has to call ny service using ChannelFactory. 我希望人们总是必须使用ChannelFactory来调用ny服务。 please discuss this issue in details. 请详细讨论此问题。 thanks 谢谢

I've used the ChannelFactory instead of the autogenerated proxies to use the same object model on the server side and on the client. 我使用ChannelFactory而不是自动生成的代理在服务器端和客户端上使用相同的对象模型。 Also here Sharing Interfaces Between a WCF Service and Client (marked w/ ServiceContract) some problems with autogenerated proxies are discussed. 同样在这里, 在WCF服务和客户端之间共享接口(标记为ServiceContract)与自动生成的代理有关的一些问题也被讨论了。

As for the hiding metadata, the answer is probably here How to hide wsdl information on WCF? 至于隐藏元数据,答案可能就在这里。 如何在WCF上隐藏wsdl信息?

I usually create two assemblies, one with the service metadata (Interfaces [service contracts] and data objects [datacontracts]) and one with the actual service implementation. 我通常创建两个程序集,一个使用服务元数据(接口[服务合同]和数据对象[datacontracts]),另一个使用实际的服务实现。

Usually i self-host wcf services and skips the DataExchange endpoint-service needed for clients that dont have the meta data (to create proxy's). 通常,我会自行托管wcf服务,并跳过没有元数据的客户端所需的DataExchange端点服务(用于创建代理)。 Clients receive my meta data dll and write their own proxies or uses a custom library together with the metadata dll to create a proxy. 客户端接收我的元数据dll并编写自己的代理,或将自定义库与元数据dll一起使用以创建代理。 Both approaches uses the channelfactory to create the proxy. 两种方法都使用channelfactory创建代理。 If a service is used in a LAN environment I typically setup a discovery service so that clients can find the service url(s) for a specific service interface (custom library code). 如果在LAN环境中使用服务,我通常会设置发现服务,以便客户端可以找到特定服务接口(自定义库代码)的服务url。

Maybe Im just old-school but I like to have control over the process. 也许我只是个老派,但我喜欢对过程进行控制。 Versioning concerns etcetera. 版本问题等等。 Another reason, when there is more than one way to use a technology, I focus on the one that can learn me the most about it. 另一个原因是,当使用一种以上的技术方式时,我将重点放在最能从中学习到有关技术的方式上。

Maybe you should use IIS and service pages (SVC) and auto-proxy creation in Visual Studio if you want to quickly test a service or isn't comfortable with the WCF programming model. 如果要快速测试服务或对WCF编程模型不满意,也许应该在Visual Studio中使用IIS和服务页面(SVC)以及自动代理创建。 Use Channelfactory if you write your own service libraries and need more fine-grained communication control (common service discovery strategy, common configurations/common binding settings, common security settings, hooking into events in the communication stack to run custom code, etcetera). 如果您编写自己的服务库并且需要更细粒度的通信控制(公共服务发现策略,公共配置/公共绑定设置,公共安全设置,挂接到通信堆栈中的事件以运行自定义代码等),请使用Channelfactory。

If you have any clients who are not .NET (like Java or PHP, for example), ChannelFactory won't work for them as that is specific to .NET and WCF. 如果您有不是.NET的任何客户端(例如,像Java或PHP), ChannelFactory将不适用于它们,因为该客户端特定于.NET和WCF。 In that case, you'll either have to publish the metadata or send the client a WSDL so they can create the proxy via whatever means their language of choice uses (I don't know much about Java, PHP, etc so I can't say much more than that definitively). 在那种情况下,您要么必须发布元数据,要么向客户端发送WSDL,以便他们可以通过他们选择的语言使用的任何方式来创建代理(我对Java,PHP等不是很了解,所以我可以)不能说那么多)。

As for using ChannelFactory , I assume you're talking about ChannelFactory<T> , as ChannelFactory itself is an abstract class and can't be instantiated. 至于使用ChannelFactory ,我假设您正在谈论ChannelFactory<T> ,因为ChannelFactory本身是一个抽象类,无法实例化。 Using the channel factory gives a greater degree of control (as others have indicated) - for ChannelFactory<T> the client will need the service contract (interface, not the implementation), so either using a common assembly shared by everyone or providing the interface to the clients are the two easiest ways to achieve this. 使用Channel Factory可以提供更大程度的控制(如其他人所指出的)-对于ChannelFactory<T> ,客户端将需要服务合同(接口,而不是实现),因此可以使用所有人共享的通用程序集或提供接口对客户而言,这是实现此目标的两种最简单的方法。

You can disable publishing metadata (the WSDL) by turning httpGetEnabled to "false" in your config file in the <serviceMetadata> tag: 您可以通过将<serviceMetadata>标记中的配置文件中的httpGetEnabled为“ false”来禁用发布元数据(WSDL):

<system.serviceModel>
  <behaviors>
    <serviceBehavior>
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="false" />
      </behavior>
    </serviceBehavior>
  </behaviors>
</system.serviceModel>

You would then reference this behavior by setting the endpoint element's behaviorConfiguration attribute to "MyServiceBehavior". 然后,您可以通过将端点元素的behaviorConfiguration属性设置为“ MyServiceBehavior”来引用此行为。

You should also remove any mex endpoints as (based on my understanding) that's a newer way for web services to expose their metadata. 您还应该删除任何mex终结点,因为(根据我的理解)这是Web服务公开其元数据的新方法。 If the metadata is not exposed, then clients cannot construct the proxy via the WSDL and will have to do so by some other method. 如果未公开元数据,那么客户端将无法通过WSDL构建代理,而将不得不通过其他方法进行构建。 .NET clients specifically will not be able to use Add Service Reference. .NET客户端特别不能使用“添加服务参考”。

Finally, if you're concerned about access to your service, you should really implement some sort of authentication scheme. 最后,如果您担心对服务的访问,则应该真正实现某种身份验证方案。 If you just simply want to disable publishing (exposing) metadata then setting httpGetEnabled to false and removing any mex endpoints should do the trick. 如果您只是想禁用发布(公开)元数据,则将httpGetEnabled设置为false并删除任何mex端点即可解决问题。

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

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