Esempio n. 1
0
        public void TestExceptionMessageWithStackTrace()
        {
            ExceptionThrower et = new ExceptionThrower();
            var api             = new InteropApi {
                CopyExceptionDetailsToThreadErrorMessage = true
            };

            api.HandleException(et.GetFabricException());

            var errorMessage = GetLastErrorMessageOnThread();

            Assert.IsTrue(errorMessage.Contains(TestExceptionMessage), "Should contain expected error message.");
            Assert.IsTrue(errorMessage.Contains("at System.Fabric.Test.Interop.InteropApiTest.ExceptionThrower.ThrowFabricException"), "Should contain excpected stackTrace.");
        }
Esempio n. 2
0
        public void TestExceptionWithLongMessageAndStackTrace()
        {
            ExceptionThrower et = new ExceptionThrower {
                HasLongMessage = true
            };
            var api = new InteropApi {
                CopyExceptionDetailsToThreadErrorMessage = true
            };

            api.HandleException(et.GetFabricException());

            var errorMessage = GetLastErrorMessageOnThread();

            Assert.IsTrue(errorMessage.Contains("testExceptionMessagezzzzz"), "Should contain expected error message.");
            Assert.IsTrue((errorMessage.Length <= MessageMaxLength), "Message should be less than " + MessageMaxLength + " in length.");
            Assert.IsTrue(errorMessage.Contains("at System.Fabric.Test.Interop.InteropApiTest.ExceptionThrower.ThrowFabricException"), "Should contain excpected stackTrace.");
        }
        private TReturnValue CreateHelper <TFactory, TReturnValue>(
            IntPtr nativeServiceType,
            IntPtr nativeServiceName,
            uint initializationDataLength,
            IntPtr nativeInitializationData,
            Guid partitionId,
            Func <TFactory, ServiceInitializationParameters, TReturnValue> creationFunc,
            Action <TReturnValue, ServiceInitializationParameters> initializationFunc,
            ServiceInitializationParameters initializationParameters)
            where TReturnValue : class
            where TFactory : class
        {
            ServiceFactoryBroker.ValidatePublicApiArguments(nativeServiceType, nativeServiceName);

            string managedServiceType = NativeTypes.FromNativeString(nativeServiceType);
            string managedServiceName = NativeTypes.FromNativeString(nativeServiceName);

            byte[] initializationData = ServiceFactoryBroker.ParseInitializationData(initializationDataLength, nativeInitializationData);

            Uri serviceNameUri = null;

            if (managedServiceName.StartsWith("fabric:", StringComparison.Ordinal))
            {
                serviceNameUri = new Uri(managedServiceName);

                AppTrace.TraceSource.WriteNoise(
                    "ServiceFactoryBroker.CreateInstance",
                    "Creating Instance for {0} Uri {1} partitionId {2}",
                    managedServiceType,
                    serviceNameUri.OriginalString,
                    partitionId);
            }
            else
            {
                AppTrace.TraceSource.WriteNoise(
                    "ServiceFactoryBroker.CreateInstance",
                    "Creating Instance for {0} System Service {1} partitionId {2}",
                    managedServiceType,
                    managedServiceName,
                    partitionId);
            }

            TFactory factory = this.serviceFactory as TFactory;

            if (factory == null)
            {
                AppTrace.TraceSource.WriteError("ServiceFactoryBroker.CreateInstanceHelper", "ServiceFactory type is incorrect for creation call {0}", this.serviceFactory.GetType().FullName);
                throw new InvalidOperationException(StringResources.Error_ServiceFactoryBroker_Invalid_ServiceFactoryType);
            }

            initializationParameters.InitializationData = initializationData;
            initializationParameters.ServiceTypeName    = managedServiceType;
            initializationParameters.ServiceName        = serviceNameUri;
            initializationParameters.PartitionId        = partitionId;

            TReturnValue instance;

            try
            {
                instance = creationFunc(factory, initializationParameters);
            }
            catch (Exception e)
            {
                AppTrace.TraceSource.WriteExceptionAsWarning("ServiceFactoryBroker.CreateInstanceHelper", e, "the passed in servicefactory threw exception");
                CreateReplicaOrInstanceApi.HandleException(e);
                throw;
            }

            if (instance == null)
            {
                AppTrace.TraceSource.WriteError("ServiceFactoryBroker.CreateInstance", "ServiceFactory returned null {0}", this.serviceFactory.GetType().FullName);
                throw new InvalidOperationException(StringResources.Error_ServiceFactoryBroker_Null_Return);
            }

            try
            {
                initializationFunc(instance, initializationParameters);
            }
            catch (Exception e)
            {
                AppTrace.TraceSource.WriteExceptionAsWarning("ServiceFactoryBroker.CreateInstanceHelper", e, "the service threw an exception in Initialize");
                CreateReplicaOrInstanceApi.HandleException(e);
                throw;
            }

            return(instance as TReturnValue);
        }