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]属性,那么一切正常。我检查了服务跟踪,没有看到任何可以帮助我的东西。