2011-09-21 24 views
1

我正在使用WCF REST服务和API密钥模板,并尝试使用验证应用程序块属性验证来实施验证。这里是我的服务:是否可以将验证应用程序块与WCF REST服务一起使用?

[ServiceContract] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] 
[ValidationBehavior] 
public class Service1 
{ 
    [FaultContract(typeof(ValidationFault))] 
    [WebGet(UriTemplate = "ValidateStuff?text={text}")] 
    public void ValidateStuff(
     [NotNullValidator] 
     string text) 
    { 
    } 

,并从模板在Global.asax:

public class Global : HttpApplication 
    { 
     void Application_Start(object sender, EventArgs e) 
     { 
      RegisterRoutes(); 
     } 

     private void RegisterRoutes() 
     { 
      // Edit the base address of Service1 by replacing the "Service1" string below 
      RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1))); 
     } 
    } 

然后我有一个客户端发送一个GET请求:

HttpWebRequest invokeRequest = WebRequest.Create(String.Concat(baseUrl, "/", uri, queryString)) as HttpWebRequest; 
invokeRequest.Method = Enum.GetName(typeof(Method), method); 
WebResponse response = invokeRequest.GetResponse()) 

现在的问题是,我每次都得到HTTP/1.1 500内部服务器错误。

如果我删除[ValidationBehavior] [FaultContract(typeof(ValidationFault))]和[NotNullValidator]属性,那么一切正常。我检查了服务跟踪,没有看到任何可以帮助我的东西。

回答

0

答案是,这的确是可能的! 我发现了什么问题。我缺少引用:

Microsoft.Practices.ServiceLocation.dll 
Microsoft.Practices.Unity.dll 
Microsoft.Practices.Unity.Interception.dll 

的strage的事情是,我没有看到在跟踪日志中也没有调试模式的任何指示。我发现的唯一方法是,当我试图做手工验证在操作实现本身,就像这样:

//at this point I got the exception saying that I'm missing the above references. 
var validationResult = Validation.Validate<T>(TInstance); 

希望它可以帮助别人

相关问题