Making a class a full fledged webservice
I am not using Visual Studio 2010. I have a class file that I marked as a webmethod. I am able to compile it u开发者_JAVA技巧sing C# compiler targetting the .NET 3.5 framework.
How can I make this a webservice? Any ideas?
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.
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
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.
精彩评论