Host multiple endpoints with the same contract but modify at runtimme

120 views Asked by At

I'm stuck for few days on a development a bit tricky. Explanation:

Functional need: Expose a unique service with different bindings type that share the same contract AND switch at runtime which binding to use in function of the clients (is .Net client, use net.tcp - if Java client, use http binding).

Here what I am in my Config file:

  <!-- Test Service -->
  <service name="TestService.v1.TestServiceImplementation, TestService" behaviorConfiguration="MyBehavior">
        <endpoint name="TestServiceV1Htpp" contract="ITestService.v1" address="http://localhost:6001/TestService/v1" binding="basicHttpBinding"  bindingConfiguration="HttpConf" behaviorConfiguration="HttpSOAPBehavior"/>
        <endpoint name="TestServiceV1NetTcp" contract="ITestService.v1" address="net.tcp://localhost:6002/TestService/v1" binding="customBinding" bindingConfiguration="TcpConfStream" behaviorConfiguration="TcpSOAPBehavior"/>
  </service>

TestService dataContract:

[ServiceContract(...)]
public interface ITestService : IDisposable
{
    [OperationContract]
    IEnumerable<string> GetData();
}

[ServiceBehavior(...)]
public class TestServiceImplementation : ITestService
{
     public IEnumerable<string> GetData()
    {
        yield return "Pong";
    }
}

And my "at runtime" contract's modification (in an endpoint behavior, in order to fake a stremed return result):

public sealed class CustomBehavior : IEndpointBehavior
{
    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        foreach (var msg in endpoint.Contract.Operations.First().Messages)
        {
            var part = msg.Body.ReturnValue;
            if (part != null)
                part.Type = typeof(Stream);
        }           
    }
}

Execution:

Everything works perfectly if I don't use my CustomBehavior. When I add it to the behavior configuration of my TCP endpoint (TcpSOAPBehavior), the Body.ReturnValue.Type is modified and this modification change ALL the contract of ALL my endpoint (even http...). While I just want to modify the TCP endpoint contract no touch the HTTP one... Is it possible to made a such modification ? Or theses endpoints are intended to share forever the same contract ?

1

There are 1 answers

0
user3088173 On

After several days of work, I found a solution. I had this error message:

"The InnerException message was 'Type 'System.Collections.Generic.List`1[[System.String]]' with data contract name 'ArrayOfValuationResult_testService_wsdl' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'."

I used ServiceKnownTypeAttribute on interface like in this blog: http://blogs.msdn.com/b/youssefm/archive/2009/04/21/understanding-known-types.aspx

In this way, I'm able to expose an IEnumerable but transform it in Stream and have a success call with TCP/Http both binding.