如何配置自托管WCF服务来压缩消息



我一直在网上寻找,但我找不到一个定义的过程或一个关于如何配置基本http自托管WCF服务来压缩服务器发送给客户端的消息的示例。

我想使用GZIP或类似技术

这是我的应用程序

<?xml version="1.0" encoding="utf-8"?>
<configuration>      
<system.serviceModel>
<services>
<service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/ApplicationSvcs"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:9001/ApplicationSvcs" binding="basicHttpBinding" contract="Application.IApplicationServices"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApplicationSvcsBehave">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
</startup>
</configuration>

我是这样创建服务的:

var host = new ServiceHost(typeof(ApplicationServicesProxy));
host.Open();

服务定义在IApplicationServices中,由ApplicationServicesProxy继承

[ServiceContract]
interface IApplicationServices
{
[OperationContract]
string[] MethodOne();
}
public class AppicationServicesProxy : IApplicationServices
{
public string[] MethodOne()
{
// return a string value;
}
}

这是我连接客户端到服务的方式

var ApplicationSvcs = new ApplicationSvcs.ApplicationServicesClient("BasicHttpBinding_IApplicationServices");

我目前正在使用。net 4.8

我解决了这样做,消息大小从300 Kb/s下降到11 Kb/s,所以它真的工作,CPU不是很感兴趣:

<system.serviceModel>
<services>
<service name="Application.ApplicationServicesProxy" behaviorConfiguration="ApplicationSvcsBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9001/ApplicationSvcs"/>
</baseAddresses>
</host>
<endpoint address="" binding="customBinding" bindingConfiguration="BinaryCompressionBinding" contract="Application.IApplicationServices"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ApplicationSvcsBehave">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="BinaryCompressionBinding">
<binaryMessageEncoding compressionFormat ="GZip"/>
<httpTransport decompressionEnabled="true"/>
</binding>
</customBinding>
</bindings>
</system.serviceModel>

和客户端

<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IApplicationServices" >
<binaryMessageEncoding compressionFormat="GZip"/>
<httpTransport maxReceivedMessageSize="20000000"/>
</binding>           
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:9001/ApplicationSvcs" binding="customBinding"
bindingConfiguration="CustomBinding_IApplicationServices" contract="ApplicationSvcs.IApplicationServices"
name="CustomBinding_IApplicationServices" />          
</client>
</system.serviceModel>

SOAP消息可能非常沉重,但在我的应用程序中,我不能使用REST,所以我希望可以提供任何帮助。

相关内容

  • 没有找到相关文章

最新更新