【发布时间】:2011-09-26 20:36:57
【问题描述】:
我正在使用带有 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 Internal Server Error。
如果我删除 [ValidationBehavior] [FaultContract(typeof(ValidationFault))] 和 [NotNullValidator] 属性,那么一切正常。我检查了服务跟踪,没有看到任何可以帮助我的东西。
【问题讨论】:
标签: wcf validation rest block