none
发现特别无语的WCF的Bug,请问如何解决呢? RRS feed

  • 问题

  • [ServiceContract(ConfigurationName = "ISessionCalculatorV1", SessionMode = SessionMode.Required)]
        public interface ISessionCalculatorV1
        {
            [OperationContract]
            void Add(double x);
    
            [OperationContract]
            double GetResult();
        }
    [ServiceContract(ConfigurationName = "ISessionCalculatorV2", SessionMode = SessionMode.Allowed)]
        public interface ISessionCalculatorV2
        {
            [OperationContract]
            void Add(double x);
    
            [OperationContract]
            double GetResult();
        }


    [ServiceContract(ConfigurationName = "ISessionCalculatorV3", SessionMode = SessionMode.NotAllowed)]
        public interface ISessionCalculatorV3
        {
            [OperationContract]
            void Add(double x);
    
            [OperationContract]
            double GetResult();
        }


    [ServiceBehavior(ConfigurationName = "SessionCalculatorService", InstanceContextMode = InstanceContextMode.PerCall)]
        public class SessionCalculatorService : ISessionCalculatorV1, ISessionCalculatorV2, ISessionCalculatorV3
        {
            public double Result { get; private set; }
    
            public void Add(double x)
            {
                this.Result += x;
            }
    
            public double GetResult()
            {
                return this.Result;
            }
        }


    <system.serviceModel>
        <services>
          <service name="SessionCalculatorService" behaviorConfiguration="CalculatorBehavior">
            <endpoint address="http://192.168.1.122:8085/SessionCalculatorV1Service" binding="ws2007HttpBinding" contract="ISessionCalculatorV1"></endpoint>
            <endpoint address="http://192.168.1.122:8085/SessionCalculatorV2Service" binding="ws2007HttpBinding" contract="ISessionCalculatorV2"></endpoint>
            <endpoint address="http://192.168.1.122:8085/SessionCalculatorV3Service" binding="ws2007HttpBinding" contract="ISessionCalculatorV3"></endpoint>
          </service>
        </services>
      </system.serviceModel>

    如果我不在OperationContract.Name起别名的话,却又无法生成元数据。起了别名之后,生成元数据都没有问题。

    但是如下代码调用确实有问题:

    <system.serviceModel>
    <client>
          <endpoint name="SessionCalculatorV1Service"
                    address="http://192.168.1.122:8085/SessionCalculatorV1Service"
                    binding="ws2007HttpBinding"
                    contract="ISessionCalculatorV1"></endpoint>
          <endpoint name="SessionCalculatorV2Service"
                    address="http://192.168.1.122:8085/SessionCalculatorV2Service"
                    binding="ws2007HttpBinding"
                    contract="ISessionCalculatorV2"></endpoint>
          <endpoint name="SessionCalculatorV3Service"
                          address="http://192.168.1.122:8085/SessionCalculatorV3Service"
                          binding="ws2007HttpBinding"
                          contract="ISessionCalculatorV3"></endpoint>
    </client>


    public static void Invoke<TChannel>(string endpointName, Action<TChannel> serviceInvokeAction)
    {
                using (ChannelFactory<TChannel> _ChannelFactory = new ChannelFactory<TChannel>(endpointName))
                {
                    TChannel _Channel = _ChannelFactory.CreateChannel();
                    serviceInvokeAction(_Channel);
                }
     }


    Console.WriteLine("SessionMode.Required");
                Invoke<ISessionCalculatorV1>("SessionCalculatorV1Service", calculator =>
                {
                    Console.WriteLine("Add(10);");
                    calculator.Add(10);
                    Console.WriteLine("Result: {0}\n", calculator.GetResult());
                });
    
                Console.WriteLine("SessionMode.Allowed");
                Invoke<ISessionCalculatorV2>("SessionCalculatorV2Service", calculator =>
                {
                    Console.WriteLine("Add(10);");
                    calculator.Add(10);
                    Console.WriteLine("Result: {0}\n", calculator.GetResult());
                });
    
                Console.WriteLine("SessionMode.NotAllowed");
                Invoke<ISessionCalculatorV2>("SessionCalculatorV3Service", calculator =>
                {
                    Console.WriteLine("Add(10);");
                    calculator.Add(10);
                    Console.WriteLine("Result: {0}\n", calculator.GetResult());
                });

    错误信息:

    其他信息: 没有终结点在侦听可以接受消息的 http://192.168.1.122:8085/SessionCalculatorV1Service。这通常是由于不正确的地址或者 SOAP 操作导致的。如果存在此情况,请参见 InnerException 以了解详细信息。

    2015年12月9日 15:20

全部回复