通过代理服务器连接到 WCF 服务时出现奇怪的异常 [英] Strange exception when connecting to a WCF service via a proxy server

查看:30
本文介绍了通过代理服务器连接到 WCF 服务时出现奇怪的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The exception "This operation is not supported for a relative URI." occurs in the following situation:

我有一个 WCF 服务:

I have a WCF service:

[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 和 int?.

MyFault and MyDto are two very simple classes marked with [DataContract] attribute and each only having three [DataMember] of type string, int and int?.

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

This service is hosted in IIS 7.0 on a Win 2008 Server along with an ASP.NET application. I am using an SVC file MyService.svc which is located directly in the root of the web site. The service configuration in web.config is the following:

<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 服务"-欢迎页面.

This seems to work so far as I can enter the address http://www.domain.com/MyService.svc in a browser and get the "This is a Windows Communication Foundation Service"-Welcome page.

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

One of the clients consuming the service is a console application:

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) 会引发以下异常:

When I run this application at a production server at a customer site calling aChannel.MyOperation(1) throws the following exception:

相对 URI 不支持此操作.

当我在我的开发 PC 上使用完全相同的配置运行这个客户端应用程序时,除了我从绑定中删除了 proxyAddress="10.20.30.40:8080"> 操作没有问题.

When I run this client application on my development PC with exactly the same config, with the exception that I remove proxyAddress="10.20.30.40:8080" from the bindings the operation works without problems.

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

Now I really don't know what specifying a proxy server address might have to do with absolute or relative URIs. The use of the proxy server or not is the only difference I can see when running the client on the production or on the development machine.

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

Does someone have an idea what this exception might mean in this context and how possibly to solve the problem?

预先感谢您的帮助!

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

In case it should be important: Service and Client are built with WCF in .NET Framework 4.

推荐答案

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

10.20.30.40:8080 is not a valid URL. You want http://10.20.30.40:8080.

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

Here's my diagnostic thought process, in case it helps anyone:

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

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

There, in front of me, was a relative URI. When he removed it, the "operation" worked, so I deduced that this was the relative URI that was supplied to the "operation" that doesn't support them.

你不必完全是夏洛克福尔摩斯来解决这个问题.关键是能够立即看到 URI 前面没有 scheme://,使其具有相对性.

You don't exactly have to be Sherlock Holmes to solve this one. The key was being able to instantly see that there was no scheme:// in front of the URI, making it relative.

这篇关于通过代理服务器连接到 WCF 服务时出现奇怪的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