我正在使用带有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.1500内部服务器错误。
如果我删除了ValidationBehavior和NotNullValidator属性,那么一切都会正常工作。我检查了服务跟踪,没有看到任何可以帮助我的东西。
发布于 2011-09-26 20:36:57
答案是,这确实是可能的!我知道问题出在哪里了。我错过了对以下内容的引用:
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll问题是,在跟踪日志或调试模式中,我没有看到任何有关这方面的指示。我发现的唯一方法是,当我试图在操作实现本身中手动进行验证时,如下所示:
//at this point I got the exception saying that I'm missing the above references.
var validationResult = Validation.Validate<T>(TInstance); 希望它能帮助到某些人
https://stackoverflow.com/questions/7507930
复制相似问题