c# - 如何配置自托管 WCF 服务以压缩消息
问题描述
我一直在网上寻找,但找不到定义的过程或有关如何配置 basicHTTP 自托管 WCF 服务以压缩服务器发送给客户端的消息的示例。
我想使用GZIP或类似技术。
这是我的应用程序 app.config:
<?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();
服务在由 ApplicationServicesProxy 继承的 IApplicationServices 中定义
[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,所以我希望这能有所帮助。
推荐阅读
- react-native - 运行更新后的 Androidx 和 gradle 功能错误
- blockchain - 为什么区块链网络会随着块大小达到其极限而变慢?
- javascript - 从第一个数组中删除重复的索引值,按照第一个操作第二个(在某些特定条件下)
- ionic-framework - 当 nav.push 组件时,IONIC NavController 是如何工作的?
- express - 如何在另一个 app.use() 中调用 app.use()
- c++ - "{ x = a; y = b; }" 在这个 incialization 中做了什么?
- laravel - 第一次点击浏览器“返回”按钮后中间件无法正常工作 Laravel
- node.js - 当调用 Axios GET 并且 express 服务器发送 .json() 时,在 xhr.js 上捕获 404
- python - ImportError: _plotly_future_ 模块必须在 plotly 模块之前导入
- docker - Docker 将日志保存在哪里?