【问题标题】:Can't resolve/use System.ServiceModel.Security.WSTrustServiceContract as service name无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称
【发布时间】:2018-06-05 04:51:34
【问题描述】:

我有一个使用 Microsoft.IdentityModel (WIF 3.5) 的令牌颁发者 WCF 服务,我需要升级到 System.IdentityModel (.NET 4.5)。问题是我无法将服务的原始名称Microsoft.IdentityModel.Protocols.WSTrust.WSTrustServiceContract 更改为更新的对应名称System.ServiceModel.Security.WSTrustServiceContract。由于某种原因,它无法被 IntelliSense 识别:

蓝色波浪线错误是:

The 'name' attribute is invalid - The value 'System.ServiceModel.Security.WSTrustServiceContract' is invalid according to its datatype 'serviceNameType'

我在<assemblies> 节点中确实有对System.ServiceModelSystem.IdentityModel 的程序集引用。

即使我忽略 IntelliSense 错误并运行服务并使用浏览器访问它,我也会收到以下元数据错误:

Metadata publishing for this service is currently disabled. 

元数据发布启用,所以我认为这是因为服务的名称问题。

我也从 VS.NET WCF 测试客户端收到此错误:

Error: Cannot obtain Metadata from http://localhost:49178/Services/Issuer.svc 
If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.
WS-Metadata Exchange Error    
URI: http://localhost:49178/Services/Issuer.svc    
Metadata contains a reference that cannot be resolved: 'http://localhost:49178/Services/Issuer.svc'.    
There was no endpoint listening at http://localhost:49178/Services/Issuer.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.    
The remote server returned an error: (404) Not Found.
HTTP GET Error    
URI: http://localhost:49178/Services/Issuer.svc    
The HTML document does not contain Web service discovery information.

我认为“Metadata contains a reference that cannot be resolved”这一行也指的是服务名称解析错误。

关于在这里做什么有什么想法吗?我会很感激任何帮助..

Issuer.svc:

<%@ ServiceHost Language="C#" Debug="true" Factory="Identity.Services.Wcf.Core.CustomSecurityTokenServiceContractFactory" Service="CustomSecurityTokenServiceConfiguration"  %>

工厂:

public class CustomSecurityTokenServiceContractFactory : WSTrustServiceHostFactory
..

服务:

public class CustomSecurityTokenServiceConfiguration : SecurityTokenServiceConfiguration
..

【问题讨论】:

  • 您是否遵循link的指南
  • @SamuelShyu:是的,我做到了。该文档仅提及命名空间更改,没有提及行为更改。我什至反编译了 Microsoft.Identity.* 和 System.Identity.* 类,它们是相同的。
  • 尝试我的答案第 4 点中的建议:单击“高级”并在生成代理时取消选中“在引用的程序集中重用类型”。
  • 有这样的参考吗?您是否检查过或自己添加了程序集?可能版本不支持?
  • @ercet00ilk :是的,参考资料在这里并且正在工作。端点合约使用 System.ServiceModel,例如 System.ServiceModel.Security.IWSTrust13SyncContract,它们可以正常解析/工作。

标签: c# .net wcf .net-4.5 wif


【解决方案1】:

有时解决此类问题的最佳方法是从头开始创建一个新的 WCF 项目,再次配置您的端点等。并从旧项目中复制现有服务,从旧项目迁移时尤其如此WCF 版本。

每次遇到 WCF 服务问题时,我都会遵循以下清单:

服务器

确保您的服务合同是使用具有适当属性的接口定义的,例如:

IMyService.cs

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    int ThisAnOperation(int a, int b);
}

检查您是否使用正确的接口实施了合同:

MyService.cs

public class MyService: IMyService
{
    public int ThisAnOperation(int a, int b)
    {
        return a * b;
    }
}

您需要有一个服务主机来访问您的服务,它们是扩展名为 .svc 的文件:

  • 创建文件 myService.svc。
  • 添加以下代码行,引用实现您的服务的类:

    &lt;%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.MyService" CodeBehind="MyService.cs" %&gt;

最后,您需要设置一个绑定,该绑定将定义哪些传输和协议可用于访问您的服务器,从一个简单的基本 HTTP 绑定开始以检查您的服务是否按预期工作,然后将其更改为更具生产性准备就绪,包括根据需要进行身份验证和/或加密和压缩。

设置基本 HTTP 绑定:

  1. 如果 &lt;system.serviceModel&gt;...&lt;/system.serviceModel&gt; 已经存在,请从您的文件 web.config 中删除它。

  2. 构建您的解决方案,它应该可以成功编译,否则请修复任何错误并重试。

  3. 右键单击您的 web.config 文件,然后单击“编辑 WCF 配置”,然后单击“创建新服务”并在服务类型中,浏览并选择编译服务时生成的 DLL 文件(应该在 bin 文件夹中)并选择您要发布的服务类:

  1. 指定服务合同(应自动填写)。

  2. 在下一页中为您的服务选择传输协议,在本例中为“HTTP”,然后选择“基本 Web 服务互操作性”。

  3. 在下一页您可以指定端点的地址,出于测试目的,您可以将此字段留空(确保您还从文本字段中删除“HTTP”)。

    李>
  4. 点击下一步,关闭配置窗口并保存。

