Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the packaging skills of WebService

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly analyzes the relevant knowledge points of WebService packaging skills, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what are the packaging skills of WebService".

When I got up this morning, I wanted to talk about WebService in .NET. Of course, I don't want to talk about what WebService is or how to use WebService, because you can easily Google the first 100 pages to find the answer. Today I'd like to share some of my tricks in using WebService.

First, problems are born-there are always headaches behind most solutions.

The legendary WebService has been used for a long time, but it has been used in the usual way: create a WebService project-> write a Web service method-- > add a Web reference to the project-- > call the Web method. This seems to be very good, very in line with the specification, and the effect is OK for a period of time, but gradually with the expansion of the project and the increase of people involved in the project at the same time, more and more people feel that this conventional method is very uncomfortable, why? Every time I modify the WebService side (add and delete Web methods, and change the method name), I update the WebService reference on the reference side, which is actually updating the WSDL file, which is very annoying.

Second, the division of civilization is divided into combination-- it is said that if you divide for a long time, you must be divided.

Well, since adding, deleting, and modifying web method names will cause updates to WSDL, let's just use a unified method as the access entry for webservice, and then internally use switch case to distinguish which method to call. Paste the code first, and then talk about it briefly:

Unified access Interface IRemoteCall:

Public interface IRemoteCall {byte [] GeneralCall (string methodName, params byte [] param);}

Then define a WebService and implement the above interface (the interface has not been really implemented on webservice before, ):

