示例#1
0
        private static ServiceAssembly CreateAssemblyRegistration(Assembly assembly)
        {
            var messagesById = new ServiceMessageByIdCollection();
            var messagesByType = new ServiceMessageByTypeCollection();
            var typeModel = RuntimeTypeModel.Create();

            foreach (var type in assembly.GetTypes())
            {
                var messageAttributes = type.GetCustomAttributes(typeof(ProtoMessageAttribute), true);

                if (messageAttributes.Length == 0)
                    continue;

                Debug.Assert(messageAttributes.Length == 1);

                var contractAttributes = type.GetCustomAttributes(typeof(ProtoContractAttribute), true);

                if (contractAttributes.Length == 0)
                    throw new ProtoChannelException(String.Format("Type '{0}' specifies the ProtoMessage attribute but not the ProtoContract attribute", type));

                var messageAttribute = (ProtoMessageAttribute)messageAttributes[0];

                var message = new ServiceMessage(messageAttribute, type);

                messagesById.Add(message);
                messagesByType.Add(message);

                typeModel.Add(type, true);
            }

            if (messagesById.Count == 0)
                throw new ProtoChannelException(String.Format("Assembly '{0}' does not contain any messages", assembly));

            return new ServiceAssembly(assembly, typeModel, messagesById, messagesByType);
        }
示例#2
0
        private Service CreateServiceRegistration(Type serviceType)
        {
            var methods = new ServiceMethodCollection();
            var messages = new ServiceMessageByIdCollection();

            foreach (var method in serviceType.GetMethods())
            {
                var methodAttributes = method.GetCustomAttributes(typeof(ProtoMethodAttribute), true);

                if (methodAttributes.Length == 0)
                    continue;

                Debug.Assert(methodAttributes.Length == 1);

                var methodAttribute = (ProtoMethodAttribute)methodAttributes[0];

                var serviceMethod = new ServiceMethod(method, methodAttribute, this);

                if (methods.ContainsKey(serviceMethod.Request))
                    throw new ProtoChannelException(String.Format("Invalid service contract '{0}'; multiple ProtoMethod's found for message type '{1}'", serviceType, serviceMethod.Request.Type));

                if (!messages.ContainsKey(serviceMethod.Request.Id))
                    messages.Add(serviceMethod.Request);

                if (serviceMethod.Response != null && !messages.ContainsKey(serviceMethod.Response.Id))
                    messages.Add(serviceMethod.Response);

                methods.Add(serviceMethod);
            }

            if (methods.Count == 0)
                throw new ProtoChannelException(String.Format("Invalid service contract '{0}'; contract does not specify any handlers", serviceType));

            return new Service(serviceType, methods, messages, this);
        }