现在您应该能够运行该服务并浏览到 MyService.svc 以访问您的服务。

  1. 激活元数据发布以便可以找到您的服务,为此,请将以下行为添加到您的 web.config:

    <system.serviceModel>
        <services>
            <service name="WcfService1.MyService">
                <endpoint binding="basicHttpBinding"
                       bindingConfiguration="" contract="WcfService1.IMyService" 
                    BehaviorConfiguration="MyServiceBehaviors" />
             </service>
         </services>
    </system.serviceModel>
    
    <behaviors>
         <serviceBehaviors>
             <behavior name="MyServiceBehaviors" >
                 <serviceMetadata httpGetEnabled="true" />
             </behavior>
         </serviceBehaviors>
    </behaviors>
    

现在您应该能够运行您的项目并在浏览器中获取服务的元数据描述页面,客户端可以使用此信息来查找服务并生成服务的代理:

客户

  1. 从您的项目中删除任何现有的服务引用。
  2. 右键单击您的项目名称,然后在“添加服务参考”中,输入您的服务地址并单击“开始”,如果一切顺利,您应该在服务窗口中看到您的服务:

  1. 尝试通过完成向导来生成代理,重新构建您的项目并尝试它。如果还是有同样的问题,删除生成的引用,重复第1点和第2点,然后:

  2. 点击“高级”并取消选中“在引用的程序集中重用类型”:

然后完成向导并编译。

希望现在一切正常!!!

【讨论】:

  • 感谢您的努力,但这并不真正适用于我的情况,因为此服务中没有 ServiceContract。 SVC 使用以 WSTrustServiceHostFactory 作为基类的类作为工厂,它使用 Microsoft.IdentityModel.Protocols.WSTrust.WSTrustServiceContract 作为服务名称,使用 IWSTrust13SyncContract(和其他变体)作为合同。这是我的错,我应该更清楚地了解这项服务。不过谢谢。
  • 没问题,您是否尝试过“在引用程序集中重用类型”设置?我用该设置解决了类似的错误。
【解决方案2】:

我的设置可能与您的类似。就我而言,我同时拥有 STS 和一个由想要令牌的人调用的服务。这就是你所拥有的,对吧?

在我拥有的实际 STS 的 Web.config 中:

<bindings>
  <ws2007HttpBinding>
    <binding name="ws2007HttpBindingConfiguration">
      <security mode="TransportWithMessageCredential">
        <message establishSecurityContext="false" clientCredentialType="Certificate"/>
      </security>
    </binding>
  </ws2007HttpBinding>
</bindings>
<services>
  <service name="System.ServiceModel.Security.WSTrustServiceContract" behaviorConfiguration="STSBehavior">
    <endpoint address="IWSTrust13" binding="ws2007HttpBinding" bindingConfiguration="ws2007HttpBindingConfiguration" contract="System.ServiceModel.Security.IWSTrust13SyncContract" name="STSWCF"/>
    <endpoint address="mex" binding="mexHttpsBinding" contract="IMetadataExchange"/>
  </service>
</services>

在我拥有的服务的 Web.config 中:

<protocolMapping>
  <!-- We want to use ws2007FederationHttpBinding over HTTPS -->
  <add scheme="https" binding="ws2007FederationHttpBinding" bindingConfiguration="ws2007FederationHttpBindingConfiguration"/>
</protocolMapping>
<bindings>
  <ws2007FederationHttpBinding>
    <binding name="ws2007FederationHttpBindingConfiguration">
      <!-- We expect a bearer token sent through an HTTPS channel -->
      <security mode="TransportWithMessageCredential">
        <message establishSecurityContext="false">
          <issuerMetadata address="https://localhost/Identity.STS.WCF/Service.svc/mex"/>
        </message>
      </security>
    </binding>
  </ws2007FederationHttpBinding>
</bindings>
<services>
  <service name="Identity.Auth.WCF.Service" behaviorConfiguration="STSBehavior">
    <endpoint address="https://localhost/Identity.Auth.WCF/Service.svc" binding="ws2007FederationHttpBinding" bindingConfiguration="ws2007FederationHttpBindingConfiguration" contract="Identity.Auth.WCF.IService" name="Identity.Auth.WCF"/>
  </service>
</services>

此外,它在这里对我有用,即使我确实遇到了与您相同的 IntelliSense 错误,并且在同一个位置。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2010-10-16
    • 2023-03-29
    • 1970-01-01
    • 2013-10-24
    • 2012-12-09
    相关资源
    最近更新 更多