使课程成为成熟的网络服务
我没有使用 Visual Studio 2010。我有一个标记为 webmethod 的类文件。 我可以使用针对 .NET 3.5 框架的 C# 编译器来编译它。
我怎样才能把它变成一个网络服务?有什么想法吗?
I am not using Visual Studio 2010. I have a class file that I marked as a webmethod.
I am able to compile it using C# compiler targetting the .NET 3.5 framework.
How can I make this a webservice? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一般来说,这是一个坏主意。
Web 服务通常设计为以面向服务的方式运行。它期望通过网络进行调用,并且在设计时就考虑到了这一点。
类通常被设计为在与其方法的调用者相同的进程中运行。例如,调用类的方法通常成本低廉,因此设计具有许多细粒度方法的类是有意义的。调用 Web 服务通常比较昂贵,因为调用通常会通过网络进行。使用更少的、粗粒度的方法来设计服务更有意义。
Web 服务通常应设计为无状态的。一般来说,一个类可以维护状态。一个例子是代表银行帐户的类。对于这样的类来说,维护帐户余额的运行总计并在调用 Deposit 和 Withdraw 方法时更改该总计是非常有意义的。但是,您不希望为网络上的每笔交易调用存款和取款。相反,您可以向服务发送一个 Transaction 对象列表,每个对象代表一次存款或取款。您可以通过一次操作将列表发送到服务,而不是通过网络多次调用。
In general, this is a bad idea.
A web service is generally designed to operate in a Service-Oriented manner. It expects to be called across a network, and is designed with that in mind.
A class is generally designed to operate in a the same process as the callers of its methods. For instance, calling the methods of a class is usually inexpensive, so it makes sense to design the class with many fine-grained methods. Calling a web service is generally more expensive because the calls will usually go across a network. It makes better sense to design the service with fewer, course-grained methods.
A web service should usually be designed to be stateless. A class, in general, can maintain state. An example would be a class that represents a bank account. It makes perfect sense for such a class to maintain a running total of the account balance, and to change that total when Deposit and Withdraw methods are called. However, you would not want to call Deposit and Withdraw for every transaction across a network. Instead, you might send the service a list of Transaction objects, each representing a Deposit or Withdrawal. You would send the list to the service in a single operation, rather than calling across the network many times.
首先,确保您的类继承自 WebService。然后将新的 WebService ASMX 文件/类添加到您的项目中。让新类继承您的主类。然后,您将能够通过 URL 访问新的 Web 服务。例如,http://example.com/WebService.asmx
First, make sure that your class inherits from WebService. Then add a new WebService ASMX file/class to your project. Have the new class inherit your main class. You;ll then be able to access the new webservice through a the url. ex, http://example.com/WebService.asmx
通常,当您将 WebService 作为 Web 项目使用时,它会自动配置。对于普通的类库,最好使用WCF类,并为该类标记使用OperationContract。
对于 WCF,配置类库也非常容易。
Generally a WebService is configured automatically when you use it as a Web project. For normal class library, it is better to use WCF classes and mark using OperationContract for the class.
Configuring a class library is also very easy in case of WCF.