Build a simple SOA architecture of BS (calling the back-end wcf Service Architecture through jquery). On the first day, jquerywcf
After studying jquery and WCF over the past few days, the company now has a requirement:
1. the foreground directly uses js to transmit the method name to the background in json format, and CALLS wcf directly. The background uses the parsing method name,
Call the corresponding method.
2. Technical difficulties: How to directly call the back-end WCF Service through jS.
You can call the corresponding method directly by using the method name.
3. Benefits of the framework: directly from the server to the front-end, saving a lot of intermediate steps. Greatly reduced the amount of code.
At the same time, the server can easily cross-platform.
4. Start to analyze the architecture:
First, the parameters to be transmitted at the foreground include the database name, method, and parms, and then transmitted to the background in json format.
Then, the backend encapsulates the parameters of the method to find the corresponding method.
Let's start programming on the first day:
First, we need to create a new web project named Client. This is the Client. Later, we will directly access the server from the Client.
Create a class library named Server, for example:
Next, we will create an Ajax wcf Service in the client named webService.
Next, let's write the operations at the service layer. on the server side, we create a webService. cs class: (first, we need to introduce
Using System. ServiceModel;
Using System. ServiceModel. Web;
) Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. data; using System. serviceModel; using System. serviceModel. web; namespace Server {public class webService: IService {public string select (string database, string method, string SQL) {object [] obj = new object [2]; obj [0] = database; obj [1] = SQL; DataTable dt = (DataTable) Common. execute ("Server", "DataBase", method, obj); return Common. dataTableToJson ("data", dt) ;}} [ServiceContract] public interface IService {[OperationContract] [WebInvoke (ResponseFormat = WebMessageFormat. json, BodyStyle = WebMessageBodyStyle. wrappedRequest)] string select (string database, string method, string SQL );}}View Code
Explanation: 1. The IService interface indicates the contract we call in WCF (rules that everyone follows), that is, the contract configured in WCF.
2. [ServiceContract] indicates that this interface can be called by wcf
3. [OperationContract] indicates that this method can be called by wcf
4. [WebInvoke (ResponseFormat = WebMessageFormat. Json, BodyStyle = WebMessageBodyStyle. WrappedRequest)]
WebInvoke indicates that post can be obtained, and ResponseFormat indicates that the request is sent in json format.
BodyStyle representation, which indicates that json is used only when a request is sent, and json is not used for response.
5. You can see all the methods called in the downloaded source code below.
So far, you can clearly see how the background code is written !!!!
Download VS2012 source code:
Http://files.cnblogs.com/files/liujing379069296/Vs2012Client.rar
Download vs2010 source code
Http://files.cnblogs.com/files/liujing379069296/VS2010Client.rar