Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Developer #1527

Merged
merged 19 commits into from
Dec 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ packages/
/Samples/Senparc.Weixin.MP.Sample.vs2017 - DPB
/Samples/Senparc.Weixin.MP.Sample/Senparc.Weixin.MP.Sample/App_Data/NeuChar/NeuCharRoot.bak.*
/Samples/Senparc.Weixin.MP.Sample.vs2017/Senparc.Weixin.MP.CoreSample/Properties/PublishProfiles/FolderProfile.pubxml
/Samples/Senparc.Weixin.MP.Sample.WebForms/Senparc.Weixin.MP.Sample.WebForms/App_Data/SenparcTraceLog/*

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%@ Application Codebehind="Global.asax.cs" Inherits="Senparc.Weixin.MP.Sample.WebForms.Global" Language="C#" %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@

using Senparc.CO2NET;
using Senparc.CO2NET.Cache;
using Senparc.CO2NET.Cache.Memcached;
using Senparc.CO2NET.RegisterServices;
using Senparc.Weixin.Entities;
using Senparc.Weixin.Exceptions;
using Senparc.Weixin.MP.Sample.CommonService;
//DPBMARK WebSocket
using Senparc.Weixin.MP.Sample.CommonService.MessageHandlers.WebSocket;//DPBMARK_END
//DPBMARK Open
using Senparc.Weixin.Open;
using Senparc.Weixin.Open.ComponentAPIs;//DPBMARK_END
//DPBMARK TenPay
using Senparc.Weixin.TenPay;//DPBMARK_END
//DPBMARK Work
using Senparc.Weixin.Work;//DPBMARK_END
//DPBMARK MiniProgram
using Senparc.Weixin.WxOpen;//DPBMARK_END
using System.IO;
using System.Web.Routing;

namespace Senparc.Weixin.MP.Sample.WebForms
{
// 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
// 请访问 http://go.microsoft.com/?LinkId=9394801

public class Global : System.Web.HttpApplication
{
protected void Application_Start()
{
//DPBMARK WebSocket
RegisterWebSocket();//微信注册WebSocket模块(按需,必须执行在RouteConfig.RegisterRoutes()之前)
//DPBMARK_END

/*
* CO2NET 全局注册开始
* 建议按照以下顺序进行注册
*/

/*
* CO2NET 是从 Senparc.Weixin 分离的底层公共基础模块,经过了长达 6 年的迭代优化。
* 关于 CO2NET 在所有项目中的通用设置可参考 CO2NET 的 Sample:
* https://github.com/Senparc/Senparc.CO2NET/blob/master/Sample/Senparc.CO2NET.Sample.netcore/Startup.cs
*/


//设置全局 Debug 状态
var isGLobalDebug = true;
//全局设置参数,将被储存到 Senparc.CO2NET.Config.SenparcSetting
var senparcSetting = SenparcSetting.BuildFromWebConfig(isGLobalDebug);
//也可以通过这种方法在程序任意位置设置全局 Debug 状态:
//Senparc.CO2NET.Config.IsDebug = isGLobalDebug;


//CO2NET 全局注册,必须!!
IRegisterService register = RegisterService.Start(senparcSetting).UseSenparcGlobal();

#region 全局缓存配置(按需)

#region 配置和使用 Redis -- DPBMARK Redis

//配置全局使用Redis缓存(按需,独立)
var redisConfigurationStr = senparcSetting.Cache_Redis_Configuration;
var useRedis = !string.IsNullOrEmpty(redisConfigurationStr) && redisConfigurationStr != "Redis配置";
if (useRedis)//这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略
{
/* 说明:
* 1、Redis 的连接字符串信息会从 Config.SenparcSetting.Cache_Redis_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略
/* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Redis 链接信息(仅修改配置,不立即启用)
*/
Senparc.CO2NET.Cache.Redis.Register.SetConfigurationOption(redisConfigurationStr);

//以下会立即将全局缓存设置为 Redis
Senparc.CO2NET.Cache.Redis.Register.UseKeyValueRedisNow();//键值对缓存策略(推荐)
//Senparc.CO2NET.Cache.Redis.Register.UseHashRedisNow();//HashSet储存格式的缓存策略

//也可以通过以下方式自定义当前需要启用的缓存策略
//CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisObjectCacheStrategy.Instance);//键值对
//CacheStrategyFactory.RegisterObjectCacheStrategy(() => RedisHashSetObjectCacheStrategy.Instance);//HashSet
}
//如果这里不进行Redis缓存启用,则目前还是默认使用内存缓存

#endregion // DPBMARK_END

#region 配置和使用 Memcached -- DPBMARK Memcached

//配置Memcached缓存(按需,独立)
var memcachedConfigurationStr = senparcSetting.Cache_Memcached_Configuration;
var useMemcached = !string.IsNullOrEmpty(memcachedConfigurationStr) && memcachedConfigurationStr != "Memcached配置";

