简体   繁体   English

如何为 WCF 创建客户端库

[英]How do I create a client-side library for WCF

I am new to .NET and I'm researching options for implementing a client/server architecture.我是 .NET 的新手,我正在研究实现客户端/服务器架构的选项。

I've followed the MSDN tutorial for creating a simple WFC Calculator client and server but I think I want to produce some sort of simple client–side library that a third party could use to call my service.我已经按照 MSDN 教程创建了一个简单的 WFC 计算器客户端和服务器,但我想我想生成某种简单的客户端库,第三方可以使用它来调用我的服务。

I've made a class library project to which I added auto generated proxy coded but found that the app.config still needs to be included in the client application rather than the library.我已经创建了一个类库项目,我在其中添加了自动生成的代理编码,但发现 app.config 仍然需要包含在客户端应用程序中,而不是包含在库中。

Question

In my target design, I'd prefer to encapsulate most (but not all) of the app.config data inside something that the company producing the client can't touch.在我的目标设计中,我更愿意将大部分(但不是全部)app.config 数据封装在生产客户端的公司无法触及的内容中。 app.config includes some information I wouldn't want a third party to modify and some information, such as the address (including port) of the service that I'd want to be configurable at installation time. app.config 包含一些我不希望第三方修改的信息和一些信息,例如我希望在安装时可配置的服务的地址(包括端口)。

Can anyone point me at further reading, samples or tutorials that would help me progress my research?任何人都可以向我指出可以帮助我推进研究的进一步阅读材料、示例或教程吗? (I realise I may not even be trying to do something sensible!) (我意识到我什至可能不会尝试做一些明智的事情!)

Background: What I'm really trying to do背景:我真正想做的事

I need to design a service that can be called from a .NET 3.5 client.我需要设计一个可以从 .NET 3.5 客户端调用的服务。 I don't yet know what technology will be used for the service (eg it could be written in Java not .NET) or the protocols that will be used for the communication between client and service.我还不知道服务将使用什么技术(例如,它可以用 Java 而不是 .NET 编写)或将用于客户端和服务之间通信的协议。 It is also possible that the implementation of the service and the communications protocols used may need to change about a year or so after the initial deployments.在初始部署后大约一年左右,服务的实施和使用的通信协议也可能需要更改。

However, I do know the list of methods that I want to present to the client and the data types to be used for parameters and responses.但是,我确实知道要呈现给客户端的方法列表以及用于参数和响应的数据类型。 I also need to document and deliver a version of the client interface to a separate company who will add calls to my service to their existing .NET client application early in the project.我还需要记录一个版本的客户端界面并将其交付给一家单独的公司,该公司将在项目早期将我的服务调用添加到他们现有的 .NET 客户端应用程序中。 So, I'd like to develop a client library that would initially be used to connect to a temporary test version of the service (which could be written in .NET) to return simulated responses.因此,我想开发一个客户端库,最初用于连接到服务的临时测试版本(可以用 .NET 编写)以返回模拟响应。 Later versions of the client library would look the same to the calling client application but connect to the real service instead.客户端库的更高版本在调用客户端应用程序时看起来相同,但改为连接到实际服务。

WCF looks like it has the flexibility I need to allow me to design the client interface independently of the service and connection protocol to the service. WCF 看起来具有我需要的灵活性,允许我独立于服务和服务的连接协议设计客户端接口。 But as I said above, app.config includes some information I wouldn't want a third party to modify and some information, such as the address (including port) of the service that I'd want to be configurable at installation.但正如我上面所说,app.config 包含一些我不希望第三方修改的信息和一些信息,例如我希望在安装时可配置的服务的地址(包括端口)。

You could go for ChennelFactory and you could indeed keep most of the configuration information in the database(ex: endpoint address), your client library which is supposed to handle the communication between the 3rd party client app and your service should read the service config details from the DB and invoke the required service and returns the result set the 3rd party app.您可以选择ChennelFactory并且您确实可以将大部分配置信息保留在数据库中(例如:端点地址),您的客户端库应该处理第 3 方客户端应用程序和您的服务之间的通信,应该读取服务配置详细信息从数据库调用所需的服务并返回第 3 方应用程序的结果集。

But why you need to hide the service details from the 3rd party application/users?但是为什么您需要对 3rd 方应用程序/用户隐藏服务详细信息? service details are not something you don't want to expose to end users.服务详细信息不是您不想向最终用户公开的内容。 why not simply expose the service with adequate WCF Security in place and let them call the relevant service.为什么不简单地公开具有足够WCF 安全性的服务并让他们调用相关服务。

if you are going for a separate client library/application to encapsulate the service details, if you need to do deliver any fix for this in the future and if the user base is large then you need to worry about upgrade mechanism and all.如果您要使用单独的客户端库/应用程序来封装服务详细信息,如果您将来需要为此提供任何修复,并且用户群很大,那么您需要担心升级机制等等。 this will add more complexity.这将增加更多的复杂性。

If you can control the installation of the client, you could lock-down access to app.config file to certain user accounts.如果您可以控制客户端的安装,则可以将 app.config 文件的访问权限锁定到某些用户帐户。

Another option would be to encrypt the contents app.config file before deploying it.另一种选择是在部署之前加密内容 app.config 文件

It's all about a trade-off between how convenient you want to make configuring the client versus how difficult you want it to be to tamper with.这完全是在您希望配置客户端的方便程度与您希望篡改客户端的难度之间进行权衡。

Edit: If your goal is to simply hide your port numbers and addresses, that's not realistically possible.编辑:如果您的目标是简单地隐藏您的端口号和地址,那实际上是不可能的。 Someone can just use a network traffic monitor to find out that information.有人可以使用网络流量监视器来找出该信息。

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

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