首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >可以在WCF REST服务中使用验证应用程序块吗?

可以在WCF REST服务中使用验证应用程序块吗?
EN

Stack Overflow用户
提问于 2011-09-22 07:31:09
回答 1查看 413关注 0票数 1

我正在使用带有API密钥模板的WCF REST服务,并尝试使用验证应用程序块属性验证来强制执行验证。这是我的服务:

代码语言:javascript
运行
复制
[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:

代码语言:javascript
运行
复制
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请求:

代码语言:javascript
运行
复制
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属性,那么一切都会正常工作。我检查了服务跟踪,没有看到任何可以帮助我的东西。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-26 20:36:57

答案是,这确实是可能的!我知道问题出在哪里了。我错过了对以下内容的引用:

代码语言:javascript
运行
复制
Microsoft.Practices.ServiceLocation.dll
Microsoft.Practices.Unity.dll
Microsoft.Practices.Unity.Interception.dll

问题是,在跟踪日志或调试模式中,我没有看到任何有关这方面的指示。我发现的唯一方法是,当我试图在操作实现本身中手动进行验证时,如下所示:

代码语言:javascript
运行
复制
//at this point I got the exception saying that I'm missing the above references.
var validationResult = Validation.Validate<T>(TInstance); 

希望它能帮助到某些人

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7507930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档