Dav*_*vid 6 .net c# asp.net wcf
我正在关注如何为WCF服务实现用户名身份验证的MSDN文章.
在步骤5中,attribute: [AspNetCompatibilityRequirements]在服务上设置a以为ASP.NET兼容模式配置WCF服务.这是必需的,因为身份验证由HTTP模块完成,您必须能够从HTTP上下文获取主体和身份,以便在WCF中以命令性或声明性方式授权用户.
当我运行该服务时,我收到此错误消息:
无法激活该服务,因为它不支持ASP.NET兼容性.为此应用程序启用了ASP.NET兼容性.在web.config中关闭ASP.NET兼容模式,或者将AspNetCompatibilityRequirements属性添加到服务类型,其RequirementsMode设置为"Allowed"或"Required".
看起来即使我明确声明了它被忽略的属性.这可能是什么原因?即使我将值更改为AspNetCompatibilityRequirementsMode.Allowed它也不起作用.同样的错误消息是因为IIS没有理由抱怨!
服务:
namespace MyNamespace.IISServiceHost
{
[ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CompanyNameAPIService
{
public CompanyNameAPIService()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
接口
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);
...
}
}
Run Code Online (Sandbox Code Playgroud)
我还在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>
Run Code Online (Sandbox Code Playgroud)
我终于让我的服务开始工作了。我发布此内容以防有人遇到此问题。有几个问题,我一一解答。
服务属性被忽略
我的 Visual Studio 解决方案包含服务项目和 ServiceHost 项目。Service 项目有一个app.config,ServiceHost 有一个web.config。
我不知道的是,尽管服务是在 web.config 中定义的,但并非所有设置都会得到遵守,因此您必须在 app.config 文件中进行配置。这让我很困惑,因为当服务发布到 IIS 时,app.config 文件消失了。我仍然不知道它是如何工作的,但确实如此。
服务名称属性
解决第一个问题后,服务成功发布,我可以使用浏览器导航到它。WSDL 配置也可用(很好)。但是当我尝试调用该服务的任何公开方法时,我得到了“ 404 not found”或“ There was no channel actively listening at 'xxx'”。
经过很长时间的尝试发现问题,我在跟踪日志中发现了这个错误:No matching <service> tag was found。
这很奇怪,因为我知道服务及其端点配置正确。
这里的问题是服务的名称。根据 StackOverflow 上的众多问题,名称的格式应为“ Complete.Namespace.Classname”。显然,如果服务托管在 IIS 中,则情况并非如此。那么服务的名称应该是在Service文件 service.svc 中的@ServiceHost 标记属性中找到的值
| 归档时间: |
|
| 查看次数: |
1578 次 |
| 最近记录: |