[WebService (Namespace = http://tempuri.org/)][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [ToolboxItem (false)] public class BlogService: System.Web.Services.WebService, IRemoteCall {[WebMethod (EnableSession = true)] public byte [] GeneralCall (string methodName) Params byte [] param) {switch (methodName) {case "LoadBlog": {long blogId = Serializer.DeserializeToObject (param) BLLBlogArtical ba = new AppBlog () .LoadBlog (blogId); return Serializer.SerializeToBinary (ba);} case "DeleteBlog": {/ / To Do Your Code return null;}}

Why do you define the interface IRemoteCall here, mainly for the unified invocation of the webservice service? all webservice classes that implement this interface can be called through GeneralCall. We will talk about the webservice accessor later. Here we will mainly talk about this switch case.

Here we define a unified access portal.

Byte [] GeneralCall (string methodName,params byte [] param)

Pass in the name of the method to be called and the serialized parameters, and return the serialized result. In order to unify the data here, we serialize the parameters and return values into an byte array, that is, using Serializer.SerializeToBinary (object), so that all calls are in a uniform format.

Some people may question whether the method names are already in string form will look ugly, and the string is error-prone, there is no intelligent prompt? That's easy to solve. We can just define the method name as a const constant. Here I have an attitude towards webservice: the webservice layer is to complete the transfer and scheduling work, it only plays the role of acceptance, using it can distribute services arbitrarily, so there is no logic (logic is encapsulated in other dll), at most some data conversion, so I adopted this fuzzy interface approach.

3. Custom webservice accessor

Above, we have completed the work of the webservice side, and then we will implement the flexible call of the client to webservice. The IRemoteCall defined here plays a role. First, we define a webservice accessor class RemoteCaller as follows:

Using System;using System.Collections.Generic;using System.Text;using System.Collections;using System.Web.Services.Protocols; using SharedLib_403; namespace ITIvy.Shared.RemoteCaller {/ remote interface accessor / public class RemoteCaller {private string _ MethodName; private byte [] _ ParamByte; private IRemoteCall _ Caller; private ArrayList _ Params / Parameter list / public ArrayList Params {get {return _ Params;} set {_ Params = value }} / public byte [] ParamByte {get {return _ ParamByte;} set {_ ParamByte = value }} / remote service method name / public string MethodName {get {return _ MethodName;} set {_ MethodName = value }} / remote service invocation interface / public IRemoteCall Caller {get {return _ Caller;} set {_ Caller = value }} / construct / Webservice remote interface public RemoteCaller (IRemoteCall caller) {_ Caller = caller; _ Params = new ArrayList () } / call remote interface / method name / Parameter object / public byte [] Call (string methodName, object param) {try {_ MethodName = methodName _ ParamByte = Serializer.SerializeToBinary (param); return _ Caller.GeneralCall (_ MethodName, _ ParamByte);} catch (Exception ex) {if (ex is SoapException) throw new Exception (SoapException) ex). Detail ["Message"] .InnerText) Else throw ex }} / call remote interface / method name / Parameter list / public byte [] Call (string methodName ArrayList param) {try {_ MethodName = methodName _ Params = param; _ ParamByte = Serializer.SerializeToBinary (_ Params); return _ Caller.GeneralCall (_ MethodName, _ ParamByte);} catch (Exception ex) {if (ex is SoapException) throw new Exception (SoapException) ex). Detail ["Message"] .InnerText) Else throw ex }} / call remote interface / method name / parameter object array / public byte [] Call (string methodName Params object [] param) {try {foreach (object obj in param) _ Params.Add (obj) _ MethodName = methodName; _ ParamByte = Serializer.SerializeToBinary (_ Params); return _ Caller.GeneralCall (_ MethodName, _ ParamByte);} catch (Exception ex) {if (ex is SoapException) throw new Exception (SoapException) ex). Detail ["Message"] .InnerText) Else throw ex }} / call remote interface / public byte [] Call () {try {if (string.IsNullOrEmpty (_ MethodName)) throw new Exception ("remote method cannot be empty!") ; return _ Caller.GeneralCall (_ MethodName, _ ParamByte);} catch (Exception ex) {if (ex is SoapException) throw new Exception (SoapException) ex). Detail ["Message"] .InnerText); else throw ex }} / call remote interface / return value type / public T Call () {byte [] resultByte = Call (); return Serializer.DeserializeToObject (resultByte) } / call remote interface / return value type / / method name / parameter list / public T Call (string methodName, ArrayList param) {byte [] resultByte = Call (methodName, param) Return Serializer.DeserializeToObject (resultByte);} public T Call (string methodName, object param) {try {_ MethodName = methodName; _ ParamByte = Serializer.SerializeToBinary (param); byte [] resultByte = _ Caller.GeneralCall (_ MethodName, _ ParamByte) Return Serializer.DeserializeToObject (resultByte);} catch (Exception ex) {if (ex is SoapException) throw new Exception (SoapException) ex). Detail ["Message"] .InnerText); else throw ex }} / call remote interface / return value type / / method name / parameter object array / public T Call (string methodName Params object [] param) {byte [] resultByte = Call (methodName, param) Return Serializer.DeserializeToObject (resultByte);}

This accessor mainly defines the overloading of a series of access interfaces, making use of the generics of c # to make the interface easier. Haha, this class allows us to call webservice in one sentence, which is quite concise. Notice the IRemoteCall attribute, which means that webservice can be accessed through this accessor as long as a class that implements the interface is passed in. Here is an example of how to use this class:

IRemoteCall Caller = new BlogService.BlogService (); BLLBlogArtical bllArtical = new RemoteCaller (Caller) .call ("LoadBlog", id)

Sorry, I'm wrong, it takes two sentences to call, but there is a lot less data conversion work here, because of generics, hehe, and I can do a lot of work in the RemoteCaller accessor class, such as exception handling, permission verification and so on.

The core of this implementation method is to use the IRemoteCall interface to specify that the implementation of the webservice class is unified GenerateCall, and then all the methods in the webservice class are integrated through switch case to avoid the trouble of updating WSDL frequently. The client uses IRemoteCall to define a webservice accessor class RemoteCaller to provide unified webservice access.

This article mainly analyzes the relevant knowledge points of WebService packaging skills, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what are the packaging skills of WebService".

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report