编程语言
首页 > 编程语言> > c# – 无法解析/使用System.ServiceModel.Security.WSTrustServiceContract作为服务名称

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

作者:互联网

我有一个令牌发行者WCF​​服务,它使用Microsoft.IdentityModel(WIF 3.5),我需要升级到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'

我在< assemblies>中有对System.ServiceModel和System.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.

我认为“元数据包含一个无法解析的引用”行也指服务名称解析错误.

关于该做什么的任何想法?我很感激任何帮助..

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服务问题时都会遵循的清单:

服务器

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

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.
>添加以下代码行,引用实现服务的类:

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

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

要设置基本的HTTP绑定:

>删除块< system.serviceModel> …< /system.serviceModel\u0026gt;从你的文件web.config,如果它已经存在.
>构建您的解决方案,它应该成功编译,否则修复任何错误,然后再试一次.
>右键单击您的web.config文件,然后单击“编辑WCF配置”,然后单击“创建新服务”并在服务类型中,浏览并选择编译服务时生成的DLL文件(应该在bin文件夹)并选择要发布的服务类:

Service Selection

>指定服务合同(应自动填写).
>在下一页中,选择服务的传输协议,在本例中为“HTTP”,然后选择“基本Web服务互操作性”.
>在下一页中,您可以指定端点的地址,出于测试目的,您可以将此字段留空(确保您还从文本字段中删除“HTTP”).
>单击“下一步”,关闭配置窗口并保存.

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

>激活元数据发布,以便找到您的服务,为此,将以下行为添加到您的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>

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

客户端

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

Service Reference

>尝试通过完成向导生成代理,重建项目并尝试.如果仍有相同的问题,请删除生成的引用并重复第1点和第2点,然后:
>单击“高级”并取消选中“在引用的程序集中重用类型”:

Advanced service settings

然后完成向导并编译.

希望现在一切都能正常工作!!!

标签:c,net,wcf,net-4-5,wif
来源: https://codeday.me/bug/20190522/1153362.html