WCF配置差异.得到错误

本文关键字:错误 配置 WCF | 更新日期: 2023-09-27 17:49:15

这件事已经困扰我两天了。我已经创建了一个WCF服务,并在本地进行了测试,一切正常。我购买了一个托管网站,我已经上传了我的WCF到托管网站。

当我创建一个Windows窗体应用程序。我可以添加一个服务引用,它在配置文件中提供以下信息:

<system.serviceModel>
    <client>
        <endpoint address="http://www.verifythisaddress.com/AddressVerification/AddressVerificationService.svc/bh"
            binding="basicHttpBinding" bindingConfiguration="bh" contract="AddressVerification.IAddressVerificationService"
            name="bh" />
        <endpoint address="http://www.verifythisaddress.com/AddressVerification/AddressVerificationService.svc/ws"
            binding="wsHttpBinding" bindingConfiguration="ws" contract="AddressVerification.IAddressVerificationService"
            name="ws">
            <identity>
                <servicePrincipalName value="host/win5041.smarterasp.net" />
            </identity>
        </endpoint>
    </client>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_IECardBalanceService" />
            <binding name="bh" />
        </basicHttpBinding>
        <wsHttpBinding>
            <binding name="ws" />
        </wsHttpBinding>
    </bindings>
</system.serviceModel>

当我运行这个应用程序时,它一切正常。

现在当我把我的服务放到另一个类中。应用程序的根配置文件应该具有相同的绑定,对吗?所以我复制了绑定并将它们粘贴到根web中。配置文件,我得到错误信息,如"访问被拒绝"或"这是http不是https"等等。需要注意的是,我的机器上没有ssl证书。

所以当我调用这个WCF配置文件时:
**<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="DBCS"  connectionString="cs"    providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService">
          <security mode="Transport" />
        </binding>
        <binding name="soapEndpointGlobalAddressCheck">
          <security mode="Transport" />
        </binding>
        <binding name="soapSSLEndpointGlobalAddressCheck">
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
     <endpoint address="https://addresscheck.melissadata.net/v2/SOAP/Service.svc"
        binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
        contract="MelissaDataService.IService" name="BasicHttpBinding_IService" />
      <endpoint address="http://address.melissadata.net/v3/SOAP/GlobalAddress"
         binding="basicHttpBinding" bindingConfiguration="soapSSLEndpointGlobalAddressCheck"
        contract="globalcheck.AddressCheckSoap" name="soapEndpointGlobalAddressCheck" />
      <endpoint address="https://address.melissadata.net/v3/SOAP/GlobalAddress"
         binding="basicHttpBinding" bindingConfiguration="soapSSLEndpointGlobalAddressCheck"
         contract="globalcheck.AddressCheckSoap" name="soapSSLEndpointGlobalAddressCheck" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior>
           <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
           <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
       </serviceBehaviors>
     </behaviors>
    <protocolMapping>
       <add binding="basicHttpBinding" scheme="http"></add>
       <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="AddressVerificationWCF.AddressVerficationService">
        <endpoint address="bh" name="bh" binding="basicHttpBinding" contract="AddressVerificationWCF.IAddressVerificationService"></endpoint>
        <endpoint address="ws" name="ws" binding="wsHttpBinding" contract="AddressVerificationWCF.IAddressVerificationService"></endpoint>
       </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>**

现在,如果我从顶部部分获取配置,并将其放入我正在运行的其他应用程序的基本配置文件中,并且所有调用该服务的代码都与第一个工作的应用程序相同。I get and access is denied错误。

我像这样调用web服务:

AddressService.IAddressVerificationService client = new AddressService.AddressVerificationServiceClient("bh");

所以我想弄清楚,为什么它对一个有效,对另一个不起作用。如能指点,不胜感激。

我最终想这样做:

        EndpointAddress endpointAddress = new EndpointAddress("http://www.verifythisaddress.com/AddressVerification/AddressVerificationService.svc/bh");
        BasicHttpBinding serviceBinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
        serviceBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
        serviceBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
        AddressService.IAddressVerificationService client = new AddressService.AddressVerificationServiceClient(serviceBinding, endpointAddress);

WCF配置差异.得到错误

第一个serviceModel清单(可以工作的那个)没有完全集成到最后一个清单中—似乎只有绑定被复制了。所以bindingsendpoints应该完全匹配如果我没理解错的话。如果服务的位置发生了变化,那么只有address url应该不同。

而且,最后的清单缺少identity部分,这可能是导致"Access Denied"消息

的原因。
 <identity>
     <servicePrincipalName value="host/win5041.smarterasp.net" />
 </identity>

在你的绑定中:

<bindings>
  <basicHttpBinding>
    <binding name="BasicHttpBinding_IService">
      <security mode="None" />
    </binding>
    <binding name="soapEndpointGlobalAddressCheck">
      <security mode="None" />
    </binding>
    <binding name="soapSSLEndpointGlobalAddressCheck">
      <security mode="None" />
    </binding>
  </basicHttpBinding>
</bindings>

设置安全模式为none。还有一些端点如

Https://

由于您没有证书,您可能应该去掉这些,而只使用http://。

生成客户端后调用服务所需的代码如下:

    <system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="soapEndpointGlobalAddressCheck" />
            <binding name="soapSSLEndpointGlobalAddressCheck">
                <security mode="None" />
            </binding>
            <binding name="BasicHttpBinding_IService">
                <security mode="Transport" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://address.melissadata.net/v3/SOAP/GlobalAddress"
            binding="basicHttpBinding" bindingConfiguration="soapEndpointGlobalAddressCheck"
            contract="AddressTest.AddressCheckSoap" name="soapEndpointGlobalAddressCheck" />
        <endpoint address="https://addresscheck.melissadata.net/v2/SOAP/Service.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
            contract="AddressVersionTwo.IService" name="BasicHttpBinding_IService" />
    </client>
</system.serviceModel>