I have just created another test that works correctly, this time it is
not using the WcfFacility

[TestFixture]
public class DataContractSerializerOperationBehaviorTestFixture
{
    private ISimpleService _client;
    private ServiceHost _host;

    [SetUp]
    public void TestInitalize()
    {
        var binding = new BasicHttpBinding();
        var baseAddress = new Uri("http://localhost:1066/
SimpleService");

        _host = new ServiceHost(typeof(SimpleService), baseAddress);
        var endpoint = _host.AddServiceEndpoint(typeof
(ISimpleService), binding, string.Empty);

        foreach (var operation in endpoint.Contract.Operations)
        {
            var behavior =
operation.Behaviors.Find<DataContractSerializerOperationBehavior>();
            if (behavior != null)
            {
                operation.Behaviors.Remove(behavior);
                operation.Behaviors.Add(new CustomOperationBehaviour
(operation));
            }
        }

        _host.Open();

        _client = ChannelFactory<ISimpleService>.CreateChannel(new
BasicHttpBinding(), new EndpointAddress("http://localhost:1066/
SimpleService"));
    }

    [TearDown]
    public void TestCleanup()
    {
        _host.Close();
    }

    [Test]
    [ExpectedException(typeof(NotImplementedException))]
    public void ThrowExceptionDuringCreateSerializer()
    {
        var name = Guid.NewGuid().ToString();
        var foo = _client.Fetch(name);
    }
}

public class NetDataContractFormatBehavior : IOperationBehavior
{
    #region IOperationBehavior Members

    public void AddBindingParameters(OperationDescription description,
BindingParameterCollection parameters)
    {
    }

    public void ApplyClientBehavior(OperationDescription description,
ClientOperation proxy)
    {
        ReplaceDataContractSerializerOperationBehavior(description);
    }

    public void ApplyDispatchBehavior(OperationDescription
description, DispatchOperation dispatch)
    {
        ReplaceDataContractSerializerOperationBehavior(description);
    }

    public void Validate(OperationDescription description)
    {
    }

    #endregion

    public static void ReplaceDataContractSerializerOperationBehavior
(OperationDescription description)
    {
        var behavior =
description.Behaviors.Find<DataContractSerializerOperationBehavior>();

        if (behavior != null)
        {
            description.Behaviors.Remove(behavior);
            description.Behaviors.Add(new CustomOperationBehaviour
(description));
        }
    }
}

public class CustomOperationBehaviour :
DataContractSerializerOperationBehavior
{
    public CustomOperationBehaviour(OperationDescription
operationDescription)
        : base(operationDescription)
    {
    }

    public override XmlObjectSerializer CreateSerializer(Type type,
string name, string ns, IList<Type> knownTypes)
    {
        throw new NotImplementedException();
    }

    public override XmlObjectSerializer CreateSerializer(Type type,
XmlDictionaryString name, XmlDictionaryString ns, IList<Type>
knownTypes)
    {
        throw new NotImplementedException();
    }
}

[ServiceContract]
public interface ISimpleService
{
    [OperationContract]
    Foo Fetch(string name);

    [OperationContract]
    string Save(Foo foo);
}

public class SimpleService : ISimpleService
{
    public Foo Fetch(string name)
    {
        return new Foo { Name = name };
    }

    public string Save(Foo foo)
    {
        return foo.Name;
    }
}

[DataContract]
public class Foo
{
    [DataMember]
    public string Name { get; set; }
}
-- 
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.


Reply via email to