【问题标题】:Strange exception when connecting to a WCF service via a proxy server通过代理服务器连接到 WCF 服务时出现奇怪的异常
【发布时间】:2011-03-04 03:47:12
【问题描述】:

相对 URI 不支持此操作。”异常发生在以下情况:

我有 WCF 服务:

[ServiceContract(ProtectionLevel=ProtectionLevel.None)]
public interface IMyService
{
    [OperationContract]
    [FaultContract(typeof(MyFault))]
    List<MyDto> MyOperation(int param);

    // other operations
}

public class MyService : IMyService
{
    public List<MyDto> MyOperation(int param)
    {
        // Do the business stuff and return a list of MyDto
    }

    // other implementations
}

MyFaultMyDto 是两个非常简单的类,用[DataContract] 属性标记,每个类只有三个[DataMember] 类型为string, int and int?

此服务与 ASP.NET 应用程序一起托管在 Win 2008 服务器上的 IIS 7.0 中。我正在使用一个 SVC 文件MyService.svc,它直接位于网站的根目录中。 web.config中的服务配置如下:

<system.serviceModel>
  <services>
    <service name="MyServiceLib.MyService">
      <endpoint address="" binding="wsHttpBinding"
          bindingConfiguration="wsHttpBindingConfig" 
          contract="MyServiceLib.IMyService" />
    </service>
  </services>
  <bindings>
    <wsHttpBinding>
      <binding name="wsHttpBindingConfig">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>
    </wsHttpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="false"/>
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
</system.serviceModel>

这似乎有效,因为我可以在浏览器中输入地址 http://www.domain.com/MyService.svc 并获得“这是 Windows Communication Foundation 服务”-欢迎页面。

使用该服务的客户端之一是控制台应用程序:

MyServiceClient aChannel = new MyServiceClient("WSHttpBinding_IMyService");
List<MyDto> aMyDtoList = aChannel.MyOperation(1);

它具有以下配置:

<system.serviceModel>
  <bindings>
    <wsHttpBinding>
      <binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"
          openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          bypassProxyOnLocal="true" transactionFlow="false"
          hostNameComparisonMode="StrongWildcard"
          maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
          messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="false"
          proxyAddress="10.20.30.40:8080" allowCookies="false">
          <readerQuotas maxDepth="32" maxStringContentLength="8192"
              maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
              enabled="false" />
          <security mode="None">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="Windows"
                negotiateServiceCredential="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://www.domain.com/MyService.svc" binding="wsHttpBinding"
          bindingConfiguration="WSHttpBinding_IMyService"
          contract="MyService.IMyService"
          name="WSHttpBinding_IMyService" />
    </client>
</system.serviceModel>

当我在客户站点的生产服务器上运行此应用程序时,调用 aChannel.MyOperation(1) 会引发以下异常:

相对 URI 不支持此操作。

当我使用完全相同的配置在我的开发 PC 上运行此客户端应用程序时,除了我从绑定中删除了 proxyAddress="10.20.30.40:8080",该操作正常运行。

现在我真的不知道指定代理服务器地址可能与绝对或相对 URI 有什么关系。在生产或开发机器上运行客户端时,我能看到的唯一区别是是否使用代理服务器。

是否有人知道此异常在这种情况下可能意味着什么以及如何解决问题?

提前感谢您的帮助!

编辑:

如果它应该很重要:服务和客户端是使用 .NET Framework 4 中的 WCF 构建的。

【问题讨论】:

    标签: wcf wcf-client


    【解决方案1】:

    10.20.30.40:8080 不是有效的 URL。你想要http://10.20.30.40:8080


    这是我的诊断思维过程,以防对任何人有所帮助:

    1. 通常不会出现异常。他们通常的意思正是他们所说的。诀窍是了解他们怎么可能说真话。
    2. 异常表示“相对 URI 不支持此操作”。如果这是真的,那么这意味着正在执行一个操作,它被赋予了一个相对 URL,但它不支持相对 URI。
    3. 然后 OP 说,当他运行应用程序时,“除了我从绑定中删除 proxyAddress="10.20.30.40:8080" 之外”。

    在我面前的是一个相对 URI。当他删除它时,“操作”起作用了,所以我推断这是提供给不支持它们的“操作”的相对 URI。

    您不必完全是福尔摩斯来解决这个问题。关键是能够立即看到 URI 前面没有 scheme://,使其成为相对的。

    【讨论】:

    • 哇!一千页长的问题,一行答案,你就一针见血了。刚刚测试,工作,标记为答案。我羞愧地躲起来。无论如何:很好,我问了。非常感谢! (编辑:大声笑,“你可以在 2 分钟内接受答案”,你太快了。现在等待 2 分钟......)
    • Slauma,你问得很好! ))因为我陷入了非常相似的情况。在我的例子中,使用代理 URL 而不使用“http://”的原因是它是从另一个合法语法的地方(wget.exe 的参数)复制而来的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 2010-12-11
    • 1970-01-01
    相关资源
    最近更新 更多