예제 #1
0
        private CodeConstructor BuildConstructor2(bool hasRequest, HostedService hostedService)
        {
            CodeConstructor constructor = new CodeConstructor();

            constructor.Name       = hostedService.Name;
            constructor.Attributes = MemberAttributes.Public;

            if (hasRequest)
            {
                constructor.Parameters.Add(new CodeParameterDeclarationExpression("I" + hostedService.Name, "service"));
                constructor.ChainedConstructorArgs.Add(new CodeVariableReferenceExpression("service"));
            }

            constructor.ChainedConstructorArgs.Add(new CodeObjectCreateExpression("ProtocolVersion10"));

            return(constructor);
        }
예제 #2
0
        private CodeConstructor BuildConstructor(bool hasRequest, HostedService hostedService)
        {
            CodeConstructor constructor = new CodeConstructor();

            constructor.Name       = hostedService.Name;
            constructor.Attributes = MemberAttributes.Public;

            if (hasRequest)
            {
                constructor.Parameters.Add(new CodeParameterDeclarationExpression("I" + hostedService.Name, "service"));
            }

            constructor.Parameters.Add(new CodeParameterDeclarationExpression("ProtocolVersion", "version"));
            constructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("version"));

            if (hasRequest)
            {
                constructor.Statements.Add(new CodeCommentStatement("Set the service implementation properties"));
                // Assign service parameter to local variable
                constructor.Statements.Add(
                    new CodeAssignStatement(
                        new CodeVariableReferenceExpression("m_service"),
                        new CodeVariableReferenceExpression("service")
                        )
                    );
            }

            constructor.Statements.Add(new CodeSnippetStatement(""));
            constructor.Statements.Add(new CodeCommentStatement("Set base service properties"));
            // Add Service Namespace assignment
            constructor.Statements.Add(
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("ServiceNamespace"),
                    new CodeObjectCreateExpression(
                        "WsXmlNamespace",
                        new CodeExpression[] {
                new CodePrimitiveExpression(hostedService.Name.Substring(0, 3).ToLower()),
                new CodePrimitiveExpression(hostedService.Namespace)
            }
                        )
                    )
                );

            // Add Service ID assignment
            constructor.Statements.Add(
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("ServiceID"),
                    new CodePrimitiveExpression("urn:uuid:" + hostedService.ServiceID.ToString())
                    )
                );

            // Add Service Name assignment
            constructor.Statements.Add(
                new CodeAssignStatement(
                    new CodeVariableReferenceExpression("ServiceTypeName"),
                    new CodePrimitiveExpression(hostedService.Name)
                    )
                );

            constructor.Statements.Add(new CodeSnippetStatement(""));
            constructor.Statements.Add(new CodeCommentStatement("Add service types here"));

            // Add operations
            // Note: Only reason to loop twice is to insure operations and events are seperated for readability
            foreach (ServiceOp serviceOp in hostedService.ServiceOperations)
            {
                if (serviceOp.Operation.Messages.Flow == OperationFlow.OneWay || serviceOp.Operation.Messages.Flow == OperationFlow.RequestResponse)
                {
                    // Get action property or build default
                    string action      = serviceOp.InAction;
                    int    actionIndex = action.LastIndexOf(':');
                    actionIndex = actionIndex == -1 || actionIndex < action.LastIndexOf('/') ? action.LastIndexOf('/') : actionIndex;
                    string actionNamespace = actionIndex == -1 ? action : action.Substring(0, actionIndex);
                    string actionName      = actionIndex == -1 ? action : action.Substring(actionIndex + 1, action.Length - (actionIndex + 1));

                    constructor.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeVariableReferenceExpression("ServiceOperations"),
                            "Add",
                            new CodeObjectCreateExpression(
                                "WsServiceOperation",
                                new CodeExpression[] {
                        new CodePrimitiveExpression(actionNamespace),
                        new CodePrimitiveExpression(actionName)
                    })));
                }
                else if (serviceOp.Operation.Messages.Flow != OperationFlow.Notification)
                {
                    throw new ArgumentException("Unsupported operation type detected. " + serviceOp.Operation.Messages.Flow.ToString());
                }
            }

            constructor.Statements.Add(new CodeSnippetStatement(""));
            constructor.Statements.Add(new CodeCommentStatement("Add event sources here"));

            bool fEventsAdded = false;

            // Add events
            foreach (ServiceOp serviceOp in hostedService.ServiceOperations)
            {
                if (serviceOp.Operation.Messages.Flow == OperationFlow.Notification)
                {
                    // Get action property or build default
                    string action      = serviceOp.OutAction;
                    int    actionIndex = action.LastIndexOf(':');
                    actionIndex = actionIndex == -1 || actionIndex < action.LastIndexOf('/') ? action.LastIndexOf('/') : actionIndex;
                    string actionNamespace = actionIndex == -1 ? action : action.Substring(0, actionIndex);
                    string actionName      = actionIndex == -1 ? action : action.Substring(actionIndex + 1, action.Length - (actionIndex + 1));

                    constructor.Statements.Add(
                        new CodeMethodInvokeExpression(
                            new CodeVariableReferenceExpression("EventSources"),
                            "Add",
                            new CodeObjectCreateExpression(
                                "DpwsWseEventSource",
                                new CodeExpression[] {
                        new CodePrimitiveExpression(hostedService.Name.Substring(0, 3).ToLower()),
                        new CodePrimitiveExpression(actionNamespace),
                        new CodePrimitiveExpression(actionName)
                    })));

                    fEventsAdded = true;
                }
            }

            if (fEventsAdded)
            {
                constructor.Statements.Add(
                    new CodeMethodInvokeExpression(
                        new CodeThisReferenceExpression(),
                        "AddEventServices"
                        )
                    );
            }

            return(constructor);
        }