我有一个非常简单的 c# 应用程序,它运行 WCF 服务,如下所示:
[ServiceContract]
public interface IMessageRepository
{
event EventHandler<string> OnNewMessage;
[OperationContract]
void RegisterClient();
[OperationContract]
void SendMessage(string message);
void ClearClients();
}
应用程序配置
<system.serviceModel>
<services>
<service name="CatcherService.Services.MessageRepository">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8740/MessageRepository/" />
</baseAddresses>
</host>
<endpoint address=""
binding="basicHttpBinding"
contract="CatcherService.Infrastructure.IMessageRepository"
>
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:8740/MessageRepository/"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
因为我不太了解 PERL,所以我从网上获取了一个客户端示例并对其进行了一些定制:
#!/usr/bin/perl
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 = 8740;
my $server = "http://localhost:$port";
my $namespace = 'http://tempuri.org/';
# Setup Network Connection
my $service = SOAP::Lite
->ns( $namespace, 'my' )
->proxy( $server )
->on_action( sub {
my $action = sprintf( 'IMessageRepository/%s', @_ );
print( "action: '$action'\n" );
return $action;
} );
print( Dumper( $service ) );
eval {
print( "making request\n" );
my $response = $service->SendMessage("A message from perl client");
print( "got response:\n$response\n" );
};
if ( $@ ) {
print( "failed:\n**************************\n$@\n*****************************\n" );
}
我在运行时收到以下错误消息:
action: 'IMessageRepository/http://tempuri.org/'
**** REQUEST ****
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:my="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><my:SendMessage><c-gensym3 xsi:type="xsd:string">A message from perl client</c-gensym3></my:SendMessage></soap:Body></soap:Envelope>
**** END REQUEST ****
**** RESPONSE ****
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Not Found</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Not Found</h2>
<hr><p>HTTP Error 404. The requested resource is not found.</p>
</BODY></HTML>
**** END RESPONSE ****
我错过了什么?perl 应该调用 C# 服务器中的 SendMessage("some text") 方法...