gpt4 book ai didi

c# - 无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称

转载 作者:太空狗 更新时间:2023-10-29 23:20:11 24 4
gpt4 key购买 nike

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

enter image description here

蓝色波浪线错误是:

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

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

即使我忽略 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
..

最佳答案

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

这是我每次遇到 WCF 服务问题时都会遵循的 list :

服务器

确保您的服务契约(Contract)是使用具有适当属性的接口(interface)定义的,例如:

IMyService.cs

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

检查您是否使用正确的界面实现了契约(Contract):

MyService.cs

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

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

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

    <%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.MyService" CodeBehind="MyService.cs" %>

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

设置基本的 HTTP 绑定(bind):

  1. 删除 block <system.serviceModel>...</system.serviceModel>从你的文件 web.config 如果它已经存在。

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

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

Service Selection

  1. 指定服务契约(Contract)(应自动填写)。

  2. 在下一页中,为您的服务选择传输协议(protocol),在本例中为“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. 右键单击您的项目名称,然后在“添加服务引用”中输入您的服务地址并单击“开始”,如果一切正常,您应该会在服务窗口中看到您的服务:

Service Reference

  1. 尝试通过完成向导生成代理,重建您的项目并尝试。如果您仍然遇到同样的问题,请删除生成的引用并重复第 1 点和第 2 点,然后:

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

Advanced service settings

然后完成向导并编译。

希望现在一切正常!!!

关于c# - 无法解析/使用 System.ServiceModel.Security.WSTrustServiceContract 作为服务名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47943115/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com