如何使用托管在 IIS 上并可通过 Web 使用的 wsHttpBinding 配置 WCF 服务?

How to configure WCF Service with wsHttpBinding hosted on IIS and consumable over Web?

我对 WCF 服务总体上没有经验(这是我第一次尝试创建、配置和使用 WCF 服务)。

  1. 我已经创建了我的 WCF 服务,并通过 WCF 测试客户端(在开发 PC "in" Visual Studio 上)对其进行了测试,并且一切正常。
  2. 我已经将它托管在我们服务器(不是开发 PC)上的 IIS 中,将其添加到 ASP.Net v4.0 应用程序池,使用 Windows Forms Application(来自开发 PC)对其进行了测试我已将服务引用添加到托管 WCF 服务,一切正常。
  3. 我在家用 PC 上尝试了相同的应用程序(和 application.config),但出现此错误:"The caller was not authenticated by the service"。

我试过 "Google it" 大多数人说错误是由不同域中的计算机引起的(这是预料之中的,因为我的家庭 PC 和工作服务器甚至不在同一个网络上),但是我假设 WCF 可以通过 Web 使用,还是我假设错了?

On Development PC, WCF Service address is redirected to Local IP of Server PC. I've also added REST/Web Service because I need to POST some data and it's consumable without any problems.

有什么我遗漏的吗?

Web.config:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>

  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>

  <system.serviceModel>

    <services>
        <service name="TestWCF.TestServis">
            <endpoint address="TestWCF" binding="wsHttpBinding" contract="TestWCF.ITestServis" />
        </service>

        <service name="TestWCF.TestPushingServis">
            <endpoint address="" binding="webHttpBinding" contract="TestWCF.ITestPushingServis" behaviorConfiguration="web" />
        </service>
    </services>

    <behaviors>

      <serviceBehaviors>
        <behavior>
          <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="true"/>
        </behavior>     
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="web">
            <webHttp />
        </behavior>
      </endpointBehaviors>

    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </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>

Application.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_ITestServis"
                         maxReceivedMessageSize="2147483647" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://testdomain.com:12345/TestServis.svc/TestWCF"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITestServis"
                contract="TestServisReference.ITestServis"
                name="WSHttpBinding_ITestServis">
                <identity>
                    <servicePrincipalName value="host/ServerName" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

对于 wsHttpBinding,您可能需要使用 ClientCredentials 属性 设置您的 windows 凭据(用户名、密码和 windows 域)。

yourClient.ClientCredentials.Windows.ClientCredential.UserName = "name"
yourClient.ClientCredentials.Windows.ClientCredential.Password = "password"
yourClient.ClientCredentials.Windows.ClientCredential.Domain = "domain"

此外 - 您可以尝试在绑定中将安全模式设置为 none:

<security mode="None" />

希望对您有所帮助,

帕维尔