首页 技术 正文
技术 2022年11月6日
0 收藏 610 点赞 587 浏览 3234 个字

最近一段时间,一直在折腾Mono,折腾Linux。让我无比痛苦的是Mono下的WCF的坑真的是太多了,这不又遇到了一个莫名其妙的问题。

环境:mono 3.2.1,Jexus 5.4.3,OS CentOS 6.2。

1:定义服务契约

using System.ServiceModel;namespace BugTest.Contract
{
[ServiceContract(Name = "Calculator", Namespace = "http://www.wcfbugtest.com")]
public interface ICalculator
{
[OperationContract]
int Add(int x, int y);
}
}

2:实现服务

WriteInfo方法主要是为了记录当时调用Add方法,传入的X,Y值,也是为了更好的展现这个Bug。
using System;
using System.IO;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using BugTest.Contract;namespace BugTest.Service
{
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class CalculatorService : ICalculator
{
public int Add(int x, int y)
{
WriteInfo(string.Format("x={0},y={1}", x, y));
return x + y;
} public void WriteInfo(string logInfo)
{
var type = Type.GetType("Mono.Runtime");
if (type != null)
{
string filePath = "/var/www/getextent/Logs/Info.log";
using (var sw = new StreamWriter(filePath, true, Encoding.UTF8))
{
sw.Write(logInfo + "\r\n");
sw.Flush();
}
}
}
}
}

3:寄宿服务

   主要是看Web.config,我的生产环境客户端使用的是Silverlight,终结点使用的绑定是customBinding,编码方法采用的是Binary Message Encoding,主要是为了减少服务端与客户端传输消息的大小,能够稍微的提升性能。此外,要在Mono下跑WCF,要为站点添加一个Global.asax,在Application_Start添加如下代码:

protected void Application_Start(object sender, EventArgs e)
{
Environment.SetEnvironmentVariable("MONO_STRICT_MS_COMPLIANT", "yes");
}

详细的配置如下:

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<customErrors mode="Off"/>
<httpRuntime/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<customBinding>
<binding name="customBinding0" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00">
<binaryMessageEncoding/>
<httpTransport maxReceivedMessageSize="" maxBufferSize=""/>
</binding>
</customBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service behaviorConfiguration="ServiceBehavior" name="BugTest.Service.CalculatorService">
<endpoint address="http://192.168.8.141:6060/CalculatorService.svc" binding="customBinding" bindingConfiguration="customBinding0" contract="BugTest.Contract.ICalculator"/>
</service>
</services>
</system.serviceModel>
</configuration>

最后发布站点,部署到Linux下面的Jexus Web服务器上。

4:客户端调用

客户端,可以使用控制台应用程序,添加服务引用,输入服务的地址,生成好客户端的代理文件,及App.config文件。

测试代码如下:

using System;
using MonoClient.ServiceReference;namespace MonoClient
{
class Program
{
static void Main(string[] args)
{
var client = new CalculatorClient();
Console.WriteLine("x + y = {2} where x = {0} and y = {1}", -, , client.Add(-, ));
client.Close(); Console.Read();
}
}
}

执行的结果如下:

Mono下的WCF的Bug?

Log的内容如下:

Mono下的WCF的Bug?

问题出现了,客户端传入的-1,在服务端莫名其妙地变成了255了。A bug ???

在windows下,使用IIS寄宿服务,是不会有问题。

此外如果使用baseHttpBinding,在Mono下WCF也是不会有问题的。即将配置按下面的方式修改:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="BugTest.Service.CalculatorService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="basicHttpBinding" contract="BugTest.Contract.ICalculator"/>
</service>
</services>
</system.serviceModel>

问题出在哪里呢? 是binaryMessageEncoding的问题吗?

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,285