private CodeMemberMethod BuildProxyCallbackMethod(ServiceOp serviceOp, ClientProxy clientProxy) { string reqElementName = null; string reqTypeName = null; string reqElementNamespace = null; foreach (Message message in clientProxy.Messages) { if (serviceOp.Operation.Messages.Output != null && serviceOp.Operation.Messages.Output.Message.Name == message.Name) { reqElementName = CodeGenUtils.GetMessageElementName(serviceOp.Operation.PortType.ServiceDescription, message); reqTypeName = CodeGenUtils.GetMessageTypeName(serviceOp.Operation.PortType.ServiceDescription, message); reqElementNamespace = message.Parts[0].Element == null ? message.Parts[0].Type.Namespace : message.Parts[0].Element.Namespace; break; } } // If request element name is null set to default for serializer reqElementName = reqElementName == null ? serviceOp.Operation.Name + "Request" : reqElementName; // If request type null set to default "void" reqTypeName = reqTypeName == null ? "void" : reqTypeName; // Set the callback method name from Action. string methodName = serviceOp.OutAction; if (methodName == null) { methodName = serviceOp.Operation.Name; } else { int index = methodName.LastIndexOfAny(new char[] { ':', '/', '\\' }); methodName = (index == -1 || index == (methodName.Length - 1)) ? methodName : methodName.Substring(index + 1); } // Create the callback handler method CodeMemberMethod codeMethod = new CodeMemberMethod(); codeMethod.Name = methodName; codeMethod.Attributes = MemberAttributes.Public; codeMethod.ReturnType = new CodeTypeReference("WsMessage"); //"Byte", 1); codeMethod.Parameters.Add(new CodeParameterDeclarationExpression("WsMessage", "request")); //codeMethod.Parameters.Add(new CodeParameterDeclarationExpression("WsWsaHeader", "header")); //codeMethod.Parameters.Add(new CodeParameterDeclarationExpression("XmlReader", "reader")); // If message body is empty skip read serializer if (reqTypeName != "void") { codeMethod.Statements.Add(new CodeCommentStatement("Build request object")); // Generated Code: OperationNameDataContractSerializer reqDcs; codeMethod.Statements.Add( new CodeVariableDeclarationStatement( CodeGenUtils.GenerateDcsName(reqTypeName), "reqDcs" ) ); // Generated Code: reqDcs = new OperationNameDataContractSerializer("InMessagePartName", InputMessageTypeNamespace); codeMethod.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("reqDcs"), new CodeObjectCreateExpression( CodeGenUtils.GenerateDcsName(reqTypeName), new CodeExpression[] { new CodePrimitiveExpression(reqElementName), new CodePrimitiveExpression(reqElementNamespace) } ) ) ); } // If the message is Mtom encoded set serializers Mtom body parts collection // req.BodyParts = this.BodyParts; if (clientProxy.EncodingType == MessageEncodingType.Mtom) { codeMethod.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("reqDcs"), "BodyParts"), new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "BodyParts") ) ); } if (reqTypeName != "void") { // Generated Code: InMessagePartName req; codeMethod.Statements.Add( new CodeVariableDeclarationStatement( reqTypeName, "req" ) ); // req = (typeName)reqDcs.ReadObject(reader); codeMethod.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("req"), new CodeCastExpression( reqTypeName, new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("reqDcs"), "ReadObject", new CodeFieldReferenceExpression( new CodeVariableReferenceExpression("request"), "Reader" ) ) ) ) ); } // Add request.Reader.Dispose() method call codeMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeFieldReferenceExpression( new CodeVariableReferenceExpression("request"), "Reader" ), "Dispose", new CodeExpression[] { } ) ); // reqest.Reader = null; codeMethod.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeVariableReferenceExpression("request"), "Reader" ), new CodePrimitiveExpression(null) ) ); codeMethod.Statements.Add(new CodeSnippetStatement("")); codeMethod.Statements.Add(new CodeCommentStatement("Call service operation to process request.")); // Add OneWay call to device developer supplied service method implementation if (reqTypeName != "void") { // Generated Code: m_class.MethodName(req); codeMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("m_eventHandler"), serviceOp.Operation.Name, new CodeExpression[] { new CodeVariableReferenceExpression("req") } ) ); } else { // Generated Code: m_class.MethodName(); codeMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("m_eventHandler"), serviceOp.Operation.Name, new CodeExpression[] { } ) ); } codeMethod.Statements.Add(new CodeSnippetStatement("")); codeMethod.Statements.Add(new CodeCommentStatement("Return OneWayResponse message for event callback messages")); codeMethod.Statements.Add( new CodeMethodReturnStatement( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("WsMessage"), "CreateOneWayResponse" ) ) ); return(codeMethod); }
private CodeConstructor BuildFirstConstructor(bool hasEvents, ClientProxy clientProxy) { CodeConstructor constructor = new CodeConstructor(); constructor.Name = clientProxy.Name + "ClientProxy"; constructor.Parameters.Add(new CodeParameterDeclarationExpression("Binding", "binding")); constructor.Parameters.Add(new CodeParameterDeclarationExpression("ProtocolVersion", "version")); constructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("binding")); constructor.BaseConstructorArgs.Add(new CodeVariableReferenceExpression("version")); constructor.Attributes = MemberAttributes.Public; if (hasEvents) { constructor.Parameters.Add(new CodeParameterDeclarationExpression(clientProxy.CallbackInterfaceName, "callbackHandler")); constructor.Statements.Add(new CodeCommentStatement("Set the client callback implementation property")); // Assign service parameter to local variable // Generated Code: m_eventHandler = callbackHandler; constructor.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("m_eventHandler"), new CodeVariableReferenceExpression("callbackHandler") ) ); } constructor.Statements.Add(new CodeSnippetStatement("")); constructor.Statements.Add(new CodeCommentStatement("Set client endpoint address")); // Create RequestChannel // Generated Code: m_requestChannel = m_localBinding.CreateClientChannel(new ClientBindingContext()); constructor.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("m_requestChannel"), new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("m_localBinding"), "CreateClientChannel", new CodeObjectCreateExpression("ClientBindingContext", new CodeVariableReferenceExpression("m_version") ) ) ) ); //if (clientProxy.EncodingType == MessageEncodingType.Mtom) //{ // constructor.Statements.Add(new CodeSnippetStatement("")); // constructor.Statements.Add(new CodeCommentStatement("Create the BodyParts instance")); // // Generated Code: BodyParts = new WsMtomBodyParts(); // constructor.Statements.Add( // new CodeAssignStatement( // new CodeVariableReferenceExpression("BodyParts"), // new CodeObjectCreateExpression( // "WsMtomBodyParts", // new CodeExpression[] {} // ) // ) // ); //} // Add event service callbacks bool firstPass = true; foreach (ServiceOp serviceOp in clientProxy.ServiceOperations) { if (serviceOp.Operation.Messages.Flow == OperationFlow.Notification) { if (firstPass) { constructor.Statements.Add(new CodeSnippetStatement("")); constructor.Statements.Add(new CodeCommentStatement("Add client callback operations and event source types")); firstPass = false; } // 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)); // Generated Code: ServiceOperations.Add(new WsServiceOperation("callback namespace", "calback method name")); constructor.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("ServiceOperations"), "Add", new CodeObjectCreateExpression( "WsServiceOperation", new CodeExpression[] { new CodePrimitiveExpression(actionNamespace), new CodePrimitiveExpression(actionName) } ) ) ); // Generated Code: EventSources.Add(new DpwsServiceType("MethodName", "Method Namespace")); constructor.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("EventSources"), "Add", new CodeObjectCreateExpression( "DpwsServiceType", new CodeExpression[] { new CodePrimitiveExpression(actionName), new CodePrimitiveExpression(actionNamespace) } ) ) ); } } if (hasEvents) { constructor.Statements.Add(new CodeSnippetStatement("")); constructor.Statements.Add(new CodeCommentStatement("Add eventing SubscriptionEnd ServiceOperations. By default Subscription End call back to this client")); // Generated Code: ServiceOperations.Add(new WsServiceOperation("WsWellKnownUri.WseNamespaceUri", "SubscriptionEnd")); constructor.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("ServiceOperations"), "Add", new CodeObjectCreateExpression( "WsServiceOperation", new CodeExpression[] { new CodeFieldReferenceExpression(new CodeVariableReferenceExpression("WsWellKnownUri"), "WseNamespaceUri"), new CodePrimitiveExpression("SubscriptionEnd") } ) ) ); constructor.Statements.Add(new CodeSnippetStatement("")); // Generated Code: this.StartEventHandlers(); constructor.Statements.Add( new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "StartEventListeners" ) ); } return(constructor); }
private CodeMemberMethod BuildInitMethod(bool hasEvents, ClientProxy clientProxy) { CodeMemberMethod initMethod = new CodeMemberMethod(); initMethod.Name = "Init"; initMethod.Attributes = MemberAttributes.Public; initMethod.Statements.Add(new CodeSnippetStatement("")); initMethod.Statements.Add(new CodeCommentStatement("// Set client endpoint address")); // Add Service ID assignment initMethod.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("EndpointAddress"), new CodePrimitiveExpression("urn:uuid:" + clientProxy.CallbackID.ToString()) ) ); if (clientProxy.EncodingType == MessageEncodingType.Mtom) { initMethod.Statements.Add(new CodeSnippetStatement("")); initMethod.Statements.Add(new CodeCommentStatement("// Create the BodyParts instance")); // Generated Code: BodyParts = new WsMtomBodyParts(); initMethod.Statements.Add( new CodeAssignStatement( new CodeVariableReferenceExpression("BodyParts"), new CodeObjectCreateExpression( "WsMtomBodyParts", new CodeExpression[] {} ) ) ); } // Add event service callbacks bool firstPass = true; foreach (ServiceOp serviceOp in clientProxy.ServiceOperations) { if (serviceOp.Operation.Messages.Flow == OperationFlow.Notification) { if (firstPass) { initMethod.Statements.Add(new CodeSnippetStatement("")); initMethod.Statements.Add(new CodeCommentStatement("Add client callback operations and event source types")); firstPass = false; } // 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)); // Generated Code: ServiceOperations.Add(new WsServiceOperation("callback namespace", "calback method name")); initMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("ServiceOperations"), "Add", new CodeObjectCreateExpression( "WsServiceOperation", new CodeExpression[] { new CodePrimitiveExpression(actionNamespace), new CodePrimitiveExpression(actionName) } ) ) ); // Generated Code: EventSources.Add(new DpwsServiceType("MethodName", "Method Namespace")); initMethod.Statements.Add( new CodeMethodInvokeExpression( new CodeVariableReferenceExpression("EventSources"), "Add", new CodeObjectCreateExpression( "DpwsServiceType", new CodeExpression[] { new CodePrimitiveExpression(actionName), new CodePrimitiveExpression(actionNamespace) } ) ) ); } } return(initMethod); }