if (useMemcached) //这里为了方便不同环境的开发者进行配置,做成了判断的方式,实际开发环境一般是确定的,这里的if条件可以忽略
{
/* 说明:
* 1、Memcached 的连接字符串信息会从 Config.SenparcSetting.Cache_Memcached_Configuration 自动获取并注册,如不需要修改,下方方法可以忽略
/* 2、如需手动修改,可以通过下方 SetConfigurationOption 方法手动设置 Memcached 链接信息(仅修改配置,不立即启用)
*/
Senparc.CO2NET.Cache.Memcached.Register.SetConfigurationOption(memcachedConfigurationStr);

//以下会立即将全局缓存设置为 Memcached
Senparc.CO2NET.Cache.Memcached.Register.UseMemcachedNow();

//也可以通过以下方式自定义当前需要启用的缓存策略
CacheStrategyFactory.RegisterObjectCacheStrategy(() => MemcachedObjectCacheStrategy.Instance);
}

#endregion // DPBMARK_END

#endregion

#region 注册日志(按需,建议)

register.RegisterTraceLog(ConfigWeixinTraceLog);//配置TraceLog

#endregion


/* 微信配置开始
* 建议按照以下顺序进行注册
*/

//设置微信 Debug 状态
var isWeixinDebug = true;
//全局设置参数,将被储存到 Senparc.Weixin.Config.SenparcWeixinSetting
var senparcWeixinSetting = SenparcWeixinSetting.BuildFromWebConfig(isWeixinDebug);
//也可以通过这种方法在程序任意位置设置微信的 Debug 状态:
//Senparc.Weixin.Config.IsDebug = isWeixinDebug;

//微信全局注册,必须!!
register.UseSenparcWeixin(senparcWeixinSetting, senparcSetting)


#region 注册公众号或小程序(按需)

//注册公众号(可注册多个) -- DPBMARK MP
.RegisterMpAccount(senparcWeixinSetting, "【盛派网络小助手】公众号")// DPBMARK_END


//注册多个公众号或小程序(可注册多个) -- DPBMARK MiniProgram
.RegisterWxOpenAccount(senparcWeixinSetting, "【盛派网络小助手】小程序")// DPBMARK_END

//除此以外,仍然可以在程序任意地方注册公众号或小程序:
//AccessTokenContainer.Register(appId, appSecret, name);//命名空间:Senparc.Weixin.MP.Containers
#endregion

#region 注册企业号(按需) -- DPBMARK Work

//注册企业微信(可注册多个)
.RegisterWorkAccount(senparcWeixinSetting, "【盛派网络】企业微信")

//除此以外,仍然可以在程序任意地方注册企业微信:
//AccessTokenContainer.Register(corpId, corpSecret, name);//命名空间:Senparc.Weixin.Work.Containers
#endregion // DPBMARK_END

#region 注册微信支付(按需) -- DPBMARK TenPay

//注册旧微信支付版本(V2)(可注册多个)
.RegisterTenpayOld(senparcWeixinSetting, "【盛派网络小助手】公众号")//这里的 name 和第一个 RegisterMpAccount() 中的一致,会被记录到同一个 SenparcWeixinSettingItem 对象中

//注册最新微信支付版本(V3)(可注册多个)
.RegisterTenpayV3(senparcWeixinSetting, "【盛派网络小助手】公众号")//记录到同一个 SenparcWeixinSettingItem 对象中

#endregion // DPBMARK_END

#region 注册微信第三方平台(按需) -- DPBMARK Open

//注册第三方平台(可注册多个)
.RegisterOpenComponent(senparcWeixinSetting,
//getComponentVerifyTicketFunc
componentAppId =>
{
var dir = Path.Combine(Server.MapPath("~/App_Data/OpenTicket"));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, string.Format("{0}.txt", componentAppId));
using (var fs = new FileStream(file, FileMode.Open))
{
using (var sr = new StreamReader(fs))
{
var ticket = sr.ReadToEnd();
return ticket;
}
}
},

//getAuthorizerRefreshTokenFunc
(componentAppId, auhtorizerId) =>
{
var dir = Path.Combine(Server.MapPath("~/App_Data/AuthorizerInfo/" + componentAppId));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId));
if (!File.Exists(file))
{
return null;
}

using (Stream fs = new FileStream(file, FileMode.Open))
{
var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
var result = (RefreshAuthorizerTokenResult)binFormat.Deserialize(fs);
return result.authorizer_refresh_token;
}
},

//authorizerTokenRefreshedFunc
(componentAppId, auhtorizerId, refreshResult) =>
{
var dir = Path.Combine(Server.MapPath("~/App_Data/AuthorizerInfo/" + componentAppId));
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}

var file = Path.Combine(dir, string.Format("{0}.bin", auhtorizerId));
using (Stream fs = new FileStream(file, FileMode.Create))
{
//这里存了整个对象,实际上只存RefreshToken也可以,有了RefreshToken就能刷新到最新的AccessToken
var binFormat = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binFormat.Serialize(fs, refreshResult);
fs.Flush();
}
}, "【盛派网络】开放平台")

//除此以外,仍然可以在程序任意地方注册开放平台:
//ComponentContainer.Register();//命名空间:Senparc.Weixin.Open.Containers
#endregion // DPBMARK_END

