400 Bad Request Exception:具有小数据的简单SOAP WCF服务

时间:2012-09-13 16:41:29

标签: c# wcf

我有一个简单的WCF服务起诉 SOAP 。我有一个非常简单的操作“GetMultiplied”,只有非常少量的数据。当客户端尝试调用该操作时,我得到以下异常。知道什么都可能是问题吗?

  

内部异常:{“远程服务器返回错误:(400)错误请求。”}

最后列出了完整的wsdl和架构。

注意:我已在服务和客户端配置中将配额值maxBufferSize等设置为更高的值。

追踪服务

当我在服务中使用跟踪时(基于How to turn on WCF tracing?),我得到以下内容 - 似乎没有记录错误。

<Type>3</Type>
<SubType Name="Information">0</SubType>
<Level>8</Level>
<TimeCreated SystemTime="2012-09-13T17:05:17.6059181Z" />
<Source Name="System.ServiceModel" />
<Description>AppDomain unloading.</Description>

服务实施

public class CalculationService : ICalculationService
{

    public virtual GetMultipliedResponse GetMultiplied(GetMultipliedRequest request)
    {
        MultipliedResult result = new MultipliedResult();
        result.ResultNumber= ((request.InputNumber)*2);

        GetMultipliedResponse response = new GetMultipliedResponse(result);
        return response;
    }
}

客户端

    static void Main(string[] args)
    {
        CalculationServiceInterfaceClient proxy = new CalculationServiceInterfaceClient();
        multipliedResult result = proxy.getMultiplied(2);
    }

在自动生成的代码中,详细信息为:

    public NewClient.CalcReference.multipliedResult getMultiplied(int inputNumber) 
    {
        NewClient.CalcReference.getMultipliedRequest inValue = new NewClient.CalcReference.getMultipliedRequest();
        inValue.inputNumber = inputNumber;

        NewClient.CalcReference.getMultipliedResponse retVal = ((NewClient.CalcReference.CalculationServiceInterface)(this)).getMultiplied(inValue);
        return retVal.restaurants;
    }

WSDL

<definitions xmlns:import0="urn:lijo:demos:multiplyservice:messages:v1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:import1="urn:lijo:demos:multiplyservice:data:v1" xmlns:tns="urn:lijo:demos:multiplyservice:calculation:v1" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" name="CalculationService" targetNamespace="urn:lijo:demos:multiplyservice:calculation:v1" xmlns="http://schemas.xmlsoap.org/wsdl/">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
<types>
<xsd:schema>
  <xsd:import schemaLocation="C:\toolbox\LijosServiceApp\NewService\RestaurantMessages.xsd" namespace="urn:lijo:demos:multiplyservice:messages:v1" />
  <xsd:import schemaLocation="C:\toolbox\LijosServiceApp\NewService\RestaurantData.xsd" namespace="urn:lijo:demos:multiplyservice:data:v1" />
  </xsd:schema>
 </types>
 <message name="getMultipliedIn">
 <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
 <part name="parameters" element="import0:getMultiplied" />
 </message>
 <message name="getMultipliedOut">
 <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
 <part name="parameters" element="import0:getMultipliedResponse" />
 </message>
 <portType name="CalculationServiceInterface">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <operation name="getMultiplied">
  <wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" />
  <input message="tns:getMultipliedIn" />
  <output message="tns:getMultipliedOut" />
  </operation>
  </portType>
  <binding name="BasicHttpBinding_CalculationServiceInterface" type="tns:CalculationServiceInterface">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
  <operation name="getMultiplied">
  <soap:operation soapAction="urn:lijo:demos:multiplyservice:calculation:v1:getMultipliedIn" style="document" />
  <input>
    <soap:body use="literal" />
  </input>
  <output>
    <soap:body use="literal" />
  </output>
  </operation>
  </binding>
  <service name="CalculationServicePort">
  <port name="CalculationServicePort" binding="tns:BasicHttpBinding_CalculationServiceInterface">
  <soap:address location="http://localhost/CalculationService" />
  </port>
  </service>
  </definitions>

XSD

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="RestaurantData" targetNamespace="urn:lijo:demos:multiplyservice:data:v1"
elementFormDefault="qualified" xmlns="urn:lijo:demos:multiplyservice:data:v1"
xmlns:mstns="urn:lijo:demos:multiplyservice:data:v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">

 <xs:complexType name="multipliedResult">
 <xs:sequence>
  <xs:element name="resultNumber" type="xs:int" />
 </xs:sequence>
 </xs:complexType>
 </xs:schema>

Cleint Config (自动生成)

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_CalculationServiceInterface"
                closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00"
                sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false"
                hostNameComparisonMode="StrongWildcard" maxBufferSize="65536"
                maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                useDefaultWebProxy="true">
                <readerQuotas maxDepth="524288" maxStringContentLength="524288" maxArrayLength="524288"
                    maxBytesPerRead="524288" maxNameTableCharCount="524288" />
                <security mode="None">
                    <transport clientCredentialType="None" proxyCredentialType="None"
                        realm="" />
                    <message clientCredentialType="UserName" algorithmSuite="Default" />
                </security>
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost/CalculationService" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_CalculationServiceInterface"
            contract="CalcReference.CalculationServiceInterface" name="CalculationServicePort" />
    </client>
</system.serviceModel>

1 个答案:

答案 0 :(得分:2)

我解决了问题:-)

我会为其他人的利益发布答案。

  1. 关键问题:我试图使用手动创建的wsdl。 (我在服务中提到了本地副本 - 我正在使用工具从wsdl生成服务代码)。该服务没有提供。我应该尝试通过浏览svc文件来查看wsdl

  2. 使用 WcfTestClient 运行服务。发出一个错误,显示项目名称和我们使用的命名空间应该相同。 (否则它将在命名空间名称之前附加项目名称,这将成为不正确的命名空间)

    在“Visual Studio命令提示符”中键入“WcfTestClient”命令。 http://blogs.msdn.com/b/wcftoolsteamblog/archive/2010/01/04/tips-for-launching-wcf-test-client.aspx

  3. 通过浏览服务中的svc文件,显示元数据发布未启用。在web.config中添加了元数据浏览的服务行为。

  4. 服务的使用相对路径(而不是localhost)error "No protocol binding matches the given address ..."

  5. 服务跟踪也很有帮助(虽然这里没有帮助我)。使用“C:\ Program Files \ Microsoft SDKs \ Windows \ v7.0A \ bin \ SvcTraceViewer.exe”。在帖子后面,错误文件(initializeData =“Error.svclog”)存储在解决方案项目中。将其更改为其他位置不起作用。 How to turn on WCF tracing?

  6. 参考One WCF service – two clients; One client does not work