• 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 + "
    ");
                        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的问题吗?

  • 相关阅读:
    深入理解JavaScript系列
    Knockout应用开发指南(完整版) 目录索引
    js原生设计模式——8单例模式之简约版属性样式方法库
    彻底理解JavaScript原型
    Javascript模块化编程(一):模块的写法
    使用Grunt构建自动化开发环境
    js原生之一个面向对象的应用
    js原生之函数
    angular源码分析:angular中的依赖注入式如何实现的
    js原生之scrollTop、offsetHeight和offsetTop等属性用法详解
  • 原文地址:https://www.cnblogs.com/supperwu/p/3406731.html
Copyright © 2020-2023  润新知