我有一个问题......我探索了整个互联网,我不知道我做错了什么。
问题:WCF Web服务,.Net Framework 3.5,2种不同类型的客户端(手持设备和普通计算机)
我要做的是创建2个不同的端点,一个使用basicBinding(For 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;
}
也许我对以编程方式发送的合同和肥皂消息的假设是错误的......
答案 0 :(得分:0)
通常,这是非常糟糕的做法 - 您不应该以这种方式手动将SOAP请求创作到WCF服务。如果你要利用开箱即用的WCF请求/回复范例而不是手动设置标题,那就更好了。
我建议的是创建一个替代示例,您可以在其中实现开箱即用的相同功能(可能在客户端使用DataContracts,OperationContracts和ServiceContracts,也许可以利用WebHttpBehavior和WebHttpBinding) 。检查在该场景中发送和接收的确切消息,并逐个字符地仔细检查这些消息与您在此处发送的消息的不同之处。
此外,很难从你提供的细节中分辨出你可能做错的其他事情。
以下是我的建议:
在服务端启用跟踪,生成跟踪日志,并使用SvcTraceViewer进行分析。为此,请按照this MSDN article on using the service trace viewer上的说明操作。注意第一次和第二次之间有什么不同。
启用调试例外。这是通过输入include {ExceptionDetailInFaults you can do by following the instructions here来完成的。注意第一次和第二次之间有什么不同。
使用Fiddler监控客户端和服务端的有线流量。
通常,一旦你这样做,你应该有更多关于服务方面什么是时髦的信息,并且可以很快地诊断问题。试试吧,请报告回来! :)