Showing posts with label WCF. Show all posts
Showing posts with label WCF. Show all posts

Tuesday, September 14, 2010

Fix WCF AddressFilter mismatch error, customized IServiceBehavior , WCF service behind Load balancer or Firewall

If you WCF service is hosed behind Load balancer or firewall , or been forwarded by any kind of device. you may end with an error like

The message with To 'http://127.0.0.1:8888/bHost1/WCFService1.svc' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher.  Check that the sender and receiver's EndpointAddresses agree.


if you have the source code, you may have to put one attribute to each of those services.

[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]

that’s fine if you own the source code. or just handful of service. If you have hundreds of service, that will be terrible change. How to do that in that case.

answse is pretty simple, write a behavior and apply it to the configuration. All you have to do is writing a behavior which will change the default addressfilter, and Apply the behavior by changing the web.config or app.config

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Configuration;

namespace Androidyou.TestLib
{
    public class OverrideWCFAddressFilterServiceBehavior : IServiceBehavior
    {
        public void AddBindingParameters(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase, System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {

        }

        public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {
            for (int i = 0; i < serviceHostBase.ChannelDispatchers.Count; i++)
            {
                ChannelDispatcher channelDispatcher = serviceHostBase.ChannelDispatchers[i] as ChannelDispatcher;

                foreach (EndpointDispatcher dispatcher2 in channelDispatcher.Endpoints)
                {
                    dispatcher2.AddressFilter = new MatchAllMessageFilter();
                }
            }
        }

        public void Validate(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
        {

        }
    }
    public class OverrideAddressFilterModeElement : BehaviorExtensionElement
    {

        public override Type BehaviorType
        {
            get { return typeof(OverrideWCFAddressFilterServiceBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new OverrideWCFAddressFilterServiceBehavior();
        }
    }
}


Config change.

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="newbehavior">
                    <matchalladdressfilter/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <extensions>
            <behaviorExtensions>
               <add name="matchalladdressfilter" type="Androidyou.TestLib.OverrideAddressFilterModeElement, Androidyou.TestLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
            </behaviorExtensions>
        </extensions>
      
        <services>
            <service name="ConsoleApplication4.SayService" behaviorConfiguration="newbehavior">
              <host>
                <baseAddresses>
                  <add baseAddress="http://localhost:9999/svc"/>
                
                </baseAddresses>
              </host>
                <endpoint address="ws" binding="wsHttpBinding" bindingConfiguration="wsbinding"
                    contract="ConsoleApplication4.SayService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

Or you may apply the behavior in Code.


host.Description.Behaviors.Add(new Androidyou.TestLib.WCFSecurityLib.OverrideWCFAddressFilterServiceBehavior());

if you want to change addressfiltermode programmatically, you may put a switch into the above behaviors, like a static variable, then change the switch programmatically which will cause the behavior to refresh the filtermode.

Thursday, September 2, 2010

Free WCF / ASMX Test Trace tool. SoapUI and TCPTrace

I’ve always write a lot C# code to do some simple WCF testing. Until I find SoapUI, one Powerful wsdl stack test tool. It has the free Version which covers pretty all the test features I need. Let me show you a quick Tutorial.

SOAPUI is based ON http protocol mostly. so you might need to add one basicHttpBinding to you configuration. then we can enable the test on the http transport channel.

  Given a very basic Service.

image

when run the host application, you should be able to access the WSDL.

image

 

Now Just download the free version of soapUI from http://sourceforge.net/projects/soapui/files/ , there are different versions. I just select the 3.5.1 version.

SoapUI is based on Standard JAVA and javafx, so make sure you have the jdk ready . if javafx is not there, the installer will access internet and download the bits for you.

Run the soapui Utility.

image

Right click the project, Add one New project , you may specify a meaningful name.  you don't have to specify the initial wsdl , we will add it later on.


image

Right Click the new created Project , and chose Add WSDL

 image

Enter or Browse the WSDL url, I will put http://localhost:22276/Service1.svc?wsdl here. Click OK. Then you will see the Operation get listed in the project panel.

image

Double Click the Request 1 under GetData.  the SOAP request template has been generated for you, replace the ? with you value. and Click green Arrow button to submit the request.

image

for complexes object,  it’s there too. 
 image

 

Is it sweet? I think so.

Click the raw Tab. you can see the raw data there. there are some buttons that enable you to change the URL, Add customized Header. etc. (if you use the professional version. you will get more features like json view of the data.)

image

Further more, you can add it as a test case. Click the + button in the toolbar

image

 

then you can click the run the test .

image


also you can add more success criteria by click the add assertion button.

image

So here is all about SOAP. if you want to see the raw Http traffic, you can use the TCpTrace, also it’s free.

basically, change you service client to point a New port that TCPtrace is listening on, and TCPtrace will forward the request to the Service Port.

image

image

Please note. for those wsHttpBinding. you may have to Add one service behavior that allow server to process the request that has a different port(endpoint action), AddressFilterMode

image 
 

Also for the WCF client, you may just put one ClientViaBehavior to the endpoint, the client runtime will do the forwarding for you.

  


  more related articles.

Free WCF / ASMX Test Trace tool. SOAPbox by vordel

Wednesday, August 25, 2010

Silverlight communication with WCF service, Disable the .svc web browser Access

For the latest Silverlight 4.0. the WCF client runtime supports two Message Encodings, the  mtomMessageEncoding encoding is not available yet.

  • BinaryMessageEncoding
  • TextMessageEncoding

So from the client side (browser side) prospective, the Siliverlight APP communicate with the WCF service(.svc) either using the Soap/Text Format or Soap/Binary Format. typically, you can tell this from the Content-Type Header.

  • application/soap+msbin1
  • application/soap+xml;
image

here is the binaryencoding.

image

If you browse the .svc url directly. you may be able to see the wsdl ( if you enabled the servermetadata httpget already)
image
always, you don’t want end user to view this information directly.  hope the user will get a customer error or even 404 error. How to do that?

Write a simple Http Module and deploy it to the web server that host SVC service, And Deny those request, only support the soap+xml or soap+msbin protocal. 
image

then when user try to access the .svc directly, will get 404 error as we expect.

 image

 
Locations of visitors to this page