One
In the previous chapter, we mainly introduced the server's listening address and logical address. This section simulates the message forwarding mechanism to actually experience how an endpoint's listening address is used.
Download a small software called Tcptrace (108k), which intercepts the port message and forwards the message.
First look at the server-side code, the server-side bindings use Ws2007httpbinding's unencrypted mode, because we will also see the tcptrace captured data plaintext, so it cannot be encrypted. The client is also using an unencrypted ws2007httpbinding.
Using System.ServiceModel;
Using System.ServiceModel.Description;
Using System.ServiceModel.Channels;
Namespace host
{
Class Program
{
static void Main (string[] args)
{
using (ServiceHost host = new ServiceHost (typeof (MYWCF. Calculator)))
{
Ws2007httpbinding bind = new ws2007httpbinding ();
Wshttpsecurity Security = new Wshttpsecurity ();
Security. Mode = Securitymode.none;
Bind. Security = security;
Host. AddServiceEndpoint (typeof (MYWCF. ICalculator), bind, "http://localhost:8888");
Host. Opened + = delegate {Console.WriteLine ("Service start!");};
Host. Open ();
Console.ReadLine ();
}
}
}
}
The server publishes an endpoint. The previous chapter says that the default logical address is the same as the listening address, both http://localhost:8888.
The client code is as follows:
Using System.ServiceModel;
Using System.ServiceModel.Description;
Using System.ServiceModel.Channels;
Namespace Client
{
Class Program
{
static void Main (string[] args)
{
Ws2007httpbinding bind = new ws2007httpbinding ();
Wshttpsecurity Security = new Wshttpsecurity ();
Security. Mode = Securitymode.none;
Bind. Security = security;
EndpointAddress endpint=new endpointaddress ("http://localhost:8888");
MYWCF. ICalculator client = CHANNELFACTORY<MYWCF. Icalculator>. CreateChannel (Bind, Endpint, New Uri ("http://localhost:9999"));
Console.WriteLine (client. ADD (1, 2));
}
}
}
Start the server first, and then start the client. The client reported an error and could not find the correct endpoint.
The client's access to the logical address is http://localhost:8888, and the listener address is http://localhost:9999. But the server listener is 8888, and does not listen 9999, so access failed. We now use the Tcptrace tool to forward the message received by the server 9999 port to the server 8888 port, the server will be able to correctly receive the request.
Click OK, and now again start the server and the client, the access is successful, and has been successfully forwarded.
Now analyze the message content. The client request <To> header is a logical address of localhost:8888, and the destination address is localhost:9999,<body> for x=1,y=2, which requests the add operation. The following server returns a successful,<addresult> of 3. Because the client's <To> logical address is the same as the server-side logical address, it is possible to pass the endpoint's message filter.
Two
If the service-side code is not changed, the server's logical address and the listening address is still 8888, now the client's logical address and listening address are set to 9999, and then through Tcptrace forwarding, 9999-port message forwarding to 8888 port.
The client code is as follows:
usingSystem.ServiceModel;usingSystem.ServiceModel.Description;usingSystem.ServiceModel.Channels;namespaceclient{classProgram {Static voidMain (string[] args) {ws2007httpbinding bind=Newws2007httpbinding (); Wshttpsecurity Security=Newwshttpsecurity (); Security. Mode=Securitymode.none; Bind. Security=security; EndpointAddress Endpint=NewEndpointAddress ("http://localhost:9999"); MYWCF. ICalculator Client= CHANNELFACTORY<MYWCF. Icalculator>. CreateChannel (Bind, endpint); Console.WriteLine (client. ADD (1,2)); } }}
Successively run the client and the service side, found an exception reported.
Let's take a look at the Tcptrace intercept request message:
The port of the <To> header address becomes 9999. Even if Tcptrace is forwarded because the logical address does not match, such a message cannot pass through the address filter of the server end endpoint.
Three
If you want the above client to have normal access to the server. The service behavior of the custom class library is set Addressfiltermode to Addressfiltermode.any, even if the requested <To> logical address is inconsistent with the service-side endpoint logical address.
using System.ServiceModel; namespace mywcf{ [ServiceBehavior (Addressfiltermode=addressfiltermode.any)] public class Calculator:icalculator { publicint Add (intint y) { return x + y;}} }
wcf-Endpoint Message Routing example