2

我有一个问题......我探索了整个互联网,但我不知道我做错了什么。

问题:WCF Web 服务、.Net Framework 3.5、2 种不同类型的客户端(手持设备和普通计算机)

我要做的是创建 2 个不同的端点,一个带有 basicBinding(用于 SOAP 请求),另一个带有 wsBinding(用于普通计算机)

所以我通过 web.config 并创建了 2 个不同的绑定,与 2 个不同的端点相关:

<bindings>
  <basicHttpBinding>
    <binding name="BasicBinding" openTimeout="00:10:00" receiveTimeout="23:59:00"
      sendTimeout="23:59:00" messageEncoding="Text" />
  </basicHttpBinding>
  <wsHttpBinding>
    <binding name="DefaultBinding" openTimeout="00:10:00" receiveTimeout="23:59:59"
      sendTimeout="23:59:59">
      <reliableSession inactivityTimeout="01:00:00" />
      <security mode="None">
        <transport clientCredentialType="None" />
        <message clientCredentialType="None" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="qtswsdl.QTS_ServiceBehavior"
    name="qtswsdl.QTS_Service">
    <endpoint address="http://host.com/service.svc/ForHh"
      binding="basicHttpBinding" bindingConfiguration="BasicBinding"
      name="handHeldEndPoint" contract="qtswsdl.QTSPort" />
    <endpoint address="http://host.com/service.svc/ForCp"
      binding="wsHttpBinding" bindingConfiguration="DefaultBinding"
      name="coProcessorEndPoint" contract="qtswsdl.QTSPort" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="qtswsdl.QTS_ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      <serviceThrottling maxConcurrentCalls="2147483647"maxConcurrentSessions="2147483647"
        maxConcurrentInstances="2147483647" />
    </behavior>
  </serviceBehaviors>
</behaviors>

因此,当我尝试将 SOAP 消息发送到“http://host.com/service.svc/ForHh”时,我收到“HTTP 400 - 错误请求”(/ForCp 也不起作用)

我尝试使用自定义客户端,使用 WcfTestClient.exe,但我无法确定正在发生的事情

任何提示或建议?

谢谢你的时间

编辑:

启用 Svc 跟踪后,我遇到了一些异常:

<Message>The message with Action 'http://Host.com/Service.svc/ForHh' cannot be processed at the receiver, due to a ContractFilter mismatch at the EndpointDispatcher. This may be because of either a contract mismatch (mismatched Actions between sender and receiver) or a binding/security mismatch between the sender and the receiver.  Check that sender and receiver have the same contract and the same binding (including security requirements, e.g. Message, Transport, None).</Message>

有趣的是,我正在以编程方式发送 SOAP 请求。我的理解是,如果我以编程方式发送 SOAP 请求,我不需要定义任何合同,因为默认情况下它是使用 SOAP 1.1 发送的。

发送请求的代码如下:

private string SendRequestAndGetAnswerFromWebService(string methodName, string requestXml){

     StringBuilder soapRequest = new StringBuilder("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
                soapRequest.Append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" ");
                soapRequest.Append("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>");
                soapRequest.Append(requestXml);//Body
                soapRequest.Append("</soap:Body></soap:Envelope>");

                   WebRequest webRequest = WebRequest.Create(@"http://Host.com/Service.svc/" + methodName);
                   HttpWebRequest httpRequest = (HttpWebRequest)webRequest;
                   httpRequest.Method = "POST";
                   httpRequest.ContentType = "text/xml; charset=ascii";
                   httpRequest.Headers.Add("SOAPAction: " + @"http://Host.com/Service.svc/Service.svc/" + methodName);
                   httpRequest.ProtocolVersion = HttpVersion.Version11;
                   httpRequest.Credentials = CredentialCache.DefaultCredentials;
                   httpRequest.Timeout = 7000;
                   httpRequest.ContentLength = System.Text.Encoding.ASCII.GetByteCount(soapRequest.ToString());
                   Stream requestStream = httpRequest.GetRequestStream();

                   //Create Stream and send Request 
                   StreamWriter streamWriter = new StreamWriter(requestStream, Encoding.ASCII);
                   streamWriter.Write(soapRequest.ToString());
                   //Send the request             
                   streamWriter.Close();

                   //Get the Response
                   HttpWebResponse wr = (HttpWebResponse)httpRequest.GetResponse();
                   StreamReader srd = new StreamReader(wr.GetResponseStream());
                   string resulXmlFromWebService = srd.ReadToEnd();
                   return resulXmlFromWebService;
}

也许我对以编程方式发送的合同和肥皂消息的假设是错误的......

4

1 回答 1

0

通常,这是非常糟糕的做法——您不应该以这种方式手动将 SOAP 请求创作到 WCF 服务。如果您要利用开箱即用的 WCF 请求/回复范例而不是手动设置标头,那就更好了。

我建议创建一个替代示例,您可以在其中实现开箱即用的相同功能(可能在客户端使用 DataContracts、OperationContracts 和 ServiceContracts,可能利用 WebHttpBehavior 和 WebHttpBinding)。检查在该场景中发送和接收的确切消息,并逐个字符仔细检查这些消息与您在此处发送的消息有何不同。

此外,仅从您提供的细节中很难判断您可能做错了什么。

这是我的建议:

一般来说,一旦你这样做了,你应该得到更多关于服务端发生了什么问题的更多信息,并且可以很快诊断出问题。试试看,请回来报告!:)

于 2012-04-26T15:35:30.090 回答