使用XML WCF请求验证 [英] WCF request validation using XML

查看:155
本文介绍了使用XML WCF请求验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用的.Net 4.0框架 WCF SOAP的Web服务。我使用合同第一页办法 - 即服务code都使用WCSF蓝色工具手写WSDL生成

我有以下有关请求报文的要求。

  

如果价格低于100,也不得征税的元素,但如果它是大于100,税收元素是必需的。

在XPath的前pression将如下:

  // T:价格[。 < 100] [未(以下:: T:税)]或
// T:价格[。 > = 100] [如下:: T:税]
 

我可以在C#$ C $的服务了C处理。不过,我想使用的任何XML技术的WSDL本身定义规则吧。这将帮助客户知道什么是输入消息应该满足业务验证。 (这是业务规则的定义,它的实现是在WSDL本身;没有其他文件被共享到客户端)

什么是实现这一目标的WCF的最佳方法是什么?在这部分中的 WSDL 我可以定义 XML验证,这样它会通过 WCF ?

在下面给出的前两个引用,也提到的XPath 办法和C#处理程序(Web服务框架)。我们不能这样做的WCF?无法C#读取WSDL中的XPath?怎么做?任何引用?

注意:我还好有C#处理程序来执行;但业务逻辑验证应该在 XML

参考

  1. 扩展ASP.NET的WebMethod框架与业务规则验证-by亚伦Skonnard和丹·沙利文
  2. WS-Policy和WSE 2.0声明处理程序 - 亚伦Skonnard
  3. 哈特穆特的盒子 - 四个信条和XML消息与WCF
  4. 消息验证用模式在WCF
  5. XML验证使用的Schematron / XSD在C#
  6. <一个href="http://stackoverflow.com/questions/2593325/defining-xml-in-an-xsd-where-an-attribute-determines-the-possible-contents">Defining XML在XSD,其中一个属性决定的可能内容
解决方案

开始与规则的纯文本描述,别人叫你服务。给每个规则中的标记,以便它可以很容易地提及。

写您的验证规则在C#中,并呼吁他们在消息验证处理程序。你仍然可以使用XQuery / XPath语法和配置文件来执行的规则,但它会是一个实现细节。如果某些规则变成是繁琐的定义,在这个层面上,你可以添加那些在code。如果企业引入规则引擎,可以使引擎的使用。但这种情况的服务的接口的后面。如果规则的改变,在WSDL保持相同。

有验证给失败加上描述失败清脆的消息规则的标签。能给人以您的服务访问整合到一个开发环境,在那里他们可以玩弄的合同。


在如何使用XQuery风格的验证:

的Schematron 允许您在XML中定义的规则。一个模式包含阶段,模式,规则和断言,但基本上你断言的人会是这样的:

 &LT;断言ID =无税,低价格的测试=价格&gt; = 100或没有(以下:: T:税)&GT;
    如果价格低于100,也不得征税元
&LT; /断言&GT;
 

Schematron中提供了一组XSLT的转换,首先改变你的Schematron模式与业务规则到另一个XSLT转换。这个生成的XSLT转换,然后将XML输入一组描述其有效性的验证信息。


但整个的一点是,有很多方法可以做到这一点,你可以配置的断言脚本语言,并使用脚本来验证反序列化的参数。

 如果(order.price&LT; 100安培;&安培; order.tax){
    失败(无税,低价格的
        如果价格低于100,也不得征税元素);
}
 

和你可以改变的实施,如果你发现了另外一个更适合你。它不会改变的wsdl也不该服务的行为。

I have a WCF SOAP web service using .Net 4.0 framework. I am using contract first approach – i.e, service code is generated from hand written WSDL using WCSF Blue tool.

I have following requirement about the request message.

If the price is less than 100, there must not be a tax element, but if it's greater then 100, the tax element is required.

In XPath the expression will be as follows

//t:price[. < 100][not(following::t:tax)] or 
//t:price[. >=100][following::t:tax]

I can handle it in C# code of the service. But I would like to define the rule it in the WSDL itself using any XML technology. This will help the client know what is the business validation that the input message should meet. (That is the definition of the business rule and its implementation is in the WSDL itself; not other documentation is to be shared to the client.)

What is the best way to achieve it in WCF? Under which section in the WSDL I can define the XML validations so that it will be processed by WCF?

In the first two references given below, there is an approach mentioned about XPath and C# handlers (for Web Services Framework). Can't we do so in WCF? Can't C# read the XPath from WSDL? How to do it? Any references?

Note: I am okay to have C# handlers to execute; but the business logic for validation should be in XML

Reference

  1. Extend the ASP.NET WebMethod Framework with Business Rules Validation -by Aaron Skonnard and Dan Sullivan
  2. WS-Policy and WSE 2.0 Assertion Handlers – by Aaron Skonnard
  3. Hartmut's Box - The Four Tenets and XML Messaging with WCF
  4. Message validation with Schema in WCF
  5. XML validation with Schematron/XSD in C#
  6. Defining xml in an xsd where an attribute determines the possible contents

解决方案

Start with a plain text description of the rules to people calling your service. Give each rule a tag so it can be referred to easily.

Write your validation rules in C# and call them in a message validation handler. You can still use XQuery / XPath syntax and configuration files to implement the rules, but it'll be an implementation detail. If some of the rules turn out to be cumbersome to define at this level, you can add those in code. If the business introduces a rules engine, you can make use of the engine. But this happens behind the interface of the service. If the rules change, the WSDL remains the same.

Have the validation give the tag of the rule that failed plus a crisp messages describing the failure. Give people integrating with your service access to a development environment where they can toy around with the contract.


On how to use XQuery style validation:

Schematron allows you to define rules in XML. A schema consists of phases, patterns, rules and assertions but basically one of your assertions would look like:

<assert id="NO-TAX-LOW-PRICE" test="price >= 100 or not(following::t:tax)">
    If the price is less than 100, there must not be a tax element
</assert>

Schematron provides a set of XSLT transforms that first transform your schematron schema with the business rules into another XSLT transform. This generated XSLT transform then transforms the XML input into a set of validation messages that describe its validity.


But the whole point is that there's many ways to do this, you could configure the assertions in a scripting language and use the script to validate the deserialized parameters.

if( order.price < 100 && order.tax ) {
    fail("NO-TAX-LOW-PRICE", 
        "If the price is less than 100, there must not be a tax element");
}

And you could change the implementation if you find out another one suits you better. It wouldn't change the wsdl nor the behavior of the service.

这篇关于使用XML WCF请求验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