在WPF布局框架中,如果标签不设置布局属性,系统默认为填充模式。当然,我们可以通过一系列标签的属性来达到布局标签的作用。 最常见的属性是Width和Height,是指定标签的宽和高。 MinWidth,MinHeight是指标签的最小宽度和最小高度,即使父容器的宽和高小于这个数字,标签也保持这个值。 MaxWidth,MaxHeight是指标签的最大宽度和最大高度。 最小和最大Width,Height将将会在一个固的区间来调整标签的大小。 HorizontalAlignment是标签在当前容器中左中右的停靠。VerticalAlignment是标签在当前容器中上中下的停靠。这两种停靠都是建立在父容器内的,只有父容器内大于标签是才能看到效果。 Margin,是一个独特的属性,它的值有三种情况,单值,表示该标签距父容器四周的距,两个值,第一个值为标签距父容器左右的距离,第二个值是标签距父器上下的距离,四个值,分别距父容器左,上,右,下的距离。如果存在Width和Height属性,它们的优先级要高于Margin。但一般情况了,两种属性的设置不会同时出现,Width和Height更强调固态的值,Margin强调随性的值,但它距边缘是固定的。 Padding和Margin相似,不同的是Padding是指标签内容与标签边界的距离。 Panel.ZIndex是一个层次的属性,这个值表示标签本身在父容器上位于那一层,值越大,越位于上层。可以用Panel.SetZIndex(标签名, 3);来设置标签元素的层次。 RenderTransofrm是一个缩放的属性,就是对当前标签的直接缩放,例子如下: <Button Content="123" ;50" Height="20" Click="Button_Click" > <Button.RenderTransform> <ScaleTransform. ScaleX="0.5" ScaleY="0.5"> </ScaleTransform> </Button.RenderTransform> </Button> C#代码: button1.RenderTransform. = new ScaleTransform(2, 2); LayoutTransform是一个旋转的属性 <Button Name="button1" Content="123" ;50" Height="20" > <Button.LayoutTransform> <RotateTransform. Angle="45"></RotateTransform> </Button.LayoutTransform> </Button> C#代码: button1.LayoutTransform. = new RotateTransform(90); 转换的类型还有MatrixTransform,如 <Button Name="button1" Content="123" ;50" Height="20" > <Button.LayoutTransform> <MatrixTransform. Matrix="3, 1, 1, 3, 0, 0"></MatrixTransform> </Button.LayoutTransform> </Button> C#代码: button1.LayoutTransform. = new MatrixTransform(3, 1, 1, 3, 0, 0); 上面的转换是用矩阵的方式实现,下面从x和y轴上来建立转换: <Button.LayoutTransform> <SkewTransform. AngleX="30" AngleY="30" ></SkewTransform> </Button.LayoutTransform> C#代码: button1.LayoutTransform. = new SkewTransform(10, 30, […]
View DetailsAsp.net的NamePipe机制给我们提供了很多扩展性. 使用HttpModule我们可能实现的有: 强制站点范围的Cookie策略 集中化监控与日志 编写设置与删除HTTP头 控制response输出,如删除多余空白字符 Session管理 认证与受权 下面我们来看如何实现自定义异常处理:
1 |
<span class="lnum"> 1: </span> <span class="kwrd">public</span> <span class="kwrd">class</span> ErrorModule:IHttpModule |
1 |
<span class="lnum"> 2: </span> { |
1 |
<span class="lnum"> 3: </span> <span class="preproc">#region</span> IHttpModule Members |
1 |
<span class="lnum"> 4: </span> |
1 |
<span class="lnum"> 5: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// Disposes of the resources (other than memory) used by the module that implements <see cref="T:System.Web.IHttpModule"/>.</span> |
1 |
<span class="lnum"> 7: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 8: </span> <span class="kwrd">public</span> <span class="kwrd">void</span> Dispose() |
1 |
<span class="lnum"> 9: </span> { |
1 |
<span class="lnum"> 10: </span> <span class="rem">//do nothing </span> |
1 |
<span class="lnum"> 11: </span> } |
1 |
<span class="lnum"> 12: </span> |
1 |
<span class="lnum"> 13: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 14: </span> <span class="rem">/// Initializes a module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 15: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 16: </span> <span class="rem">/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access to the methods, properties, and events common to all application objects within an ASP.NET application</param></span> |
1 |
<span class="lnum"> 17: </span> <span class="kwrd">public</span> <span class="kwrd">void</span> Init(HttpApplication context) |
1 |
<span class="lnum"> 18: </span> { |
1 |
<span class="lnum"> 19: </span> context.Error += <span class="kwrd">new</span> EventHandler(customcontext_Error); |
1 |
<span class="lnum"> 20: </span> } |
1 |
<span class="lnum"> 21: </span> |
1 |
<span class="lnum"> 22: </span> <span class="kwrd">private</span> <span class="kwrd">void</span> customcontext_Error(<span class="kwrd">object</span> sender, EventArgs e) |
1 |
<span class="lnum"> 23: </span> { |
1 |
<span class="lnum"> 24: </span> HttpContext ctx = HttpContext.Current; |
1 |
<span class="lnum"> 25: </span> HttpResponse response = ctx.Response; |
1 |
<span class="lnum"> 26: </span> HttpRequest request = ctx.Request; |
1 |
<span class="lnum"> 27: </span> |
1 |
<span class="lnum"> 28: </span> Exception exception = ctx.Server.GetLastError(); |
1 |
<span class="lnum"> 29: </span> |
1 |
<span class="lnum"> 30: </span> var sboutput = <span class="kwrd">new</span> StringBuilder(); |
1 |
<span class="lnum"> 31: </span> sboutput.Append(<span class="str">"Querystring:<p/>"</span>); |
1 |
<span class="lnum"> 32: </span> <span class="rem">//Get out the query string </span> |
1 |
<span class="lnum"> 33: </span> <span class="kwrd">int</span> count = request.QueryString.Count; |
1 |
<span class="lnum"> 34: </span> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i < count; i++) |
1 |
<span class="lnum"> 35: </span> { |
1 |
<span class="lnum"> 36: </span> sboutput.AppendFormat(<span class="str">"<br/> {0}:-- {1} "</span>, request.QueryString.Keys[i], request.QueryString[i]); |
1 |
<span class="lnum"> 37: </span> } |
1 |
<span class="lnum"> 38: </span> <span class="rem">//Get out the form collection info</span> |
1 |
<span class="lnum"> 39: </span> sboutput.Append(<span class="str">"<p>-------------------------<p/>Form:<p/>"</span>); |
1 |
<span class="lnum"> 40: </span> count = request.Form.Count; |
1 |
<span class="lnum"> 41: </span> <span class="kwrd">for</span> (<span class="kwrd">int</span> i = 0; i < count; i++) |
1 |
<span class="lnum"> 42: </span> { |
1 |
<span class="lnum"> 43: </span> sboutput.AppendFormat(<span class="str">"<br/> {0}:-- {1} -- <br/>"</span>, request.Form.Keys[i], request.Form[i]); |
1 |
<span class="lnum"> 44: </span> } |
1 |
<span class="lnum"> 45: </span> sboutput.Append(<span class="str">"<p>-------------------------<p/>ErrorInfo:<p/>"</span>); |
1 |
<span class="lnum"> 46: </span> sboutput.AppendFormat(<span class="str">@"Your request could not processed. Please press the back button on your browser and try again.<br/></span> |
1 |
<span class="lnum"> 47: </span> If the problem persists, please contact technical support<p/> |
1 |
<span class="lnum"> 48: </span> Information below is for technical support:<p/> |
1 |
<span class="lnum"> 49: </span> <p/>URL:{0}<p/>Stacktrace:---<br/>{1}<p/>InnerException:<br/>{2}" |
1 |
<span class="lnum"> 50: </span> , ctx.Request.Url, exception.InnerException.StackTrace, exception.InnerException); |
1 |
<span class="lnum"> 51: </span> |
1 |
<span class="lnum"> 52: </span> response.Write(sboutput.ToString()); |
1 |
<span class="lnum"> 53: </span> |
1 |
<span class="lnum"> 54: </span> <span class="rem">// To let the page finish running we clear the error</span> |
1 |
<span class="lnum"> 55: </span> ctx.Server.ClearError(); |
1 |
<span class="lnum"> 56: </span> } |
1 |
<span class="lnum"> 57: </span> |
1 |
<span class="lnum"> 58: </span> <span class="preproc">#endregion</span> |
1 |
<span class="lnum"> 59: </span> |
1 |
<span class="lnum"> 60: </span> } |
上面的代码实现了IHttpModule接口, 实现基于HttpApplication.Error事件, 接着我们自定义输出了一些信息,包括Form,QueryString. 最后把原来的Error信息清除了,这样你看到以前那个黄页了. 这个自定义的Module可以用于调试Web应用程序使用. Web.config中配置:
1 |
<span class="kwrd"><</span><span class="html">httpModules</span><span class="kwrd">></span> |
1 |
<span class="kwrd"><</span><span class="html">add</span> <span class="attr">name</span><span class="kwrd">="ErrorLoggingModule"</span> <span class="attr">type</span><span class="kwrd">="MyWeb.ErrorModule"</span><span class="kwrd">/></span> |
1 |
<span class="kwrd"></</span><span class="html">httpModules</span><span class="kwrd">></span> |
实际开发中,我们可以做的事儿很多,对这些信息记日志,发邮件. 如下, 我们演示使用Enterprise Library 做异常处理并日志记录,部分代码如下:
1 |
<span class="lnum"> 1: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 2: </span> <span class="rem">/// Handles the Error event of the EntLibLogging control.</span> |
1 |
<span class="lnum"> 3: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 4: </span> <span class="rem">/// <param name="sender">The source of the event.</param></span> |
1 |
<span class="lnum"> 5: </span> <span class="rem">/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param></span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// <remarks>author Petter Liu http://wintersun.cnblogs.com</remarks></span> |
1 |
<span class="lnum"> 7: </span> <span class="kwrd">private</span> <span class="kwrd">void</span> EntLibLogging_Error(<span class="kwrd">object</span> sender, EventArgs e) |
1 |
<span class="lnum"> 8: </span> { |
1 |
<span class="lnum"> 9: </span> var builder = <span class="kwrd">new</span> ConfigurationSourceBuilder(); |
1 |
<span class="lnum"> 10: </span> |
1 |
<span class="lnum"> 11: </span> builder.ConfigureInstrumentation() |
1 |
<span class="lnum"> 12: </span> .ForApplicationInstance(<span class="str">"MyApp"</span>) |
1 |
<span class="lnum"> 13: </span> .EnableLogging() |
1 |
<span class="lnum"> 14: </span> .EnablePerformanceCounters(); |
1 |
<span class="lnum"> 15: </span> |
1 |
<span class="lnum"> 16: </span> <span class="rem">//a single exception handling policy named MyPolicy for exceptions of type ArgumentNullException. </span> |
1 |
<span class="lnum"> 17: </span> <span class="rem">//The handler for this exception policy will log the exception to the General category (defined in the Logging Application Block configuration) </span> |
1 |
<span class="lnum"> 18: </span> <span class="rem">//as a warning with event ID 9000, wrap the ArgumentNullException with an InvalidOperationException, set the new exception message to MyMessage, and then re-throw the exception.</span> |
1 |
<span class="lnum"> 19: </span> builder.ConfigureExceptionHandling() |
1 |
<span class="lnum"> 20: </span> .GivenPolicyWithName(<span class="str">"MyPolicy"</span>) |
1 |
<span class="lnum"> 21: </span> .ForExceptionType<ArgumentNullException>() |
1 |
<span class="lnum"> 22: </span> .LogToCategory(<span class="str">"Exception"</span>) |
1 |
<span class="lnum"> 23: </span> .WithSeverity(System.Diagnostics.TraceEventType.Warning) |
1 |
<span class="lnum"> 24: </span> .UsingEventId(9000) |
1 |
<span class="lnum"> 25: </span> .WrapWith<InvalidOperationException>() |
1 |
<span class="lnum"> 26: </span> .UsingMessage(<span class="str">"MyMessage"</span>) |
1 |
<span class="lnum"> 27: </span> .ThenNotifyRethrow(); |
1 |
<span class="lnum"> 28: </span> |
1 |
<span class="lnum"> 29: </span> <span class="rem">//logging application </span> |
1 |
<span class="lnum"> 30: </span> builder.ConfigureLogging() |
1 |
<span class="lnum"> 31: </span> .WithOptions |
1 |
<span class="lnum"> 32: </span> .DoNotRevertImpersonation() |
1 |
<span class="lnum"> 33: </span> .LogToCategoryNamed(<span class="str">"Exception"</span>) |
1 |
<span class="lnum"> 34: </span> .SendTo.FlatFile(<span class="str">"Exception Logging File"</span>) |
1 |
<span class="lnum"> 35: </span> .FormatWith(<span class="kwrd">new</span> FormatterBuilder() |
1 |
<span class="lnum"> 36: </span> .TextFormatterNamed(<span class="str">"Text Formatter"</span>) |
1 |
<span class="lnum"> 37: </span> . UsingTemplate(<span class="str">"Timestamp: {timestamp}{newline}Message: {message}{newline}Category: {category}{newline}"</span>)) |
1 |
<span class="lnum"> 38: </span> .ToFile(<span class="str">"d:\\logs\\ExceptionsLog.log"</span>) |
1 |
<span class="lnum"> 39: </span> .SendTo.RollingFile(<span class="str">"Rolling Log files"</span>) |
1 |
<span class="lnum"> 40: </span> .RollAfterSize(1024) |
1 |
<span class="lnum"> 41: </span> .ToFile(<span class="str">"d:\\logs\\Rollinglog.log"</span>) |
1 |
<span class="lnum"> 42: </span> .LogToCategoryNamed(<span class="str">"General"</span>) |
1 |
<span class="lnum"> 43: </span> .WithOptions.SetAsDefaultCategory() |
1 |
<span class="lnum"> 44: </span> .SendTo.SharedListenerNamed(<span class="str">"Exception Logging File"</span>); |
1 |
<span class="lnum"> 45: </span> |
1 |
<span class="lnum"> 46: </span> var configSource = <span class="kwrd">new</span> DictionaryConfigurationSource(); |
1 |
<span class="lnum"> 47: </span> builder.UpdateConfigurationWithReplace(configSource); |
1 |
<span class="lnum"> 48: </span> EnterpriseLibraryContainer.Current = EnterpriseLibraryContainer.CreateDefaultContainer(configSource); |
1 |
<span class="lnum"> 49: </span> |
1 |
<span class="lnum"> 50: </span> var ex = HttpContext.Current.Server.GetLastError(); |
1 |
<span class="lnum"> 51: </span> var em = EnterpriseLibraryContainer.Current.GetInstance<ExceptionManager>(); |
1 |
<span class="lnum"> 52: </span> em.HandleException(ex.InnerException, <span class="str">"MyPolicy"</span>); |
1 |
<span class="lnum"> 53: </span> } |
注意上面的代码, 为了运行代码您需要引用以下程序集 Enterprise Library Share Common LibraryMicrosoft.Practices.ServiceLocationLogging Application BlockException Handling Application BlockException Handling Logging Provider 这里我们使用Fluent API配置, 因此没有配置XML文件. 所以不需要Web.Config中配置任何信息. 代码中有注释. 为了测试我们使用一个PAGE故意Throw 一个ArgumentNullException. 通过Server.GetLastError()获取Exception, 这时由名为MyPolicy策略处理异常. 对于ArgumentNullException把它们记录日志到名为Exception分类中,这个日志文件位于d:\\logs\\ExceptionLog.log.实际开发你完全按照你的需要来自定义策略. 然后这个日志文件中写出的内容是这样的:
1 |
----------------------------------------Timestamp: 2011-11-12 5:57:08Message: HandlingInstanceID: a99d005d-5f8d-4613-9522-2d60efb089aaAn exception of type <span class="str">'System.ArgumentNullException'</span> occurred and was caught.----------------------------------------------------------------------------11/12/2011 13:57:08Type : System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Message : Value cannot be <span class="kwrd">null</span>.Parameter name: Demo errorSource : MyWebHelp link : ParamName : Demo errorData : System.Collections.ListDictionaryInternalTargetSite : Void Page_Load(System.Object, System.EventArgs)Stack Trace : at MyWeb.About.Page_Load(Object sender, EventArgs e) <span class="kwrd">in</span> H:\My Project\DotNet40\TDD2010\WebHost\MyWeb\About.aspx.cs:line 14 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Additional Info:MachineName : USERTimeStamp : 2011-11-12 5:57:08FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35AppDomainName : 3e5cb21e-3-129655510216406250ThreadIdentity : WindowsIdentity : USER\PetterCategory: Exception |
由于我们日志模块我们配置还需要写Rollinglog.log文件,内容如下:
1 |
----------------------------------------Exception Warning: 9000 : Timestamp: 2011-11-12 5:57:08Message: HandlingInstanceID: a99d005d-5f8d-4613-9522-2d60efb089aaAn exception of type <span class="str">'System.ArgumentNullException'</span> occurred and was caught.----------------------------------------------------------------------------11/12/2011 13:57:08Type : System.ArgumentNullException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089Message : Value cannot be <span class="kwrd">null</span>.Parameter name: Demo errorSource : MyWebHelp link : ParamName : Demo errorData : System.Collections.ListDictionaryInternalTargetSite : Void Page_Load(System.Object, System.EventArgs)Stack Trace : at MyWeb.About.Page_Load(Object sender, EventArgs e) <span class="kwrd">in</span> H:\My Project\DotNet40\TDD2010\WebHost\MyWeb\About.aspx.cs:line 14 at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)Additional Info:MachineName : USERTimeStamp : 2011-11-12 5:57:08FullName : Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35AppDomainName : 3e5cb21e-3-129655510216406250ThreadIdentity : WindowsIdentity : USER\PetterCategory: ExceptionPriority: 0EventId: 9000Severity: WarningTitle:Enterprise Library Exception HandlingMachine: USERApp Domain: 3e5cb21e-3-129655510216406250ProcessId: 2444Process Name: C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.exeThread Name: Win32 ThreadId:2748Extended Properties: ---------------------------------------- |
1 |
你可以看到上面的EventId=9000与我们之前在CODE中配置是相同的. <br /> |
1 |
在开源社区有一个组件<a href="http://code.google.com/p/elmah/" target="_blank">ELMAH</a>(Error Logging Modules and Handlers for ASP.NET),已实现Error处理发邮件,写DB,发送到SNS等功能. 我们随意来看下它的代码:<br />它就是基于IHttpModule的扩展,下面代码来ELMAH: |
1 |
<span class="lnum"> 1: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 2: </span> <span class="rem">/// Provides an abstract base class for <see cref="IHttpModule"/> that</span> |
1 |
<span class="lnum"> 3: </span> <span class="rem">/// supports discovery from within partial trust environments.</span> |
1 |
<span class="lnum"> 4: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 5: </span> <span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> HttpModuleBase : IHttpModule |
1 |
<span class="lnum"> 6: </span> { |
1 |
<span class="lnum"> 7: </span> <span class="kwrd">void</span> IHttpModule.Init(HttpApplication context) |
1 |
<span class="lnum"> 8: </span> { |
1 |
<span class="lnum"> 9: </span> <span class="kwrd">if</span> (context == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 10: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"context"</span>); |
1 |
<span class="lnum"> 11: </span> |
1 |
<span class="lnum"> 12: </span> <span class="kwrd">if</span> (SupportDiscoverability) |
1 |
<span class="lnum"> 13: </span> HttpModuleRegistry.RegisterInPartialTrust(context, <span class="kwrd">this</span>); |
1 |
<span class="lnum"> 14: </span> |
1 |
<span class="lnum"> 15: </span> OnInit(context); |
1 |
<span class="lnum"> 16: </span> } |
1 |
<span class="lnum"> 17: </span> |
1 |
<span class="lnum"> 18: </span> <span class="kwrd">void</span> IHttpModule.Dispose() |
1 |
<span class="lnum"> 19: </span> { |
1 |
<span class="lnum"> 20: </span> OnDispose(); |
1 |
<span class="lnum"> 21: </span> } |
1 |
<span class="lnum"> 22: </span> |
1 |
<span class="lnum"> 23: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 24: </span> <span class="rem">/// Determines whether the module will be registered for discovery</span> |
1 |
<span class="lnum"> 25: </span> <span class="rem">/// in partial trust environments or not.</span> |
1 |
<span class="lnum"> 26: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 27: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">bool</span> SupportDiscoverability |
1 |
<span class="lnum"> 28: </span> { |
1 |
<span class="lnum"> 29: </span> get { <span class="kwrd">return</span> <span class="kwrd">false</span>; } |
1 |
<span class="lnum"> 30: </span> } |
1 |
<span class="lnum"> 31: </span> |
1 |
<span class="lnum"> 32: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 33: </span> <span class="rem">/// Initializes the module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 34: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 35: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnInit(HttpApplication application) {} |
1 |
<span class="lnum"> 36: </span> |
1 |
<span class="lnum"> 37: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 38: </span> <span class="rem">/// Disposes of the resources (other than memory) used by the module.</span> |
1 |
<span class="lnum"> 39: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 40: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnDispose() {} |
1 |
<span class="lnum"> 41: </span> } |
1 |
这是ErrorLogModule实现之前HttpModuleBase: |
1 |
<span class="lnum"> 1: </span><span class="kwrd">public</span> <span class="kwrd">class</span> ErrorLogModule : HttpModuleBase, IExceptionFiltering |
1 |
<span class="lnum"> 2: </span> { |
1 |
<span class="lnum"> 3: </span> <span class="kwrd">public</span> <span class="kwrd">event</span> ExceptionFilterEventHandler Filtering; |
1 |
<span class="lnum"> 4: </span> <span class="kwrd">public</span> <span class="kwrd">event</span> ErrorLoggedEventHandler Logged; |
1 |
<span class="lnum"> 5: </span> |
1 |
<span class="lnum"> 6: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 7: </span> <span class="rem">/// Initializes the module and prepares it to handle requests.</span> |
1 |
<span class="lnum"> 8: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 9: </span> |
1 |
<span class="lnum"> 10: </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnInit(HttpApplication application) |
1 |
<span class="lnum"> 11: </span> { |
1 |
<span class="lnum"> 12: </span> <span class="kwrd">if</span> (application == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 13: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"application"</span>); |
1 |
<span class="lnum"> 14: </span> |
1 |
<span class="lnum"> 15: </span> application.Error += <span class="kwrd">new</span> EventHandler(OnError); |
1 |
<span class="lnum"> 16: </span> ErrorSignal.Get(application).Raised += <span class="kwrd">new</span> ErrorSignalEventHandler(OnErrorSignaled); |
1 |
<span class="lnum"> 17: </span> } |
1 |
<span class="lnum"> 18: </span> |
1 |
<span class="lnum"> 19: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 20: </span> <span class="rem">/// Gets the <see cref="ErrorLog"/> instance to which the module</span> |
1 |
<span class="lnum"> 21: </span> <span class="rem">/// will log exceptions.</span> |
1 |
<span class="lnum"> 22: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 23: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> ErrorLog GetErrorLog(HttpContext context) |
1 |
<span class="lnum"> 24: </span> { |
1 |
<span class="lnum"> 25: </span> <span class="kwrd">return</span> ErrorLog.GetDefault(context); |
1 |
<span class="lnum"> 26: </span> } |
1 |
<span class="lnum"> 27: </span> |
1 |
<span class="lnum"> 28: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 29: </span> <span class="rem">/// The handler called when an unhandled exception bubbles up to </span> |
1 |
<span class="lnum"> 30: </span> <span class="rem">/// the module.</span> |
1 |
<span class="lnum"> 31: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 32: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnError(<span class="kwrd">object</span> sender, EventArgs args) |
1 |
<span class="lnum"> 33: </span> { |
1 |
<span class="lnum"> 34: </span> HttpApplication application = (HttpApplication) sender; |
1 |
<span class="lnum"> 35: </span> LogException(application.Server.GetLastError(), application.Context); |
1 |
<span class="lnum"> 36: </span> } |
1 |
<span class="lnum"> 37: </span> |
1 |
<span class="lnum"> 38: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 39: </span> <span class="rem">/// The handler called when an exception is explicitly signaled.</span> |
1 |
<span class="lnum"> 40: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 41: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnErrorSignaled(<span class="kwrd">object</span> sender, ErrorSignalEventArgs args) |
1 |
<span class="lnum"> 42: </span> { |
1 |
<span class="lnum"> 43: </span> LogException(args.Exception, args.Context); |
1 |
<span class="lnum"> 44: </span> } |
1 |
<span class="lnum"> 45: </span> |
1 |
<span class="lnum"> 46: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 47: </span> <span class="rem">/// Logs an exception and its context to the error log.</span> |
1 |
<span class="lnum"> 48: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 49: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> LogException(Exception e, HttpContext context) |
1 |
<span class="lnum"> 50: </span> { |
1 |
<span class="lnum"> 51: </span> <span class="kwrd">if</span> (e == <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 52: </span> <span class="kwrd">throw</span> <span class="kwrd">new</span> ArgumentNullException(<span class="str">"e"</span>); |
1 |
<span class="lnum"> 53: </span> |
1 |
<span class="lnum"> 54: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 55: </span> <span class="rem">// Fire an event to check if listeners want to filter out</span> |
1 |
<span class="lnum"> 56: </span> <span class="rem">// logging of the uncaught exception.</span> |
1 |
<span class="lnum"> 57: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 58: </span> |
1 |
<span class="lnum"> 59: </span> ExceptionFilterEventArgs args = <span class="kwrd">new</span> ExceptionFilterEventArgs(e, context); |
1 |
<span class="lnum"> 60: </span> OnFiltering(args); |
1 |
<span class="lnum"> 61: </span> |
1 |
<span class="lnum"> 62: </span> <span class="kwrd">if</span> (args.Dismissed) |
1 |
<span class="lnum"> 63: </span> <span class="kwrd">return</span>; |
1 |
<span class="lnum"> 64: </span> |
1 |
<span class="lnum"> 65: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 66: </span> <span class="rem">// Log away...</span> |
1 |
<span class="lnum"> 67: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 68: </span> |
1 |
<span class="lnum"> 69: </span> ErrorLogEntry entry = <span class="kwrd">null</span>; |
1 |
<span class="lnum"> 70: </span> |
1 |
<span class="lnum"> 71: </span> <span class="kwrd">try</span> |
1 |
<span class="lnum"> 72: </span> { |
1 |
<span class="lnum"> 73: </span> Error error = <span class="kwrd">new</span> Error(e, context); |
1 |
<span class="lnum"> 74: </span> ErrorLog log = GetErrorLog(context); |
1 |
<span class="lnum"> 75: </span> <span class="kwrd">string</span> id = log.Log(error); |
1 |
<span class="lnum"> 76: </span> entry = <span class="kwrd">new</span> ErrorLogEntry(log, id, error); |
1 |
<span class="lnum"> 77: </span> } |
1 |
<span class="lnum"> 78: </span> <span class="kwrd">catch</span> (Exception localException) |
1 |
<span class="lnum"> 79: </span> { |
1 |
<span class="lnum"> 80: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 81: </span> <span class="rem">// IMPORTANT! We swallow any exception raised during the </span> |
1 |
<span class="lnum"> 82: </span> <span class="rem">// logging and send them out to the trace . The idea </span> |
1 |
<span class="lnum"> 83: </span> <span class="rem">// here is that logging of exceptions by itself should not </span> |
1 |
<span class="lnum"> 84: </span> <span class="rem">// be critical to the overall operation of the application.</span> |
1 |
<span class="lnum"> 85: </span> <span class="rem">// The bad thing is that we catch ANY kind of exception, </span> |
1 |
<span class="lnum"> 86: </span> <span class="rem">// even system ones and potentially let them slip by.</span> |
1 |
<span class="lnum"> 87: </span> <span class="rem">//</span> |
1 |
<span class="lnum"> 88: </span> |
1 |
<span class="lnum"> 89: </span> Trace.WriteLine(localException); |
1 |
<span class="lnum"> 90: </span> } |
1 |
<span class="lnum"> 91: </span> |
1 |
<span class="lnum"> 92: </span> <span class="kwrd">if</span> (entry != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 93: </span> OnLogged(<span class="kwrd">new</span> ErrorLoggedEventArgs(entry)); |
1 |
<span class="lnum"> 94: </span> } |
1 |
<span class="lnum"> 95: </span> |
1 |
<span class="lnum"> 96: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 97: </span> <span class="rem">/// Raises the <see cref="Logged"/> event.</span> |
1 |
<span class="lnum"> 98: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 99: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnLogged(ErrorLoggedEventArgs args) |
1 |
<span class="lnum"> 100: </span> { |
1 |
<span class="lnum"> 101: </span> ErrorLoggedEventHandler handler = Logged; |
1 |
<span class="lnum"> 102: </span> |
1 |
<span class="lnum"> 103: </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 104: </span> handler(<span class="kwrd">this</span>, args); |
1 |
<span class="lnum"> 105: </span> } |
1 |
<span class="lnum"> 106: </span> |
1 |
<span class="lnum"> 107: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 108: </span> <span class="rem">/// Raises the <see cref="Filtering"/> event.</span> |
1 |
<span class="lnum"> 109: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 110: </span> <span class="kwrd">protected</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> OnFiltering(ExceptionFilterEventArgs args) |
1 |
<span class="lnum"> 111: </span> { |
1 |
<span class="lnum"> 112: </span> ExceptionFilterEventHandler handler = Filtering; |
1 |
<span class="lnum"> 113: </span> |
1 |
<span class="lnum"> 114: </span> <span class="kwrd">if</span> (handler != <span class="kwrd">null</span>) |
1 |
<span class="lnum"> 115: </span> handler(<span class="kwrd">this</span>, args); |
1 |
<span class="lnum"> 116: </span> } |
1 |
<span class="lnum"> 117: </span> |
1 |
<span class="lnum"> 118: </span> <span class="rem">/// <summary></span> |
1 |
<span class="lnum"> 119: </span> <span class="rem">/// Determines whether the module will be registered for discovery</span> |
1 |
<span class="lnum"> 120: </span> <span class="rem">/// in partial trust environments or not.</span> |
1 |
<span class="lnum"> 121: </span> <span class="rem">/// </summary></span> |
1 |
<span class="lnum"> 122: </span> <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">bool</span> SupportDiscoverability |
1 |
<span class="lnum"> 123: </span> { |
1 |
<span class="lnum"> 124: </span> get { <span class="kwrd">return</span> <span class="kwrd">true</span>; } |
1 |
<span class="lnum"> 125: </span> } |
1 |
<span class="lnum"> 126: </span> } |
1 |
更多的功能等待您去挖掘,我们在实际开发中按需选择.希望这篇POST对您开发有帮助.<br /> |
转自:http://www.cnblogs.com/wintersun/archive/2011/11/12/2246513.html
View Detailsusing System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls; using System.IO; public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { string Path = Server.MapPath("1.txt"); string strings = "1232222"; FileAdd(Path, strings); } public static void FileAdd(string Path, string strings) { //实现一个System.IO.StreamWrite,使其以一种特定的编码向流中写入数据,AppendText数据追加到文件末尾 //这句也可以写成StreamWriter sw = new StreamWriter(Path, true, Encoding.GetEncoding("GB2312")); StreamWriter sw = File.AppendText(Path); //开始写入 sw.WriteLine(strings); //清理当前编码器的缓冲区,并将所有缓存数据写入基础流 sw.Flush(); sw.Close(); } } 转自:http://blog.sina.com.cn/s/blog_812ce0500100ywjb.html
View DetailsGlobal.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法。你可以使用这个文件实现应用程序安全性以及其它一些任务。下面让我们详细看一下如何在应用程序开发工作中使用这个文件。 概述Global.asax 位 于应用程序根目录下。虽然 Visual Studio .NET 会自动插入这个文件到所有的 ASP.NET 项目中,但是它实际上是一个可选文件。 删除它不会出问题——当然是在你没有使用它的情况下。.asax 文件扩展名指出它是一个应用程序文件,而不是一个使 用 aspx 的 ASP.NET 文件。Global.asax 文件被配置为任何(通过 URL 的)直接 HTTP 请求都被自动 拒绝,所以用户不能下载或查看其内容。ASP.NET 页面框架能够自动识别出对Global.asax 文件所做的任何更改。 在 Global.asax 被更改后ASP.NET 页面框架会重新启动应用程序,包括关闭所有的浏览器会话,去除所有状态信息,并重新启动应用程序 域。 编程Global.asax 文件继承自HttpApplication 类,它维护一个HttpApplication 对象池,并在需要时将对象池中的对象分配给应用程序。Global.asax 文件包含以下事件:Application_Init:在应用程序被实例化或第一次被调用时,该事件被触发。对于所有的HttpApplication 对象实例,它都会被调用。Application_Disposed:在应用程序被销毁之前触发。这是清除以前所用资源的理想位置。Application_Error:当应用程序中遇到一个未处理的异常时,该事件被触发。Application_Start:在HttpApplication 类的第一个实例被创建时,该事件被触发。它允许你创建可以由所有HttpApplication 实例访问的对象。Application_End:在HttpApplication 类的最后一个实例被销毁时,该事件被触发。在一个应用程序的生命周期内它只被触发一次。Application_BeginRequest:在接收到一个应用程序请求时触发。对于一个请求来说,它是第一个被触发的事件,请求一般是用户输入的一个页面请求(URL)。Application_EndRequest:针对应用程序请求的最后一个事件。Application_PreRequestHandlerExecute:在 ASP.NET 页面框架开始执行诸如页面或 Web 服务之类的事件处理程序之前,该事件被触发。Application_PostRequestHandlerExecute:在 ASP.NET 页面框架结束执行一个事件处理程序时,该事件被触发。Applcation_PreSendRequestHeaders:在 ASP.NET 页面框架发送 HTTP 头给请求客户(浏览器)时,该事件被触发。Application_PreSendContent:在 ASP.NET 页面框架发送内容给请求客户(浏览器)时,该事件被触发。Application_AcquireRequestState:在 ASP.NET 页面框架得到与当前请求相关的当前状态(Session 状态)时,该事件被触发。Application_ReleaseRequestState:在 ASP.NET 页面框架执行完所有的事件处理程序时,该事件被触发。这将导致所有的状态模块保存它们当前的状态数据。Application_ResolveRequestCache:在 ASP.NET 页面框架完成一个授权请求时,该事件被触发。它允许缓存模块从缓存中为请求提供服务,从而绕过事件处理程序的执行。Application_UpdateRequestCache:在 ASP.NET 页面框架完成事件处理程序的执行时,该事件被触发,从而使缓存模块存储响应数据,以供响应后续的请求时使用。Application_AuthenticateRequest:在安全模块建立起当前用户的有效的身份时,该事件被触发。在这个时候,用户的凭据将会被验证。Application_AuthorizeRequest:当安全模块确认一个用户可以访问资源之后,该事件被触发。Session_Start:在一个新用户访问应用程序 Web 站点时,该事件被触发。 Session_End:在一个用户的会话超时、结束或他们离开应用程序 Web 站点时,该事件被触发。Application_Error 事件显示一个简单的消息用以说明发生的错误。 这个事件列表看起来好像多得吓人,但是在不同环境下这些事件可能会非常有用。使用这些事件的一个关键问题是知道它们被触发的顺序。Application_Init 和Application_Start 事件在应用程序第一次启动 时被触发一次。相似地,Application_Disposed 和 Application_End 事件在应用程序终止时被触发一次。此外,基于会 话的事件(Session_Start 和 Session_End)只在用户进入和离开站点时被使用。其余的事件则处理应用程序请求,这些事件被触发的 顺序是:Application_BeginRequestApplication_AuthenticateRequestApplication_AuthorizeRequestApplication_ResolveRequestCacheApplication_AcquireRequestStateApplication_PreRequestHandlerExecuteApplication_PreSendRequestHeadersApplication_PreSendRequestContent<<执行代码>>Application_PostRequestHandlerExecuteApplication_ReleaseRequestStateApplication_UpdateRequestCacheApplication_EndRequest 这些事件常被用于安全性方面。 转自:http://www.cnblogs.com/xiaopin/archive/2010/08/31/1813747.html
View Detailsweb.config是asp.net中保存配置信息(比如数据库连接字符串等)的重要文件。它是基于xml的文本文件方式放在Web应用程序的任何目录中,并且默认不随源文件编译到Dll中,而运行环境随时监视着它是否有改变,一但有变动,系统会自动重新加载里面的最新内容。 一、 配置文件保存位置 .net的默认配置文件保存在“windows目录\Microsoft.NET\Framework\对应.net版本\config”文件夹下面。不同的操作系统windows目录不一样,我们在命令行下输入“echo %windir%”查看windows目录所在的位置。 图:web.config所在的目录 Asp.net中有两个非常重要的配置文件,分别是machine.config和web.config,它们都位于config文件夹下面。这两个文件一般不需要我们手工是维护它,保持默认即可。但针对asp.net应用程序,它自身会有0个,1个或者多个web.config配置文件,多个配置文件会存在加载顺序问题。下节会介绍。 注意,传说中.net3.0和.net3.5只是在.net2.0的基础上扩充中,所以还是没用的.net2.0的配置文件。它们连config这个目录都没有。 二、 配置文件加载顺序 IIS在Asp.net网站启动时,会加载配置文件中的配置信息,然后缓存这些信息,不会每次要用都去读取配置文件,只是IIS会随时监视着这些文件的变化,一量有变化,它会重新去读取并缓存配置信息。 Asp.net网站运行时会按照以下方式加载配置文件中的节点信息: 1) 如果在当前运行页面所在的目录下有web.config文件,则查找是否存在所需要的节点,如果存在则返回结果,并停止下一步地查找。 2) 如里所在目录不存在web.config配置或者配置文件里没有所需要的节点,则查找它所在的上一级目录的配置文件中的节点,直到网站根目录。(问题:IIS6中的虚拟目录算不算根目录) 3) 如果网站根目录中都不存在web.config或者所需要的配置节点,转而到“windows目录\Microsoft.NET\Framework\对应.net版本\config\web.config”中去查找。 4) 如果第3条中还没找到,继续到“windows目录\Microsoft.NET\Framework\对应.net版本\config\machine.config”中去查找。 5) 如果还没找到,那就报错吧。 存在两个问题 1) IIS6中的虚拟目录算不算根目录。 2) 在系统运行时,在一个原本没有web.config的目录中手工加上一个web.config,会不会自动加载。 三、 配置文件节点介绍 Web.config文件是一个xml文本文件,它的根节点为<configuration>,该节点下包含常见的子节点有:<configSections>、<appSettings>、<connectionStrings>(保存数据库连接字符串)、<location>和<system.web>。下面针对各节点配置进行介绍。 1. <configSections> configSections 元素指定了配置节和处理程序声明。由于 ASP.NET 不对如何处理配置文件内的设置作任何假设,因此这非常必要。但 ASP.NET 会将配置数据的处理委托给配置节处理程序。配置结构信息如下: <configSections> <!--定义配置节处理程序与配置元素之间的关联。--> <section /> <!--定义配置节处理程序与配置节之间的关联。--> <sectionGroup /> <!--移除对继承的节和节组的引用。--> <remove /> <!--移除对继承的节和节组的所有引用,只允许由当前 section 和 sectionGroup 元素添加的节和节组。--> <clear/> </configSections> 每个 section 元素标识一个配置节或元素以及对该配置节或元素进行处理的关联 ConfigurationSection 派生类。可以在 sectionGroup 元素中对 section 元素进行逻辑分组,以对 section 元素进行组织并避免命名冲突。section 和 sectionGroup 元素包含在 configSections 元素中。 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素。 下面我们来示例写一个自定义配置信息,并完成它的SectionHandler,首先我们在<configuration>节点下添加configSections。 <configuration> <configSections> <sectionGroup name="mySectionGroup"> <section name="mySection" requirePermission="true" type="ConfigTest.SectionHandler.MySectionHandler,ConfigTest.SectionHandler" /> […]
View Details本文From hongshengpeng 解决ASP.NET 4.0 "A potentially dangerous Request.Form value was detected from the client". 错误在.net中,Request时出现有HTML或Javascript等字符串时,系统会认为是危险性值。立马报错。(在.aspx文件头中加入这句: <%@ Page validateRequest="false" %>,但还是出现相同错误) 或是修改web.config文件: <configuration> < system.web> <pages validateRequest="false" /> < /system.web> < /configuration> 但错误依旧。对.NET 4.0, 应该加上requestValidationMode="2.0" to the httpRuntime configuration section of the web.config :<httpRuntime requestValidationMode="2.0"/>(这句重要) 结构:<configuration> <system.web> <httpRuntime requestValidationMode="2.0">
View Details1、下载Eclipse3.7,登录http://www.eclipse.org/downloads/,下载Eclipse Classic 3.7: 2、安装ADT插件:下载好Eclipse后解压,运行Eclipse,第一次运行会提示设置workspace;在菜单栏选择Help—Install New Software,在Work with栏输入http://dl-ssl.google.com/android/eclipse/并回车,稍后显示如下图: 选择Select All按钮,单击Next——Next,选中“I Accept (此处略去若干字)”,再单击Finish,开始安装插件: 安装期间会提示“Warning(此处略去若干字)”,选择OK。安装完成后选择Restart,重启Eclispe。 此时选择Window,会出现如下选项: 并且Preference窗口中会出现Android选项,说明ADT插件安装成功。 3、安装Android SDK:登录http://developer.android.com/sdk/index.html,下载android-sdk_r12-windows.zip: 得到的是一个压缩包,解压到D盘根目录下。这里请注意,解压之后的SDK Manager.exe文件的绝对地址不要带有空格,否则后面启动模拟器的时候会有如下报错: invalid command-line parameter: Files\android-sdk-windows\tools/emulator-arm.exe.Hint: use '@foo' to launch a virtual device named 'foo'.please use -help for more information 这里文件的绝地地址是D:\android-sdk-windows\SDK Manager.exe。 4、运行D:\android-sdk-windows\SDK Manager.exe,会自动检查更新。我们下载的android-sdk_r12-windows.zip仅仅是冰山一角,我们还要下载安装整个冰山。 在上图中,你可以直接选择Install或者选择Accept All之后再选择Install。我是全选的,接下来的安装过程持续了几个小时,一定要淡定。 5、安装完毕后,关闭SDK Manager.exe。运行Eclipse,选择Window——Preference——Android,在SDK Location选择D:\android-sdk-windows,单击Apply后在Target Name中选择Android 2.3.3,单击OK,以关闭Preference对话框。 6、创建Android模拟器:在Eclipse中选择Window——Android SDK and AVD Manager,在对话框的左边选中Virtual decices,单击右部的New按钮(你可能需要把对话框拉宽才能看见这个按钮),仿照下图填写参数: 单击Create AVD按钮,完成模拟器的创建。 7、在Android SDK and AVD Manager窗口选中Android2.3.3,单击Start,弹出对话框以设定参数。为了使模拟器大小适中,可以仿照下图填写参数: 设定参数后,单击Launch,启动模拟器: 上图显示的模拟器我进行了一些设置,如时区选择、语言选择。 至此,环境搭建完毕,可以进行开发了。 转自:http://my.oschina.net/plumsoft/blog/29283
View DetailsRed5不能启动,删除这两个文件却可以,不理解: com.springsource.slf4j.juli-1.6.1.jarcom.springsource.slf4j.log4j-1.6.1.jar
View Details在IService1.cs定义服务契约 namespace WcfDemo{ // 注意: 如果更改此处的接口名称 "IService1",也必须更新 Web.config 中对 "IService1" 的引用。 [ServiceContract] // 服务合同 即提供服务的接口或类 public interface IService1 { [OperationContract] void AddTicket(int count); [OperationContract] int BuyTickets(int Num); [OperationContract] //服务契约 即提供服务的实现方法 int GetRemainingNum(); // 任务: 在此处添加服务操作 } // 使用下面示例中说明的数据约定将复合类型添加到服务操作。 [DataContract] //数据契约 public class Ticket { bool boolCount = true;//判断是否还有车票 int howmany = 10;//还有多少车票 [DataMember] public bool BoolCalue { get { return boolCount; } set { if (HowMany > 0) { boolCount = false; } else { boolCount = true; } } } [DataMember] public int HowMany { get { return howmany; } set { howmany = value; } } }} 在Service1.svc中实现契约服务
1 |
namespace WcfDemo { // 注意: 如果更改此处的类名“Service1”,也必须更新 Web.config 和关联的 .svc 文件中对“Service1”的引用。 public class Service1 : IService1 { Ticket T = new Ticket(); public void AddTicket(int count) { T.HowMany = T.HowMany + count; } public int GetRemainingNum() { return T.HowMany; } public int BuyTickets(int Num) { if (T.BoolCalue) { T.HowMany = T.HowMany - Num; return 1; } else { return 0; } } } } |
1 |
<wbr></wbr> |
1 |
<br />添加宿主程序用于监测服务<br />添加WinForm项目加入解决方案<br />界面如下图:<br /><img src="//www.qiandabao.com/upload/201008310021340361.jpg" real_src="//www.qiandabao.com/upload/201008310021340361.jpg" width="490" height="367" alt="WCF实例" title="WCF实例" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; border-image: initial; list-style-type: none; list-style-position: initial; list-style-image: initial; " /><br /><br />界面上两个按钮:<br />启动服务按钮: 用于启动wcf服务<br />停止服务按钮: 用于停止wcf服务<br />Label: <wbr></wbr> <wbr></wbr> 用于显示服务相关信息<br />后台代码为:<br />应用命名空间 <wbr></wbr> using System.ServiceModel;<br />添加引用 wcf服务生成的dll文件<br /> |
1 |
public partial class Form1 : Form { public Form1() { InitializeComponent(); } ServiceHost host = null;//定义 ServiceHost private void button1_Click(object sender, EventArgs e) { host = new ServiceHost(typeof(WcfDemo.Service1));//WcfDemo.Service1 为引用的dll中的服务 host.Open();//启动服务 this.label1.Text = "服务已启动"; } private void button2_Click(object sender, EventArgs e) { if (host.State != CommunicationState.Closed)//判断服务是否关闭 { host.Close();//关闭服务 } this.label1.Text = "服务已关闭"; } } |
1 |
<br /><br />接下来配置app.config<br /> |
1 |
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <services><!--添加服务--> <service name="WcfDemo.Service1" behaviorConfiguration="CalculatorServiceBehavio<wbr></wbr>r"> <!--name 必须与代码中的host实例初始化的服务一样 behaviorConfiguration 行为配置 --> <host> <baseAddresses> <!--添加调用服务地址--> <add baseAddress="http://localhost:8000/"/> </baseAddresses> </host> <!--添加契约接口 contract="WcfDemo.IService1" WcfDemo.IService1为契约接口 binding="wsHttpBinding" wsHttpBinding为通过Http调用--> <endpoint address="" binding="wsHttpBinding" contract="WcfDemo.IService1"></endpoint> </service> </services> <!--定义CalculatorServiceBehavio<wbr></wbr>r的行为--> <behaviors> <serviceBehaviors> <behavior name="CalculatorServiceBehavio<wbr></wbr>r"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailIn<wbr></wbr>Faults="false"/> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel> </configuration> |
1 |
<br /><br />在服务启动后可通过appConfig中baseAddress节点中的baseAddress地址查看Wcf服务<br /> |
1 |
<wbr></wbr> |
为项目添加服务引用地址输入服务主机appconfig中baseAddress地址点击前往(添加服务引用时一点是在服务启动状态下的)
1 |
public partial class Form2 : Form { public Form2() { InitializeComponent(); } ServiceReference1.Service1Client TClient = new WinFormsClient.ServiceReference1.Service1Client(); //声明客户端调用 private void button1_Click(object sender, EventArgs e) { int i = TClient.BuyTickets(2); //调用WCF中的方法 if (i == 1) { this.label1.Text = "购买成功"; } this.label1.Text += "剩余车票还有" + TClient.GetRemainingNum().ToString(); } private void button2_Click(object sender, EventArgs e) { this.label1.Text = ""; this.label1.Text = TClient.GetRemainingNum().ToString();//调用WCF中的方法 } } |
一、前言 Orion这个东西,小巧,但是功能却非常多,废话不讲,看它目前的支持就知道了: (1)Servlets/JSP (2)EJB (3)HTTP (注:说明不再需要Apache或者IIS等Web Server) 二、下载,安装下载地址为Orion Application Server官方站点:http://www.orionserver.com/,我下载的是Orion1.4版本。下载完,解压到一个目录,我这里用E:\Orion作为例子。三、配置 (1)根据Orion官方站点的安装说明,如果要支持JSP等技术的话,要将JDK的tools.jar复制到e:\orion目录下,tools.jar文件在你安装的JDK目录的lib子目录下。到现在,其实已经成功了缺省的配置了,如果你的80端口没有被占用,而且你也想用80作为端口的话,你到这里已经完成了,否则,请继续下面的配置。 (2)配置端口在E:\orion\config目录下有个default-web-site.xml文件,找到文件中下面行:将这行改为:注意,我这里使用的是8008端口。四、测试(1)启动Orion:在Orion目录下,command方式运行:java -jar orion.jar将出现Orion/x.x.x initialized,我这里版本是1.4所以显示:Orion/1.4.0 initialized到此,说明你已经成功了。(2)执行jsp文件在浏览器中输入:http://localhost:8008/出现:Orion Application Server 1.4.0 – Up and running,下面有JSP examples你可以试着运行。 十天学会JAVA之第二天Tomcat安装,JAVA/JSP学习系列之二(Tomcat安装) 一、前言 对于初学者来说,要执行JSP和Servlet,Tomcat是一个很不错的选择,。Tomcat是Apache组织的产品,sun公司对它的支持也相当不错。 二、下载,安装 下载地址为JAVA官方站点:jakarta.apache.org,国内也比较多。从这个地址可以下载:http://jakarta.apache.org/builds/tomcat/release/,我下载的是Tomcat32b4版本。 下载完,解压到一个目录,我这里用F:\tomcat32b4作为例子。三、配置 桌面上选择“我的电脑”(右键) 高级 环境变量 在“系统变量”—>“新建” 在变量名中输入:JAVA_HOME,变量值中输入:F:\JDK13然后确定; 在“系统变量”—>“新建” 在变量名中输入:TOMCAT_HOME,变量值中输入:F:\tomcat32b4然后确定;好了,配置完了,要重新启动计算机后,环境变量才能有效的。注意,对于Tomcat其他设置,如果没有必要,是可以不改的,我这里讲的都是最简单而可行的方法,如果对你的改动没有把握,还是建议不要改动。四、测试 (1)启动tomcat:在F:\tomcat32b4\bin下有一个:startup.bat,运行它,将出现一个Dos窗口。 (2)浏览 在浏览器中输入:http://localhost:8080/看看有什么效果(Tomcat默认端口为8080,在不冲突的前提下,你可以改动的),是不是可以看到一只可爱的小猫(其实,我个人认为,无论从颜色还是样子,更象一只老虎) (3)运行 在这个页面有JSP Examples和 Servlet Examples, 好了,自己测试运行它们吧。 (4)退出tomcat在F:\tomcat32b4\bin下有一个:shutdown.bat,运行它,将关闭Dos窗口(如果坚持直接关闭这个DOS窗口也是可以的,但是,既然有这个shutdown.bat,应该有它存在的理由)。
View Details