C# 已忽略WCF服务属性

C# 已忽略WCF服务属性,c#,asp.net,.net,wcf,C#,Asp.net,.net,Wcf,下面我将介绍如何为WCF服务实现用户名身份验证 在步骤5中,在服务上设置属性:[AspNetCompatibilityRequirements],以将WCF服务配置为ASP.NET兼容模式。这是必需的,因为身份验证是由HTTP模块完成的,您必须能够从HTTP上下文中获取主体和标识,以便在WCF中以强制方式或声明方式授权用户 当我运行该服务时,会收到以下错误消息: namespace MyNamespace.IISServiceHost { [ServiceBehavior(MaxItem

下面我将介绍如何为WCF服务实现用户名身份验证

在步骤5中,在服务上设置
属性:[AspNetCompatibilityRequirements]
,以将WCF服务配置为ASP.NET兼容模式。这是必需的,因为身份验证是由HTTP模块完成的,您必须能够从HTTP上下文中获取主体和标识,以便在WCF中以强制方式或声明方式授权用户

当我运行该服务时,会收到以下错误消息:

namespace MyNamespace.IISServiceHost
{
    [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class CompanyNameAPIService
    {
        public CompanyNameAPIService()
        {
        }
    }
}
无法激活该服务,因为它不支持ASP.NET 兼容性。已为此应用程序启用ASP.NET兼容性。 在web.config中关闭ASP.NET兼容模式或添加 AspNetCompatibilityRequirements属性到具有 RequirementsMode设置为“允许”或“必需”

看起来即使我明确声明了属性,它还是被忽略了。这是什么原因呢?即使我将值更改为AspNetCompatibilityRequirementsMode.Allowed,它也不起作用。相同的错误消息,因为IIS没有理由抱怨

服务:

namespace MyNamespace.IISServiceHost
{
    [ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class CompanyNameAPIService
    {
        public CompanyNameAPIService()
        {
        }
    }
}
接口

namespace MyNamespace
{
    [ServiceContract]
    public interface ICompanyAPI
    {
        [OperationContract]
        [PrincipalPermission(SecurityAction.Demand, Role="WSIuser")]
        [ServiceKnownType(typeof(Supplier))]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void AddUpdateSuppliers(int companyId, Supplier[] sups);

        [OperationContract]
        [PrincipalPermission(SecurityAction.Demand, Role = "WSIuser")]
        [ServiceKnownType(typeof(Dimension))]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void AddUpdateDimension(int companyId, Dimension dims);

        ...
    }
}
我还在Web.config中设置了等效项

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="True" multipleSiteBindingsEnabled="true" />
    <service behaviorConfiguration="ServiceBehavior" name="MyNamespace.IISServiceHost.CompanyAPIService">
        <endpoint address="" behaviorConfiguration="largeDataBehavior" 
            binding="basicHttpBinding" bindingConfiguration="BasicBindingConfiguration" 
            name="BasicEndpoint" contract="MyNamespace.ICompanyAPI" />
        <endpoint address="help" behaviorConfiguration="helpPageBehavior" 
            binding="mexHttpsBinding" bindingConfiguration="" name="MexEndpoint" 
            contract="MyNamespace.ICompanyAPI" 
            kind="mexEndpoint" endpointConfiguration="" />
    </service>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicBindingConfiguration"><security mode="Transport" /></binding>
        </basicHttpBinding>
        <webHttpBinding>
            <binding name="webBinding">
                <security mode="Transport"><transport clientCredentialType="None" /></security>
            </binding>
    </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="largeDataBehavior">
                <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                <enableWebScript />
            </behavior>
            <behavior name="helpPageBehavior">
                <webHttp helpEnabled="true" />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <serviceMetadata httpsGetEnabled="true" httpsGetUrl="https://localhost/WSI/service.svc/wsdl" />
                <serviceDebug httpsHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
                <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="WSIRoleProvider">
                    <authorizationPolicies>
                        <add policyType="myNamespace.AuthorizationPolicy.HttpContextPrincipalPolicy, AuthorizationPolicy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                    </authorizationPolicies>
                </serviceAuthorization>
            </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

我终于让服务正常运行了。我张贴这一情况下,有人遇到这个问题。 有几个问题,所以我一一回答

忽略服务属性

我的Visual Studio解决方案包含服务项目和ServiceHost项目。服务项目有一个
app.config
,ServiceHost有一个
web.config

我不知道的是,即使该服务是在web.config中定义的,但并不是所有的设置都是有效的,因此您必须在app.config文件中进行配置。这让我很困惑,因为当服务发布到IIS时,app.config文件消失了。我仍然不知道它是如何工作的,但它是

服务名称属性

namespace MyNamespace
{
    [ServiceContract]
    public interface ICompanyAPI
    {
        [OperationContract]
        [PrincipalPermission(SecurityAction.Demand, Role="WSIuser")]
        [ServiceKnownType(typeof(Supplier))]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void AddUpdateSuppliers(int companyId, Supplier[] sups);

        [OperationContract]
        [PrincipalPermission(SecurityAction.Demand, Role = "WSIuser")]
        [ServiceKnownType(typeof(Dimension))]
        [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void AddUpdateDimension(int companyId, Dimension dims);

        ...
    }
}
在我解决了第一个问题后,服务成功发布,我可以使用浏览器导航到它。WSDL配置也可用(nice)。但是,当我试图调用服务的任何公开方法时,我得到了“
404未找到”
”或“
在“xxx”没有正在监听的频道”

在长时间尝试发现问题后,我在跟踪日志中发现此错误:
未找到匹配的标记

这很奇怪,因为我知道服务及其端点配置正确


这里的问题是服务的名称。根据StackOverflow上的许多问题,名称的格式应为“
Complete.Namespace.Classname
”。显然,如果服务托管在IIS中,则情况并非如此。那么服务的名称应该是在文件服务的@ServiceHost标记属性
service
中找到的值。svc

您的服务不实现任何接口,您是如何在web.config中配置绑定的?我认为您无论如何都需要实现接口:
公共类CompanyNameAPIService:ICompanyAPI
您的服务类是
myownamespace.IISServiceHost.CompanyNameAPIService
(基于您在文章中的信息)。但是,在config中,您将名称定义为
MyNamespace.CompanyAPI
。web.config中
元素的
name
属性必须与服务类的完全限定名匹配。我将名称更改为完全限定名,但仍然收到相同的错误。@Alberto,我正在使用
contract=“MyNamespace.ICompanyAPI”
在web.config中实现接口。你认为这就是原因吗?