首页 > 解决方案 > WCF 服务在 Postman 中工作,但在 VB.NET 网站中失败

问题描述

我创建了我的第一个 WCF 服务,它在 Postman 中的 https 中完美运行。

现在,我继续使用我的 VB.NET 网站中的服务,但我无法让它工作。

这是来自服务的 Web.config

 <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="inculdeExceptionDetails">
          <serviceDebug includeExceptionDetailInFaults="true " />
        </behavior>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="https">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http" />
      <add binding="webHttpBinding" scheme="https" bindingConfiguration="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

我在客户端上尝试了很多东西。但是我现在的点是,我可以初始化类,但是当我去调用一个函数时,我得到:

The provided URI scheme 'https' is invalid; expected 'http'.

这是我当前的 web.config:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="basicHttpBinding_ICustomZendeskWrapper">
          <security mode="None">
              <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>

    <client>
      <endpoint address="https://www.myDomain.info/Webservices/WCF/CustomZendeskWrapper.svc" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding_ICustomZendeskWrapper"   contract="CustomZendeskWrapper.ICustomZendeskWrapper" />
    </client>
  </system.serviceModel>

有人可以指出我正确的方向吗?

这是我更正的设置:

服务 Web.Config:

<system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />
    </diagnostics>
    <behaviors>
      <serviceBehaviors>
        <behavior name="inculdeExceptionDetails">
          <serviceDebug includeExceptionDetailInFaults="true " />
        </behavior>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="https">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <protocolMapping>
      <add binding="webHttpBinding" scheme="http" />
      <add binding="webHttpBinding" scheme="https" bindingConfiguration="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户端 Web.Config:

 <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding_ICustomZendeskWrapper">
          <security mode="Transport">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webhttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <client>
      <endpoint address="https://www.myDomain.info/Webservices/WCF/CustomZendeskWrapper.svc" binding="webHttpBinding" bindingConfiguration="webHttpBinding_ICustomZendeskWrapper" behaviorConfiguration="webhttp"   contract="CustomZendeskWrapper.ICustomZendeskWrapper" />
    </client>
  </system.serviceModel>

标签: vb.netwcfpostman

解决方案


首先,您的配置文件使用 WebHttpBinding 创建服务,因此您的服务以 REST 样式在 http 和 https 上正常工作。我们应该使用 WebHttpBinding 来调用服务或将 http 请求发送到正确的 URL,而不是使用 BasicHttpBinding。
在这种情况下,如果你想通过添加服务引用来调用服务,就像你的配置一样。我建议您进行以下更改。

  1. 在客户端的 webconfig 中使用 WebHttpBinding 而不是 BasicHttpBinding。
  2. 将适当的属性添加到自动生成的操作中。[WebGet]、[WebInvoke]
  3. 将 webhttp 端点行为添加到客户端端点。

这可以工作,但我不认为这是你想要的。如您所知,只有当服务器和客户端之间的 wcf 绑定类型一致时,我们才能成功调用服务。这种情况的另一个解决方案是我们使用 BasicHttpBinding 创建服务。它也适用于 http 和 http。
请参考我下面的配置。
服务器端。

<system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http"/>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

客户端

//use self-signed certificate
   ServicePointManager.ServerCertificateValidationCallback += delegate
            {
                return true;
            };
            ServiceReference2.Service1Client client = new ServiceReference2.Service1Client("BasicHttpsBinding_IService1");
            var result=client.GetData(234);
            Console.WriteLine(result);

配置文件。

  <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" />
                <binding name="BasicHttpsBinding_IService1">
                    <security mode="Transport" />
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://vabqia593vm:11024/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference2.IService1"
                name="BasicHttpBinding_IService1" />
            <endpoint address="https://localhost:11025/Service1.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpsBinding_IService1" contract="ServiceReference2.IService1"
                name="BasicHttpsBinding_IService1" />
        </client>
    </system.serviceModel>

另外,我们应该在 IIS 绑定模块中添加 http 绑定和 https 绑定。 在此处输入图像描述

如果有什么我可以帮忙的,请随时告诉我。


推荐阅读