简体   繁体   English

在WCF Web服务中使用异常处理应用程序块的简单示例

[英]Simple Example in using The Exception Handling Application Block in WCF Webservice

So i'm trying to use The Exception Handling Application Block from the Enterprise Library, but i just can't figure out how to do this. 因此,我尝试使用企业库中的“异常处理应用程序块”,但我只是不知道如何执行此操作。

This is my code from my WCF project: 这是我的WCF项目中的代码:

namespace TextWebService
{
    [ServiceContract]
    public interface ITextWebService
    {
        [OperationContract]
        string ToLower(string inputString);

        [OperationContract]
        string ToUpper(string inputString);
    }
}

... ...

using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;

namespace TextWebService
{
    public class TextWebService : ITextWebService
    {
        public string ToLower(string inputString)
        {
            return inputString.ToLower();
        }

        public string ToUpper(string inputString)
        {
            return inputString.ToUpper();
        }
    }
}

The ASP.NEt that uses this service has a textbox and "Invoke Service Methods" button which transforms the input string like so: 使用此服务的ASP.NEt具有一个文本框和“调用服务方法”按钮,该按钮将转换输入字符串,如下所示:

输入字符串

Everytime when I input large text, I get this error: 每次输入大文本时,都会出现此错误:

在此处输入图片说明

I just want a simple example how can I prevent that error from showing, or modify it and use the exception handling application bloc . 我只想要一个简单的示例,如何防止显示该错误或对其进行修改, 并使用异常处理应用程序bloc I tried a lot of examples, but just can't seem to get it. 我尝试了很多示例,但似乎无法理解。 PS it must be done from WCF PS必须从WCF完成

If you are certain that the content that comes trough your project is always valid. 如果您确定项目中的内容始终有效。 You can try to add the following to your web.config: 您可以尝试将以下内容添加到您的web.config中:

<system.web>
    <httpRuntime requestValidationMode="2.0" />
    <pages validateRequest="false" />
</system.web>

Here's a litle bit of back info about why you are getting this error (from this post): 这是一小部分关于为什么会出现此错误的后退信息(来自帖子):

..Note that a "<" could also come from other outside sources, like a database field, a configuration, a file, a feed and so on. ..请注意,“ <”也可能来自其他外部来源,例如数据库字段,配置,文件,提要等。

Furthermore, "<" is not inherently dangerous, its only dangerous in a specific context: when writing unencoded strings to HTML output (because of XSS). 此外,“ <”并不是天生的危险,它仅在特定情况下是危险的:将未编码的字符串写入HTML输出时(由于XSS)。 In other contexts different substrings are dangerous, eg if you write an user-provided URL into a link, the substring "javascript:" may be dangerous. 在其他情况下,不同的子字符串很危险,例如,如果您将用户提供的URL写入链接,则子字符串“ javascript:”可能很危险。 The single quote character on the other hand is dangerous when interpolating strings in SQL queries, but perfectly safe if it is a part of a name submitted from a form or read from a database field... 另一方面,单引号字符在SQL查询中插入字符串时很危险,但是如果它是从表单提交的名称或从数据库字段读取的名称的一部分,则绝对安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM