Mono下的WCF的Bug?
最近一段时间,一直在折腾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="2147483647" maxBufferSize="2147483647"/> </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}", -1, 2, client.Add(-1, 2)); client.Close(); Console.Read(); } } }
执行的结果如下:
Log的内容如下:
问题出现了,客户端传入的-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的问题吗?