The SDK that is being developed to process WeChat requests in. NET Core 1.0 with the most simplified operations, coresdk

Source: Internet
Author: User

A Development SDK that processes requests in. NET Core 1.0 with the most simplified operations, coresdk

The existing SDK library is in. NET Core 1.0 indicates that there is a lot of pressure, such as Deepleo. weixin. SDK and Senparc. weixin (even if one existing one is transferred. NET Core 1.0 initial project, but it is not available yet ). The former is lightweight and the latter is all lightweight, but in my opinion, the former is too lightweight. If I don't remember the parameters, it is useless if I don't click them, the latter needs to customize a processing method to execute related tasks, but the related types will never be redundant for me, I am not happy to use it. Therefore, you can develop an SDK library suitable for your own use by combining the two, which is open-source after the end of the project.

This Demo library has been released to NuGet (https://www.nuget.org/packages/OYMLCN.WeChat ),

If you are interested, you can use PM> Install-Package OYMLCN. WeChat to Install and use

Or add the reference version 0.0.1 of OYMLCN. WeChat to the project dependency.

Attached sample code for using this package

1 using Microsoft. aspNetCore. builder; 2 using Microsoft. aspNetCore. hosting; 3 using Microsoft. extensions. dependencyInjection; 4 using Microsoft. extensions. logging; 5 // The namespace of this library is WeChat, rather than the OYMLCN. weChat 6 using WeChat; 7 8 namespace WebApplication 9 {10 public class Startup 11 {12 // This method gets called by the runtime. use this method to add services to the container. 13 // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 14 public void ConfigureServices (IServiceCollection services) 15 {16} 17 18 // This method gets called by the runtime. use this method to configure the HTTP request pipeline. 19 public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 20 {21 22 loggerFactory. addConsole (); 23 24 if (env. isDevelopment () 25 {26 app. useDeveloperExceptionPage (); 27} 28 29 app. run (async (context) => 30 {31 var config = new Config () {AppId = "", AppSecret = "", EncodingAESKey = "", Token = ""}; 32 // This method has encapsulated the verification-related reply 33 // if it is a Get request, it will directly return 34 if (context. isGet (config) 35 return; 36 else if (context. isPost () 37 {38 // The string 39 string rsp = string returned to the background. empty; 40 // get the requested message entity by configuring 41 // This method encapsulates the message validity verification and type conversion 42 // If verification fails or conversion fails, null 43 WeChat is returned. entities. message msg = context. getMessage (config); 44 if (msg = null) return; 45 // further process 46 switch (msg. type) 47 {48 case WeChat. entities. messageType. text: 49 // can be converted to TextMessage 50 WeChat. entities. textMessage text = msg. asText (); 51 // get message content 52 // get the get attribute of only one Conent 53 // It is recommended to retrieve the text 54 string content = text directly through GetText. content; 55 // directly obtain the message content 56 Content = msg. getText (); 57 switch (content) 58 {59 case "Single Image": 60 rsp = msg. replyNews (new WeChat. entities. news () {description = "test", title = "test image", picurl = "image address", url = "link address"}); 61 break; 62 case "Multi-image": 63 rsp = msg. replyNews (64 new WeChat. entities. news () {}, 65 new WeChat. entities. news () {} 66); 67 break; 68 default: 69 rsp = msg. replyText ("You sent:" + msg. asText (). content); 70 break; 71} 72 break; 73 case WeChat. entities. messageType. event: 74 // convert to Event message 75 WeChat. entities. eventMessage evt = msg. asEvent (); 76 // currently, only the get attribute 77 switch (evt. event) 78 {79 case WeChat. entities. event. unsubscribe: 80 // TODO 81 return; 82 case WeChat. entities. event. subscribe: 83 rsp = msg. replyText ("follow successful"); 84 break; 85 case WeChat. entities. event. CLICK: 86 switch (evt. eventKey) 87 {88 default: 89 rsp = msg. replyText ("the returned information of this menu is not defined yet"); 90 break; 91} 92 break; 93 default: 94 rsp = msg. replyText ("not supported currently"); 95 break; 96} 97 break; 98 // It is only a Demo now. More functions will be improved in 99 case WeChat in the future. entities. messageType. noSupport: 100 default: 101 rsp = msg. replyText ("received but not accepted"); 102 break; 103} 104 // Finally, the encrypted message 105 await context is returned. finalResponse (config, rsp); 106} 107}); 108} 109} 110}

 

Attached is the Demo of Using Middleware during development.

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. threading. tasks; 5 using Microsoft. aspNetCore. builder; 6 using Microsoft. aspNetCore. http; 7 using WeChat; 8 using WeChat. entities; 9 10 namespace WebApplication. middleware11 {12 // You may need to install the Microsoft. aspNetCore. http. abstractions package into your project13 public class WeChatMiddleware14 {15 p Rivate readonly RequestDelegate _ next; 16 17 public WeChatMiddleware (RequestDelegate next) 18 {19 _ next = next; 20} 21 22 public async Task Invoke (HttpContext httpContext) 23 {24 if (httpContext. request. path. value. toLower (). endsWith ("/wxapi") 25 {26 # region Get27 if (httpContext. request. method = "GET") 28 {29 string echoString = httpContext. request. query ["echoStr"]; 30 // ignore 31 if (string. isNul LOrWhiteSpace (echoString) 32 await httpContext. response. writeAsync ("( ̄ ▽  ̄) What do you want"); 33 34 if (httpContext. checkSignature (Option. weChatConfig) 35 await httpContext. response. writeAsync (echoString); 36} 37 # endregion38 # region POST39 else if (httpContext. request. method = "POST") 40 {41 string rsp = string. empty; 42 var message = httpContext. getMessage (Option. weChatConfig); 43 if (message = null) return ;// Message Body Verification Failed or Conversion failed to terminate processing 44 switch (message. type) 45 {46 case MessageType. text: 47 switch (message. getText () 48 {49 case "Single Image": 50 rsp = message. replyNews (new News () {description = "test", title = "test image"}); 51 break; 52 case "Multi-image": 53 rsp = message. replyNews (54 new News () {description = "", title = ")", picurl = "url"}, 55 new News () {title = "Al", picurl = "url"} 56); 57 break; 58 default: 59 rsp = mes Sage. replyText ("You sent:" + message. getText (); 60 break; 61} 62 break; 63 case MessageType. event: 64 var evt = message. asEvent (); 65 switch (evt. event) 66 {67 case Event. unsubscribe: return; 68 case Event. subscribe: 69 rsp = message. replyText ("follow successful"); 70 break; 71} 72 break; 73 default: 74 rsp = message. replyText ("Al, are you crazy? "); 75 break; 76} 77 await httpContext. finalResponse (Option. weChatConfig, rsp); return; // The message is returned directly after processing ends. 78} 79 # endregion80 // No data is returned by default to avoid repeated request 81 return; 82} 83 await _ next (httpContext ); 84 85} 86} 87 88 // Extension method used to add the middleware to the HTTP request pipeline.89 public static class WeChatMiddlewareExtensions90 {91 public static IApplicationBuilder UseWeChatMiddleware (this IApplicationBuilder builder) 92 {93 return builder. useMiddleware <WeChatMiddleware> (); 94} 95} 96}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.