错误请求错误 400 - WCF 客户端 [英] Bad Request Error 400 - WCF Client

查看:38
本文介绍了错误请求错误 400 - WCF 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF Web 服务在我的 IIS 上运行.它从客户端获取数据并插入到数据库中.我的 Web 服务需要一个复杂数据类型播放器的对象.在客户端中,我创建了一个 Players 对象并将其传递给 Web 服务来完成工作.

I have a WCF Web Service Running on my IIS. It takes data from client and inserts in the Database. My Web Service requies an object of complex Data type Players. In the Client I create an object of Players and pass it to the Web Service to do the Work.

在我的客户端控制台应用程序中,我正在做

In my client console app i am doing

string strBaseAddress = ConfigurationSettings.AppSettings["WEBSERVICE"];
Uri baseAddress = new Uri(strBaseAddress);
WebServiceHost host = new WebServiceHost(typeof(TrialCenterAPI), baseAddress);
host.open
// Doing Work
host.close

此应用运行良好.

现在当我编写另一个客户端时,通过添加服务引用来做同样的工作.它每次都给我 400 Bad Request Error.

Now when I wrote another client by adding a service reference to do the same work. It gives me the 400 Bad Request Error every time.

我创建了自动生成的 API Client 对象,然后调用相应的 Web 服务方法并将 Players 对象传递给它.我尝试谷歌搜索,但没有得到任何解决方案.我增加了服务器和客户端 Web 配置中的最大大小.

I created object of the API Client which was generated automatically, then called the appropriate method of web service and passed the object of Players to it. I tried googling and i didn't get any solution., I increased the Max size in Server as well as Client Web Config.

客户端 Web Config 使用自定义绑定如下:

The client Web Config uses Custom Binding as follows:

<system.serviceModel>
      <bindings>
        <customBinding>
          <binding name="WebHttpBinding">
            <textMessageEncoding 
                 maxReadPoolSize="2147483647" 
                 maxWritePoolSize="2147483647"
                 messageVersion="None" writeEncoding="utf-8" >
              <readerQuotas maxDepth="2147483647" 
                     maxStringContentLength="2147483647" 
                     maxArrayLength="2147483647"
                     maxBytesPerRead="2147483647" 
                     maxNameTableCharCount="2147483647" />
            </textMessageEncoding>
            <httpTransport 
                 maxReceivedMessageSize="2147483647" 
                 maxBufferSize="2147483647"/>
          </binding>
        </customBinding>
      </bindings>
      <client>
          <endpoint name="WebHttpBinding"  
              address="Service Address/"
              binding="customBinding" 
              bindingConfiguration="WebHttpBinding"
              contract="TestClientAPI.API" />
      </client>
   </system.serviceModel>

任何帮助将不胜感激.

我在 msdn 中找到了一个帖子,上面写着

I found one thread in msdn which says

这是 Web 编程模型本身的限制.与 SOAP 端点(即具有 BasicHttpBinding、WSHttpBinding 等的端点)不同,SOAP 端点有一种方法可以公开有关自身的元数据(WSDL 或 Mex)以及有关所有操作的信息/端点中的参数,目前没有标准方法来公开非 SOAP 端点的元数据——这正是基于 webHttpBinding 的端点.简而言之,WCF 测试客户端对基于 Web 的端点没有用.如果当 WCF 发布其下一个版本时,会出现一些表示 Web 样式端点的标准,我们可能会更新测试客户端以支持它,但目前还没有被广泛采用.

"This is a limitation of the web programming model itself. Unlike SOAP endpoints (i.e., those with BasicHttpBinding, WSHttpBinding, etc) which have a way to expose metadata about itself (WSDL or Mex) with information about all the operations / parameters in the endpoint, there's currently no standard way to expose metadata for a non-SOAP endpoint - and that's exactly what the webHttpBinding-based endpoints are. In short, the WCF Test Client won't be useful for web-based endpoints. If some standard for representing web-style endpoints emerges when WCF ships its next version, we'll likely update the test client to support it, but for now there's none widely adopted.

您可以尝试的一种替代方法(虽然绝对不是那么简单/方便)是使用 ChannelFactory 类(使用服务中使用的相同合同/绑定/行为(参见下面的示例))在代码中自己创建测试客户端.然后可以使用一些网络捕获/重放工具,例如 Fiddler (www.fiddlertool.com) 向服务发送额外的请求."

One alternative you can try (although definitely not as simple / handy) is to create the test client yourself in code using the ChannelFactory class (using the same contract / binding / behavior used in the service (see example below). You can then use some network capture/replay tool such as Fiddler (www.fiddlertool.com) to send additional requests to the service."

链接是:http://social.msdn.microsoft.com/forums/en-US/wcf/thread/deabd25b-a219-4e95-9826-d40dc2f75543

我正在寻找这个确切问题的解决方案.我可以使用 ChannelFactory 类或 Fiddler 来使用服务.我无法使用服务客户端.我想在WCF模型中这个问题仍然没有进展.

And I am looking for the solution of this exact problem. I am able to consume service using the ChannelFactory class or Fiddler. I am not being able to use the Service client. I guess there is still no progress in this issue in WCF model.

还有一件事,我在服务器的 web.config 或客户端的 app.config 中没有.我在服务器 Web.config 中添加了这个,然后我得到了一个不同的错误'System.ServiceModel.ClientBase`1'的类型初始化程序抛出了一个异常."我还从配置中删除了 webHttpBinding 设置.

One more thing, I dont have in my web.config at server or app.config in client. I added this in the Server Web.config and then I got a different error "The type initializer for 'System.ServiceModel.ClientBase`1' threw an exception." Also i removed the webHttpBinding Settings from config.

推荐答案

首先,webHttpBinding(REST 风格)不使用任何这些设置 - 这些仅适用于基于 SOAP 的wsHttpBindingbasicHttpBinding 之类的绑定.

First of all, the webHttpBinding (REST-style) does not use any of those settings - those are only for SOAP-based bindings like wsHttpBinding or basicHttpBinding.

对于 WCF REST 样式绑定,您需要确保 IIS 可以支持更大的传输 - 默认情况下,IIS 最大消息大小设置为比默认值更高的值.

For WCF REST style bindings, you need to make sure the IIS can support larger transfers - by default, the IIS max message size to higher values than it is by default.

检查您的 web.config - 您有这样的条目吗?

Check your web.config - do you have an entry like this?

<system.web>
     ......
     <httpRuntime maxRequestLength="32678"/>  
     ......
</system.web>

这是一个以 KB 为单位的限制 - 默认值为 4096 (4 MB) - 您可能需要将其增加到更高的值.

This is a limit in KB - the default is 4096 (4 MB) - you might need to increase that to a higher value.

如果不是消息大小导致问题,那么可能是您的服务导致了异常?你到底在做什么?上传或下载文件什么的?

If it's not the message size that causes the problem, then maybe your service is causing an exception? What exactly are you doing? Uploading or downloading files or something?

马克

这篇关于错误请求错误 400 - WCF 客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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