wcf - 添加 WCF 服务引用,或使用 svcutil.exe 作为我自己的 WCF 服务

标签 wcf wcf-binding wcf-security wcf-client

我在添加我开发的 WCF 服务时遇到问题。 WCF 配置为使用证书。它曾经有效,但由于某些原因现在不起作用。以下是我尝试过的L

场景 1:

浏览时https://depart.MyDomain.com/ver.svc?wsdl在 Web 浏览器上,它显示 xml,但显示:

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe http://depart.MyDomain.com/ver.svc?wsdl 

.....
.....

场景 2:

运行下面的 svcutil.exe 时:

svcutil.exe https://depart.MyDomain.com/ver.svc?wsdl

它生成:

 Attempting to download metadata from 'https://depart.Mydomain.com/Ver.svc?wsdl' using WS-Metadata Exchange or DISCO.
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (R) Communication Foundation, Version 4.0.30319.1]
Copyright (c) Microsoft Corporation.  All rights reserved.

Error: Cannot obtain Metadata from https://depart.Mydomain.com/Ver.svc?wsdl

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 addr
ess.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.


WS-Metadata Exchange Error
    URI: https://depart.Mydomain.com/Ver.svc?wsdl

    Metadata contains a reference that cannot be resolved: 'https://depart.Mydomain.com/Ver.svc?wsdl'.

    <?xml version="1.0" encoding="utf-16"?><Fault xmlns="http://www.w3.org/2003/05/soap-envelope"><Code><Value>Sender</Value><Subcode><Value xmlns:a="http://doc
s.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:InvalidSecurity</Value></Subcode></Code><Reason><Text xml:lang="en-GB">An error occur
red when verifying security for the message.</Text></Reason></Fault>


HTTP GET Error
    URI: https://depart.Mydomain.com/Ver.svc?wsdl

    The document at the url https://depart.Mydomain.com/Ver.svc?wsdl was not recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'XML Schema' is 'The document format is not recognized (the content type is 'text/html; charset=UTF-8').'.
- Report from 'DISCO Document' is 'There was an error downloading 'https://depart.Mydomain.com/Ver.svc?disco'.'.
  - The request failed with HTTP status 403: Forbidden.
- Report from 'https://depart.Mydomain.com/Ver.svc?wsdl' is 'The document format is not recognized (the content type is 'text/html; ch
arset=UTF-8').'.
- Report from 'WSDL Document' is 'The document format is not recognized (the content type is 'text/html; charset=UTF-8').'.

场景 3:

当我尝试在 Visual studio 2010 中添加服务引用时,它在场景 2 中生成类似的错误

下面是 web.config:

   <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" />
    </diagnostics>
    <services>
      <service behaviorConfiguration="VServiceBehaviour"
               name="Ver.Service">
        <endpoint address="ver" listenUri="" binding="wsHttpBinding" bindingConfiguration="wshttpbindingcfg"
                   contract="Ver.IVer" bindingNamespace="http://www.MyDomain.com/ver" behaviorConfiguration ="VerEndpointBehaviour">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="mexhttpbinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://depart.MyDomain.com/" />
          </baseAddresses>
        </host>
      </service>    
    </services>
    <bindings>
      <mexHttpBinding>
        <binding name="mexhttpbinding" />
      </mexHttpBinding>
      <wsHttpBinding>

        <binding name="wshttpbindingcfg" maxReceivedMessageSize="2000000000" sendTimeout="00:10:00">
          <readerQuotas maxStringContentLength="2000000000"/>

          <security mode="TransportWithMessageCredential">
            <message clientCredentialType="Certificate" establishSecurityContext="False" negotiateServiceCredential="False" algorithmSuite="Default"/>
          </security>

        </binding>
      </wsHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="VerEndpointBehaviour">
          <instanceContextBehavior/>
          <verInspectorBehavior/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="VServiceBehaviour">
          <dataContractSerializer maxItemsInObjectGraph="100000000"/>
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />

          <serviceCredentials>
            <clientCertificate>
              <authentication certificateValidationMode="PeerOrChainTrust" trustedStoreLocation="LocalMachine" mapClientCertificateToWindowsAccount="false"/>
            </clientCertificate>

            <serviceCertificate
               x509FindType="FindByThumbprint"
               findValue="xxx"
               storeLocation="LocalMachine"
               storeName="My"/>
          </serviceCredentials>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

感谢您提前提供任何建议!

最佳答案

对于安全通信 channel ,请使用 mexHttpsBinding(带有附加 s)而不是 mexHttpBinding,请参阅:http://msdn.microsoft.com/en-us/library/aa967391.aspx并替换

<serviceMetadata httpGetEnabled="true" /> 

<serviceMetadata httpsGetEnabled="true" /> 

还有附加的 s。

关于wcf - 添加 WCF 服务引用,或使用 svcutil.exe 作为我自己的 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056451/

相关文章:

.net - 将查询从客户端发送到 WCF 服务

c# - 如何在回调 channel 上抛出错误通知客户端?

WCF安全,用户名密码无证书

带有 CustomBinding 的 WCF 服务配置 HTTPS

c# - 使用证书签署文件

c# - WCF - 在 CMS 中使用 Web 控制的客户端安全

.net n 层身份和服务架构中的授权

.net - 客户端 app.config 中的 wsHttpBinding 更改为 basicHttpBinding

c# - WCF : How to detect a session is timeout?

wcf - "Guarding"来自某些用户的WCF服务-应用程序身份