WCF大型请求数据并发处理

时间:2014-03-03 13:25:36

标签: wcf concurrency

我有一个自托管的WCF服务来处理HTTP POST请求 我的WCF服务方法将处理2种类型的请求(1.获取数据,2。对给定数据执行某些操作)。

我的情景
1.已启动包含大量数据的操作请求。 :在该动作开始时做一些处理。 2.客户端读取一些结果。 3.发送上一个操作的剩余数据。

我使用'TcpListener'实现了它,它正在工作。

问题: 当我尝试使用WCF实现此场景时,我在第一个操作请求期间收到了“错误请求”。

[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;
    }
}

任何帮助/指导都会很棒,因为我真的坚持这个。

0 个答案:

没有答案