;

/* 微信配置结束 */
}

//DPBMARK WebSocket
/// <summary>
/// 注册WebSocket模块(可用于小程序或独立WebSocket应用)
/// </summary>
private void RegisterWebSocket()
{
Senparc.WebSocket.WebSocketConfig.RegisterRoutes(RouteTable.Routes);
Senparc.WebSocket.WebSocketConfig.RegisterMessageHandler<CustomWebSocketMessageHandler>();
}
//DPBMARK_END

/// <summary>
/// 配置微信跟踪日志
/// </summary>
private void ConfigWeixinTraceLog()
{
//Senparc.CO2NET.Config.IsDebug = false;

//这里设为Debug状态时,/App_Data/WeixinTraceLog/目录下会生成日志文件记录所有的API请求日志,正式发布版本建议关闭
Senparc.Weixin.WeixinTrace.SendCustomLog("系统日志", "系统启动");//只在Senparc.Weixin.Config.IsDebug = true的情况下生效

//自定义日志记录回调
Senparc.Weixin.WeixinTrace.OnLogFunc = () =>
{
//加入每次触发Log后需要执行的代码
};

//当发生基于WeixinException的异常时触发
Senparc.Weixin.WeixinTrace.OnWeixinExceptionFunc = ex =>
{
//加入每次触发WeixinExceptionLog后需要执行的代码

//发送模板消息给管理员
var eventService = new EventService();
eventService.ConfigOnWeixinExceptionFunc(ex);
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,51 @@
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Reference Include="BinaryFormatter, Version=2.1.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\BinaryFormatter.2.1.4\lib\netstandard1.1\BinaryFormatter.dll</HintPath>
</Reference>
<Reference Include="Senparc.CO2NET, Version=0.2.16.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.CO2NET.0.2.16\lib\net45\Senparc.CO2NET.dll</HintPath>
<Reference Include="Enyim.Caching, Version=2.11.0.0, Culture=neutral, PublicKeyToken=cec98615db04012e, processorArchitecture=MSIL">
<HintPath>..\packages\Membase.2.14\lib\net35\Enyim.Caching.dll</HintPath>
</Reference>
<Reference Include="Enyim.Caching.Web, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Enyim.Caching.Web.1.0.0.1\lib\net40\Enyim.Caching.Web.dll</HintPath>
</Reference>
<Reference Include="Membase, Version=2.14.0.0, Culture=neutral, PublicKeyToken=cec98615db04012e, processorArchitecture=MSIL">
<HintPath>..\packages\Membase.2.14\lib\net35\Membase.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="Senparc.CO2NET, Version=0.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.CO2NET.0.4.1\lib\net45\Senparc.CO2NET.dll</HintPath>
</Reference>
<Reference Include="Senparc.CO2NET.Cache.Memcached, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.CO2NET.Cache.Memcached.3.2.0\lib\net45\Senparc.CO2NET.Cache.Memcached.dll</HintPath>
</Reference>
<Reference Include="Senparc.CO2NET.Cache.Redis, Version=3.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.CO2NET.Cache.Redis.3.3.0\lib\net45\Senparc.CO2NET.Cache.Redis.dll</HintPath>
</Reference>
<Reference Include="Senparc.CO2NET.Cache.Redis.RedLock, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.CO2NET.Cache.Redis.RedLock.2.0.0\lib\net45\Senparc.CO2NET.Cache.Redis.RedLock.dll</HintPath>
</Reference>
<Reference Include="Senparc.NeuChar, Version=0.2.7.3, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Senparc.NeuChar.0.2.7.3\lib\net45\Senparc.NeuChar.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.Composition" />
<Reference Include="System.Data" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Numerics" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Web.ApplicationServices" />
Expand All @@ -72,6 +103,7 @@
</ItemGroup>
<ItemGroup>
<Content Include="Default.aspx" />
<Content Include="Global.asax" />
<Content Include="Work.aspx" />
<Content Include="Scripts\jquery-1.7.1.intellisense.js" />
<Content Include="Scripts\jquery-1.7.1.js" />
Expand All @@ -88,6 +120,9 @@
<Compile Include="Default.aspx.designer.cs">
<DependentUpon>Default.aspx</DependentUpon>
</Compile>
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Work.aspx.cs">
<DependentUpon>Work.aspx</DependentUpon>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@
</customErrors>
-->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID" />
</system.web>
<!--<sessionState customProvider="Memcached" mode="Custom">
<providers>
<add name="Memcached" type="Enyim.Caching.Web.MembaseSessionStateProvider, Enyim.Caching.Web" />
</providers>
</sessionState>--></system.web>
<!--
system.webServer 节是在 Internet Information Services 7.0 下运行 ASP.NET AJAX
所必需的。对于早期版本的 IIS,不必保留此节。
Expand All @@ -74,7 +78,7 @@
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-11.0.0.0" newVersion="11.0.0.0" />
<bindingRedirect oldVersion="0.0.0.0-12.0.0.0" newVersion="12.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Enyim.Caching" publicKeyToken="cec98615db04012e" culture="neutral" />
Expand Down
Loading