public static void InstanceContextMode_Single()
        {
            SingleInstanceContextSimpleService.ClearCounts();
            var serviceInstance = new SingleInstanceContextSimpleService();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleInstanceContextSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(serviceInstance);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            Assert.Equal(1, SingleInstanceContextSimpleService.AddBindingParametersCallCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ApplyDispatchBehaviorCount);
            Assert.Equal(1, SingleInstanceContextSimpleService.ValidateCallCount);
            string echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            Assert.Equal(1, SingleInstanceContextSimpleService.CreationCount);
            Assert.Equal(0, SingleInstanceContextSimpleService.DisposalCount);
            Assert.Equal(2, serviceInstance.CallCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#2
0
        public static void ComplexMessage_OperationFormatUseLiteral()
        {
            System.ServiceModel.ChannelFactory <IEchoSoapService> factory = DispatcherHelper
                                                                            .CreateChannelFactory <EchoSoapService, IEchoSoapService>();
            factory.Open();
            IEchoSoapService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            var expected = new ComplexMessage
            {
                Date          = System.DateTime.Now,
                InnerMessages = new InnerComplexMessage[]
                {
                    new InnerComplexMessage
                    {
                        Guid = System.Guid.NewGuid(),
                    },
                },
            };
            ComplexMessage actual = channel.GetComplexMessageLiteral(expected);

            Assert.Equal(expected, actual);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InstanceContextMode_PerCall()
        {
            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerCallInstanceContextSimpleServiceAndBehavior, ISimpleService>(
                (services) =>
            {
                services.AddTransient <PerCallInstanceContextSimpleServiceAndBehavior>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance created as part of service startup to probe if type is availale in DI
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount);
            // Instance not disposed as it implements IServiceBehavior and is added to service behaviors
            Assert.Equal(0, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.AddBindingParametersCallCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ApplyDispatchBehaviorCount);
            Assert.Equal(1, PerCallInstanceContextSimpleServiceAndBehavior.ValidateCallCount);

            PerCallInstanceContextSimpleServiceAndBehavior.ClearCounts();
            string echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            PerCallInstanceContextSimpleServiceAndBehavior.WaitForDisposalCount(2, TimeSpan.FromSeconds(30));
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(2, PerCallInstanceContextSimpleServiceAndBehavior.DisposalCount);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#4
0
        public static void ServiceThrowsTimeoutException()
        {
            var factory = DispatcherHelper.CreateChannelFactory <ThrowingService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(new ThrowingService(new TimeoutException()));
            });

            factory.Open();
            var channel = factory.CreateChannel();

            System.ServiceModel.FaultException exceptionThrown = null;
            try
            {
                var echo = channel.Echo("hello");
            }
            catch (System.ServiceModel.FaultException e)
            {
                exceptionThrown = e;
            }

            Assert.NotNull(exceptionThrown);
            Assert.True(exceptionThrown.Code.IsReceiverFault);
            Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#5
0
        public static void InstanceContextMode_PerSession_WithBehavior_NoInjection()
        {
            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleServiceAndBehavior, ISimpleSessionService>();

            factory.Open();
            var channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance created as part of service startup as it implements IServiceBehavior
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            // Instance not disposed as it implements IServiceBehavior and is added to service behaviors
            Assert.Equal(0, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.AddBindingParametersCallCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.ApplyDispatchBehaviorCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.ValidateCallCount);

            PerSessionInstanceContextSimpleServiceAndBehavior.ClearCounts();
            var echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            echo = channel.Echo("hello");
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.CreationCount);
            Assert.Equal(1, PerSessionInstanceContextSimpleServiceAndBehavior.DisposalCount);
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#6
0
        public static void ServiceThrowsExceptionDetailsIncludedInFault()
        {
            string exceptionMessage    = "This is the exception message";
            string stackTraceTopMethod = "   at ErrorHandling.ThrowingService.Echo(String echo)";

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <ThrowingDetailInFaultService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(new ThrowingDetailInFaultService(new Exception(exceptionMessage)));
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.IClientChannel)channel).Open();
            System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> exceptionThrown = Assert.Throws <System.ServiceModel.FaultException <System.ServiceModel.ExceptionDetail> >(() =>
            {
                _ = channel.Echo("hello");
            });
            Assert.NotNull(exceptionThrown);
            Assert.NotNull(exceptionThrown.Detail);
            Assert.True(exceptionThrown.Code.IsReceiverFault);
            System.ServiceModel.ExceptionDetail detail = exceptionThrown.Detail;
            Assert.Equal(exceptionMessage, detail.Message);
            Assert.StartsWith(stackTraceTopMethod, detail.StackTrace);
            Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void ReplacementMessageUsed()
        {
            string replacementEchoString = "bbbbb";
            var    inspector             = new MessageReplacingDispatchMessageInspector(replacementEchoString);
            var    behavior = new TestServiceBehavior {
                DispatchMessageInspector = inspector
            };
            var service = new DispatcherTestService();
            var factory = DispatcherHelper.CreateChannelFactory <DispatcherTestService, ISimpleService>(
                (services) =>
            {
                services.AddSingleton <IServiceBehavior>(behavior);
                services.AddSingleton(service);
            });

            factory.Open();
            var channel = factory.CreateChannel();
            var echo    = channel.Echo("hello");

            Assert.Equal(replacementEchoString, service.ReceivedEcho);
            Assert.Equal(replacementEchoString, echo);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#8
0
        public static async Task AsyncServiceThrowsTimeoutExceptionAfterAwait()
        {
            System.ServiceModel.ChannelFactory <ISimpleAsyncService> factory = DispatcherHelper.CreateChannelFactory <ThrowingAsyncService, ISimpleAsyncService>(
                (services) =>
            {
                services.AddSingleton(new ThrowingAsyncService(new TimeoutException(), beforeAwait: false));
            });
            factory.Open();
            ISimpleAsyncService channel = factory.CreateChannel();

            ((System.ServiceModel.IClientChannel)channel).Open();
            System.ServiceModel.FaultException exceptionThrown = null;
            try
            {
                string echo = await channel.EchoAsync("hello");
            }
            catch (System.ServiceModel.FaultException e)
            {
                exceptionThrown = e;
            }

            Assert.NotNull(exceptionThrown);
            Assert.True(exceptionThrown.Code.IsReceiverFault);
            Assert.Equal("InternalServiceFault", exceptionThrown.Code.SubCode.Name);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#9
0
        public static void InstanceContextMode_PerSession_NoInjection()
        {
            PerSessionInstanceContextSimpleService.ClearCounts();
            var factory = DispatcherHelper.CreateChannelFactory <PerSessionInstanceContextSimpleService, ISimpleSessionService>();

            factory.Open();
            var channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            // Instance shouldn't be created as part of service startup to as type isn't available in DI
            Assert.Equal(0, PerSessionInstanceContextSimpleService.CreationCount);
            Assert.Equal(0, PerSessionInstanceContextSimpleService.DisposalCount);

            PerSessionInstanceContextSimpleService.ClearCounts();
            var echo = channel.Echo("hello");

            echo = channel.Echo("hello");
            echo = channel.Echo("hello");
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.Equal(1, PerSessionInstanceContextSimpleService.CreationCount);
            PerSessionInstanceContextSimpleService.WaitForDisposalCount(1, TimeSpan.FromSeconds(30));
            Assert.Equal(1, PerSessionInstanceContextSimpleService.DisposalCount);
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#10
0
 internal static ChannelFactory <TContract> CreateChannelFactory <TService, TContract>(IServiceBehavior serviceBehavior) where TService : class
 {
     return(DispatcherHelper.CreateChannelFactory <TService, TContract>(
                (IServiceCollection services) =>
     {
         services.AddSingleton(serviceBehavior);
     }));
 }
示例#11
0
        public void IsSingletonShouldBeFalseWhenInstanceContextModeIsPerSession()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <PerSessionSimpleService, ISimpleService>();
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);

            factory.Close();
        }
示例#12
0
        public void IsSingletonShouldBeTrueWhenInstanceContextModeIsSingleAndServiceIsRegisteredInDI()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddTransient <SingleSimpleService>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);

            factory.Close();
        }
        public static void AttributeNoPropertiesContract()
        {
            var factory = DispatcherHelper.CreateChannelFactory <ServiceModelSimpleService, IServiceModelSimpleService>();

            factory.Open();
            var channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            var echo = channel.Echo("hello");

            Assert.Equal("hello", echo);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#14
0
        public void ServiceProviderShouldBeExposedThroughOperationContextInstanceContextExtensionsWhenSingleServiceIsRegisteredWithinDI()
        {
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <SingleSimpleServiceUsingServiceProviderFromOperationContext, ISimpleService>(
                (services) =>
            {
                services.AddTransient <SingleSimpleServiceUsingServiceProviderFromOperationContext>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            channel.Echo(input);
            channel.Echo(input);

            factory.Close();
        }
示例#15
0
        public static void Echo_OperationFormatUseLiteral()
        {
            System.ServiceModel.ChannelFactory <IEchoSoapService> factory = DispatcherHelper
                                                                            .CreateChannelFactory <EchoSoapService, IEchoSoapService>();
            factory.Open();
            IEchoSoapService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.EchoLiteral("hello");

            Assert.Equal("hello", echo);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InjectedTransientInstanceWithServiceBehaviorPerCall_Succeeds()
        {
            DisposableSimpleService.InstantiationCount = 0;
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleServiceWithServiceBehaviorPerCall, ISimpleService>(
                (services) =>
            {
                services.AddTransient <DisposableSimpleServiceWithServiceBehaviorPerCall>();
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
示例#17
0
        public static void AttributesForMessageContract()
        {
            var factory = DispatcherHelper.CreateChannelFactory <ServiceModelSimpleService, IServiceModelSimpleService>();

            factory.Open();
            var channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            var echo = channel.EchoWithMessageContract(new EchoMessageRequest()
            {
                Text = "Message Hello", APIKey = "DEVKEYTOTEST"
            });

            Assert.NotNull(echo);
            Assert.NotEmpty(echo.SayHello);
            Assert.NotEmpty(echo.SayHi);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
示例#18
0
        public static void ServiceSendFaultMessage()
        {
            var fault = new TestFault {
                Message = Guid.NewGuid().ToString()
            };
            var reason = new FaultReason(Guid.NewGuid().ToString());
            var code   = new FaultCode(nameof(ServiceSendFaultMessage));

            System.ServiceModel.ChannelFactory <IFaultingService> factory = DispatcherHelper.CreateChannelFactory <FaultingService, IFaultingService>(
                (services) =>
            {
                services.AddSingleton(new FaultingService(fault, reason, code));
            });
            factory.Open();
            IFaultingService channel         = factory.CreateChannel();
            Exception        exceptionThrown = null;

            try
            {
                channel.FaultingOperation();
            }
            catch (Exception e)
            {
                exceptionThrown = e;
            }

            Assert.NotNull(exceptionThrown);
            Assert.IsType <System.ServiceModel.FaultException <TestFault> >(exceptionThrown);
            var faultException = (System.ServiceModel.FaultException <TestFault>)exceptionThrown;

            Assert.Equal(fault.Message, faultException.Detail.Message);
            Assert.Equal(reason.ToString(), faultException.Reason.ToString());
            Assert.Equal(code.Name, faultException.Code.Name);
            // Empty string FaultCode namespace becomes default soap envelope ns
            Assert.Equal("http://www.w3.org/2003/05/soap-envelope", faultException.Code.Namespace);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }
        public static void InjectedSingletonInstanceWithServiceBehaviorSingle_NotDisposed()
        {
            DisposableSimpleService.InstantiationCount = 0;
            var serviceInstance = new DisposableSimpleServiceWithServiceBehaviorSingle();

            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleServiceWithServiceBehaviorSingle, ISimpleService>(
                (services) =>
            {
                services.AddSingleton(serviceInstance);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            Assert.False(serviceInstance.IsDisposed, "Service instance shouldn't be disposed");
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
        public static void InjectedTransientInstanceInjectedServiceBehaviorPerCall_Succeeds()
        {
            DisposableSimpleService.InstantiationCount = 0;
            var serviceBehaviorAttr = new CoreWCF.ServiceBehaviorAttribute();

            serviceBehaviorAttr.InstanceContextMode = CoreWCF.InstanceContextMode.PerCall;
            serviceBehaviorAttr.ConcurrencyMode     = CoreWCF.ConcurrencyMode.Multiple;
            System.ServiceModel.ChannelFactory <ISimpleService> factory = DispatcherHelper.CreateChannelFactory <DisposableSimpleService, ISimpleService>(
                (services) =>
            {
                services.AddTransient <DisposableSimpleService>();
                services.AddSingleton <IServiceBehavior>(serviceBehaviorAttr);
            });
            factory.Open();
            ISimpleService channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
            Assert.Equal(1, DisposableSimpleService.InstantiationCount);
        }
示例#21
0
 internal static ChannelFactory <TContract> CreateChannelFactory <TService, TContract>(Action <CoreWCF.ServiceHostBase> configureServiceHostBase) where TService : class
 {
     return(DispatcherHelper.CreateChannelFactory <TService, TContract>(
                (IServiceCollection services) => { }, configureServiceHostBase));
 }
        public static void AttributeWithNameNamespaceActionReplyActionContract()
        {
            System.ServiceModel.ChannelFactory <IServiceModelServiceWithPropertiesSet> factory = DispatcherHelper.CreateChannelFactory <ServiceModelSimpleService, IServiceModelServiceWithPropertiesSet>();
            factory.Open();
            IServiceModelServiceWithPropertiesSet channel = factory.CreateChannel();

            ((System.ServiceModel.Channels.IChannel)channel).Open();
            string echo = channel.Echo("hello");

            Assert.Equal("hello", echo);
            ((System.ServiceModel.Channels.IChannel)channel).Close();
            factory.Close();
            TestHelper.CloseServiceModelObjects((System.ServiceModel.Channels.IChannel)channel, factory);
        }