asp.net – 如何在WCF中使用SOAP :: Lite?
我只是想开始使用WCF.听起来它应该是超级简单的,但使用像SOAP :: Lite这样的简单API,我无法使任何工作. googlesphere中有很多过时的例子,我尝试了很多这些例子都无济于事.所以现在我试图将问题分解到最低水平.我创建了由Visual Web Developer 2008 Express模板生成的WCF应用程序.这是它的服务合同:
[ServiceContract] public interface IService1 { [OperationContract] string GetData(int value); [OperationContract] CompositeType GetDataUsingDataContract(CompositeType composite); // TODO: Add your service operations here } // Use a data contract as illustrated in the sample below to add composite types to service operations. [DataContract] public class CompositeType { bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get { return boolValue; } set { boolValue = value; } } [DataMember] public string StringValue { get { return stringValue; } set { stringValue = value; } } } 我在调试模式下启动它并试图联系.使用调试URL http:// localhost:59315 / Service1.svc使用浏览器这样做会给我一个页面说明:
所以,然后我浏览了那个wsdl并得到了这个: <wsdl:definitions name="Service1" targetNamespace="http://tempuri.org/"> <wsp:Policy wsu:Id="WSHttpBinding_IService1_policy"> <wsp:ExactlyOne> <wsp:All> <sp:SymmetricBinding> <wsp:Policy> <sp:ProtectionToken> <wsp:Policy> <sp:SecureConversationToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"> <wsp:Policy> <sp:RequireDerivedKeys/> <sp:BootstrapPolicy> <wsp:Policy> <sp:SignedParts> <sp:Body/> <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/> </sp:SignedParts> <sp:EncryptedParts> <sp:Body/> </sp:EncryptedParts> <sp:SymmetricBinding> <wsp:Policy> <sp:ProtectionToken> <wsp:Policy> <sp:SpnegoContextToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"> <wsp:Policy> <sp:RequireDerivedKeys/> </wsp:Policy> </sp:SpnegoContextToken> </wsp:Policy> </sp:ProtectionToken> <sp:AlgorithmSuite> <wsp:Policy> <sp:Basic256/> </wsp:Policy> </sp:AlgorithmSuite> <sp:Layout> <wsp:Policy> <sp:Strict/> </wsp:Policy> </sp:Layout> <sp:IncludeTimestamp/> <sp:EncryptSignature/> <sp:OnlySignEntireHeadersAndBody/> </wsp:Policy> </sp:SymmetricBinding> <sp:Wss11> <wsp:Policy> <sp:MustSupportRefKeyIdentifier/> <sp:MustSupportRefIssuerSerial/> <sp:MustSupportRefThumbprint/> <sp:MustSupportRefEncryptedKey/> </wsp:Policy> </sp:Wss11> <sp:Trust10> <wsp:Policy> <sp:MustSupportIssuedTokens/> <sp:RequireClientEntropy/> <sp:RequireServerEntropy/> </wsp:Policy> </sp:Trust10> </wsp:Policy> </sp:BootstrapPolicy> </wsp:Policy> </sp:SecureConversationToken> </wsp:Policy> </sp:ProtectionToken> <sp:AlgorithmSuite> <wsp:Policy> <sp:Basic256/> </wsp:Policy> </sp:AlgorithmSuite> <sp:Layout> <wsp:Policy> <sp:Strict/> </wsp:Policy> </sp:Layout> <sp:IncludeTimestamp/> <sp:EncryptSignature/> <sp:OnlySignEntireHeadersAndBody/> </wsp:Policy> </sp:SymmetricBinding> <sp:Wss11> <wsp:Policy> <sp:MustSupportRefKeyIdentifier/> <sp:MustSupportRefIssuerSerial/> <sp:MustSupportRefThumbprint/> <sp:MustSupportRefEncryptedKey/> </wsp:Policy> </sp:Wss11> <sp:Trust10> <wsp:Policy> <sp:MustSupportIssuedTokens/> <sp:RequireClientEntropy/> <sp:RequireServerEntropy/> </wsp:Policy> </sp:Trust10> <wsaw:UsingAddressing/> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetData_Input_policy"> <wsp:ExactlyOne> <wsp:All> <sp:SignedParts> <sp:Body/> <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/> </sp:SignedParts> <sp:EncryptedParts> <sp:Body/> </sp:EncryptedParts> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetData_output_policy"> <wsp:ExactlyOne> <wsp:All> <sp:SignedParts> <sp:Body/> <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/> </sp:SignedParts> <sp:EncryptedParts> <sp:Body/> </sp:EncryptedParts> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetDataUsingDataContract_Input_policy"> <wsp:ExactlyOne> <wsp:All> <sp:SignedParts> <sp:Body/> <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/> </sp:SignedParts> <sp:EncryptedParts> <sp:Body/> </sp:EncryptedParts> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <wsp:Policy wsu:Id="WSHttpBinding_IService1_GetDataUsingDataContract_output_policy"> <wsp:ExactlyOne> <wsp:All> <sp:SignedParts> <sp:Body/> <sp:Header Name="To" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="From" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="FaultTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="ReplyTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="MessageID" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="RelatesTo" Namespace="http://www.w3.org/2005/08/addressing"/> <sp:Header Name="Action" Namespace="http://www.w3.org/2005/08/addressing"/> </sp:SignedParts> <sp:EncryptedParts> <sp:Body/> </sp:EncryptedParts> </wsp:All> </wsp:ExactlyOne> </wsp:Policy> <wsdl:types> <xsd:schema targetNamespace="http://tempuri.org/Imports"> <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd0" namespace="http://tempuri.org/"/> <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/> <xsd:import schemaLocation="http://localhost:59315/Service1.svc?xsd=xsd2" namespace="http://schemas.datacontract.org/2004/07/RossDominosService"/> </xsd:schema> </wsdl:types> <wsdl:message name="IService1_GetData_InputMessage"> <wsdl:part name="parameters" element="tns:GetData"/> </wsdl:message> <wsdl:message name="IService1_GetData_OutputMessage"> <wsdl:part name="parameters" element="tns:GetDataResponse"/> </wsdl:message> <wsdl:message name="IService1_GetDataUsingDataContract_InputMessage"> <wsdl:part name="parameters" element="tns:GetDataUsingDataContract"/> </wsdl:message> <wsdl:message name="IService1_GetDataUsingDataContract_OutputMessage"> <wsdl:part name="parameters" element="tns:GetDataUsingDataContractResponse"/> </wsdl:message> <wsdl:portType name="IService1"> <wsdl:operation name="GetData"> <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetData" message="tns:IService1_GetData_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataResponse" message="tns:IService1_GetData_OutputMessage"/> </wsdl:operation> <wsdl:operation name="GetDataUsingDataContract"> <wsdl:input wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContract" message="tns:IService1_GetDataUsingDataContract_InputMessage"/> <wsdl:output wsaw:Action="http://tempuri.org/IService1/GetDataUsingDataContractResponse" message="tns:IService1_GetDataUsingDataContract_OutputMessage"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="WSHttpBinding_IService1" type="tns:IService1"> <wsp:PolicyReference URI="#WSHttpBinding_IService1_policy"/> <soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="GetData"> <soap12:operation soapAction="http://tempuri.org/IService1/GetData" style="document"/> <wsdl:input> <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetData_Input_policy"/> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetData_output_policy"/> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="GetDataUsingDataContract"> <soap12:operation soapAction="http://tempuri.org/IService1/GetDataUsingDataContract" style="document"/> <wsdl:input> <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetDataUsingDataContract_Input_policy"/> <soap12:body use="literal"/> </wsdl:input> <wsdl:output> <wsp:PolicyReference URI="#WSHttpBinding_IService1_GetDataUsingDataContract_output_policy"/> <soap12:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Service1"> <wsdl:port name="WSHttpBinding_IService1" binding="tns:WSHttpBinding_IService1"> <soap12:address location="http://localhost:59315/Service1.svc"/> <wsa10:EndpointReference> <wsa10:Address>http://localhost:59315/Service1.svc</wsa10:Address> <Identity> <Dns>localhost</Dns> </Identity> </wsa10:EndpointReference> </wsdl:port> </wsdl:service> </wsdl:definitions> 没有什么太疯狂了(除了SOAP本身)…无论如何,现在我尝试一个简单的SOAP :: Lite应用程序: #!/cygdrive/C/Perl64/bin/perl.exe -w use strict; use warnings; use Data::Dumper; use SOAP::Lite +trace => [ transport => sub { my ($in) = @_; if ( ref($in) eq "HTTP::Request") { print( "REQUESTn" . $in->content() . "nEND REQUESTn" ); } elsif ( ref($in) eq "HTTP::Response") { print( "RESPONSEn" . $in->content() . "nEND RESPONSEn" ); } } ]; my $server = 'http://localhost:59315/Service1.svc'; my $url = "$server?wsdl"; my $xmlns = 'http://schemas.datacontract.org/2004/07/RossDominosService'; # Setup Network Connection my $service = SOAP::Lite ->ns( $url ) ->proxy( $server ) ->on_fault( sub { my ($soap,$res) = @_; die ref $res ? $res->faultstring() : $soap->transport()->status(),"n"; } ); ; print( Dumper( $service ) ); eval { print( "making requestn" ); my $response = $service->GetData( SOAP::Data->new( name => 'value',value => 5 ) ); print( "got response:n$responsen" ); }; if ( $@ ) { print( "failed:n**************************n$@n*****************************n" ); } 调试中的哪个是发出此SOAP请求: <?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:namesp1="http://localhost:59315/Service1.svc?wsdl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <namesp1:GetData> <value xsi:type="xsd:int">5</value> </namesp1:GetData> </soap:Body> </soap:Envelope> 但回应是: failed: ************************** 415 Unsupported Media Type ***************************** 到底是怎么回事?这就是“Hello,World!” SOAP应用程序,我无法弄明白. ———————– UPDATE ————————– —– 我发现我需要将绑定从其默认的wsHttpBinding更改为: <endpoint address="" binding="basicHttpBinding" contract="RossDominosService.IService1"> 现在它似乎连接,但我收到这个新错误: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body> <s:Fault> <faultcode xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:ActionNotSupported</faultcode> <faultstring xml:lang="en-US">The message with Action 'http://tempuri.org/GetData' 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).</faultstring> </s:Fault> </s:Body> </s:Envelope> 这似乎不应该这么难…… ———————– UPDATE ————————– —– 得到它了!经过一些修修补补,以及soapUI的一些帮助,我发现需要修改动作,以便: <namespace>/<serviceContract>/<method> 在这种情况下: http://tempuri.org/IService1/GetData 使用SOAP :: Lite时,默认的SOAPAction是 <namespace>#<method> 所以你必须这样修改你的soap对象: $soap->on_action( sub { sprintf( '%sIService1/%s',@_ ) } ); 我会写下面的答案,所以我们可以关闭这个问题…… 解决方法
这就是我使用perl SOAP :: Lite客户端启动和运行WCF Web服务的方法.请注意,我使用Visual Web Developer Express 2008和perl 5.14与SOAP :: Lite 0.715.
>打开Visual Web Developer > Visual C# – >卷筒纸 >打开Web.config >更改< compilation debug =“false”> to< compilation debug =“true”> >打开Service1.svc.cs >按f5(开始调试) 现在应该弹出一个浏览器窗口打开到您的服务页面.记下网址中的端口,您将需要它为客户端.然后创建此脚本: #!/cygdrive/C/Perl64/bin/perl.exe -w use strict; use warnings; use Data::Dumper; use SOAP::Lite +trace => [ transport => sub { my ($in) = @_; if ( ref($in) eq "HTTP::Request") { print( "**** REQUEST ****n" . $in->content() . "n**** END REQUEST ****n" ); } elsif ( ref($in) eq "HTTP::Response") { print( "**** RESPONSE ****n" . $in->content() . "n**** END RESPONSE ****n" ); } } ]; my $port = 63181; my $server = "http://localhost:$port/Service1.svc"; my $namespace = 'http://tempuri.org/'; # Setup Network Connection my $service = SOAP::Lite ->ns( $namespace,'my' ) ->proxy( $server ) ->on_action( sub { my $action = sprintf( '%sIService1/%s',@_ ); print( "action: '$action'n" ); return $action; } ); print( Dumper( $service ) ); eval { print( "making requestn" ); my $response = $service->GetData( SOAP::Data->new( prefix => 'my',name => 'value',value => 5 ) ); print( "got response:n$responsen" ); }; if ( $@ ) { print( "failed:n**************************n$@n*****************************n" ); } 您需要更改端口号以匹配您的端口号.另外,我留下了一堆调试代码,这样你就可以看到发生了什么. 我希望这有助于其他人. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何根据用户角色启用和禁用按钮?
- asp.net-web-api – JwtSecurityTokenHandler 4.0.0重大改变
- 如何在ASP.Net MVC中执行301永久重定向路由
- asp.net-mvc – 如何测试MVC3 razor布局中是否存在变量?
- 对于单个控件,asp.net – ValidateRequest =“false”
- asp.net-mvc – AllowAnonymous不能使用自定义Authorizatio
- 如何从经典ASP输出Excel * .xls文件
- asp.net-mvc – AJAX POST到MVC Controller显示302错误
- asp.net-mvc-3 – MVC3 razor Webgrid分页和排序超过2000条
- asp.net – 如何诊断IIS将CPU推向100%?
- ASP.NET Core中使用xUnit进行单元测试
- asp.net-mvc – 为桌面和移动浏览器格式化ASP.Ne
- asp.net-mvc-5 – nuget中用于与WEB API集成的Ni
- asp.net – Orchard CMS如何进行日志记录?
- asp.net-mvc – 如何使用ASP.NET MVC在我的现有数
- asp.net-mvc-3 – ASP.Net MVC 3不显眼的验证不适
- 如何在asp.net mvc 2中获取Html.EditorForModel(
- asp.net-mvc – 依赖注入框架的重点是什么?
- asp.net – Html.Partial()跳过控制器动作
- asp.net – Web API 2 – ApiController.Interna