Question

I have a self hosted WCF service for handling HTTP POST request.
My WCF service method will handle 2 types of request (1. getting data, 2. Do some action with given data).

My scenario :
1. Started Action request with large amount of data. : during the starting of that action do some processing. 2. client reads some results. 3. sending the remaining data for the previous action.

I implemented this using 'TcpListener' it is working.

Problem: when i try to implement this scenario using WCF, i have got the 'Bad request' during the first action request.

[ServiceContract]
    public interface ITest
    {
        [OperationContract, WebInvoke(Method = "POST", UriTemplate = "{*pathname}")]
        Stream GetResponse(string pathname, Stream requestBody);
    }

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Exact, ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single, UseSynchronizationContext = false, IncludeExceptionDetailInFaults = true)]
    class TestService : ITest
    {
        public Uri BaseUri { get; private set; }
        public IPAddress Address { get; private set; }
        public Int32 Port { get; private set; }
        public bool IsStarted { get { return Host != null; } }
        public ServiceHost Host { get; private set; }

        public TestService()
        {
        }

        public TestService(IPAddress ipAddress, int port)
        {
            if (ipAddress == null) { throw new ArgumentNullException("Address"); }
            this.Address = ipAddress;
            this.Port = port;
            this.BaseUri = new Uri(string.Format("http://{0}:{1}", this.Address, this.Port));
        }

    public void Start()
    {
        if (IsStarted) { throw new InvalidOperationException("Service is already started."); }
        Host = CreateServiceHost();
        Host.Open();
    }

    public void Stop()
    {
        if (!IsStarted) { throw new InvalidOperationException("Service is already stopped."); }
        Host.Close();
        Host = null;
    }

    private ServiceHost CreateServiceHost()
    {
        ServiceHost host = new ServiceHost(typeof(TestService), this.BaseUri);

        WebHttpBinding webhttpBinding = new WebHttpBinding();
        webhttpBinding.MaxBufferPoolSize = int.MaxValue;
        webhttpBinding.MaxBufferSize = int.MaxValue;
        webhttpBinding.MaxReceivedMessageSize = int.MaxValue;

        CustomBinding binding = new CustomBinding(webhttpBinding);
        WebMessageEncodingBindingElement webEncoding = binding.Elements.Find<WebMessageEncodingBindingElement>();
        webEncoding.ContentTypeMapper = new RawMapper();

        host.AddServiceEndpoint(typeof(ITest), binding, new Uri(this.BaseUri.AbsoluteUri)).Behaviors.Add(new WebHttpBehavior());

        return host;
    }

    public Stream GetResponse(string pathname, Stream requestBody)
    {
        Stream response=null;
        /**************     Process 'requestBody'    **************/

        //requestBody contains "MY_ACTION"
        //{
        //      Find the start delimeter from response : START processing
        //      Find the end delimeter from response : STOP processing
        //}

        //requestBody contains "GET_MY_DATA"
        //{
        //      Find data and send response
        //}
        return response;
    }
}

public class RawMapper : WebContentTypeMapper
{
    public override WebContentFormat GetMessageFormatForContentType(string contentType)
    {
        return WebContentFormat.Raw;
    }
}

Any help/guidance would be great as I'm really stuck on this.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top