如果调用时间超过 1 分钟,ServiceBus 上的 WCF 服务将失败

标签 wcf azure servicebus

我有一个托管在 ServiceHost 对象中的 WCF 服务。 ServiceHost 是在 Azure 辅助角色的 OnStart 方法上创建的。这是代码:

        ServiceBusEnvironment.SystemConnectivity.Mode = ConnectivityMode.Http;

        Uri baseAddress = ServiceBusEnvironment.CreateServiceUri("http", "my_sb", "SimpleService");

        host = new ServiceHost(typeof(SimpleService1.Service1), baseAddress);

        BasicHttpRelayBinding binding = new BasicHttpRelayBinding(EndToEndBasicHttpSecurityMode.None, RelayClientAuthenticationType.None);
        binding.OpenTimeout = new TimeSpan(1, 1, 0);
        binding.ReceiveTimeout = new TimeSpan(1, 10, 0);
        binding.SendTimeout = new TimeSpan(1, 10, 0);
        binding.MaxReceivedMessageSize = 73400320;
        XmlDictionaryReaderQuotas readerQuotas = new XmlDictionaryReaderQuotas();
        readerQuotas.MaxArrayLength = 73400320;
        binding.ReaderQuotas = readerQuotas;

        TransportClientEndpointBehavior sharedSecretServiceBusCredential = new TransportClientEndpointBehavior();
        sharedSecretServiceBusCredential.CredentialType = TransportClientCredentialType.SharedSecret;

        sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerName = "owner";
        sharedSecretServiceBusCredential.Credentials.SharedSecret.IssuerSecret = "blablablabla==";


        ContractDescription contractDescription = ContractDescription.GetContract(typeof(SimpleService1.IService1), typeof(SimpleService1.Service1));
        ServiceEndpoint serviceEndPoint = new ServiceEndpoint(contractDescription);
        serviceEndPoint.Address = new EndpointAddress(baseAddress);
        serviceEndPoint.Binding = binding;

        IEndpointBehavior serviceRegistrySettings = new ServiceRegistrySettings(DiscoveryType.Public);

        serviceEndPoint.Behaviors.Add(serviceRegistrySettings);
        serviceEndPoint.Behaviors.Add(sharedSecretServiceBusCredential);


        host.Description.Endpoints.Add(serviceEndPoint);

        try
        {

            host.Open();

        }

        catch (Exception ex)
        {

            Trace.WriteLine(ex.Message, "Error");

            throw;

        }

        Trace.WriteLine("SimpleService1 running...");

客户端的绑定(bind)配置为:

        <basicHttpBinding>
          <binding name="FileTransferBinding" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" maxReceivedMessageSize="73400320">
            <readerQuotas maxArrayLength="73400320"/>
            <security mode="None"/>
          </binding>
        </basicHttpBinding>

<endpoint address="http://my_sb.servicebus.windows.net/simpleservice" binding="basicHttpBinding" bindingConfiguration="FileTransferBinding" contract="Service1reference.IService1" name="FileTransferBinding" behaviorConfiguration="sbBehavior"/>

问题是,如果对服务的一次调用花费的时间超过 1 分钟,客户端就会收到此异常:

The content type application/xml; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly

如果我将绑定(bind)更改为 netTcpRelayBinding,则一切正常。

最佳答案

这是因为如果连接空闲时间超过一分钟,Windows Azure 负载平衡器就会断开您的连接。

最好的选择是使用 WCF 回调。这将执行从服务器到客户端的调用,以使其知道长时间运行的操作已完成。有关如何执行此操作的更多信息,请查看此博客 [ WCF Callbacks

关于如果调用时间超过 1 分钟,ServiceBus 上的 WCF 服务将失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7271050/

相关文章:

java - 如何使用Java创建Azure服务总线队列?

WCF 和 Windows Server 服务总线端点

python - 如何使用 POST 请求中的参数触发 Azure 函数

azure - Microsoft Service Bus 是否会为主题中的每个订阅重复消息?

asp.net - jQuery、WCF 和 ASP.NET

wcf - 通过命名管道从 Windows 服务( session #0)连接到桌面应用程序( session #1)

wcf - 如何创建 F# WCF 客户端?我不断收到 app.config 错误

.net - 试图对无法访问的主机进行套接字操作

azure - Cosmos DB 一致性

azure - HTTP 请求超时 Windows Azure 部署